javascript - jQuery $.post problems (Not sending variable to PHP) -
i came across little problem , struggling solve it... wondering if had ideas... have read lot of stackoverflow posts same issues, still can't solve mine. workin in codeigniter.
i trying send variable php controller form view using jquery ($.post). here mine js code, embedded view:
$('#testbtn').on('click', function() { var ordervalue = this.value; $.post('http://localhost/codeigniter/welcome/index', {val: 'myvalue'}, function(data) { alert(data); }); });
here php code in controller:
$a = isset($_post['val']) ? $_post['val'] : 'not set yet.'; echo $a ;
here button: <button id="testbtn" value="thevalue">button</button>
the button clicked, alert message variable , html code of whole page, php echo doesn't change, still says: "not set yet.".
basically seems easy, can't still find error...
without diving code more, work.
html:
<button id="testbtn" value="thevalue">button</button>
javascript:
$('#testbtn').on('click', function() { var ordervalue = {"ordervalue": $(this).val()}; $.ajax({ url: 'http://localhost/codeigniter/welcome/index', method: 'post', data: ordervalue, success: function(data) { alert(data); } });
php:
public function index() { $a = isset($_post['ordervalue']) ? $_post['ordervalue'] : 'not set yet.'; return a; }
as stated in other comments though, may not best practice.
Comments
Post a Comment