Javascript:
function setitem(id, phpvalue){
var data = {
Sub_category: $("#Subcategory").val()
}
var formsubmission = '<?php echo base_url().'index.php/AJAX/get_item'?>';
$.ajax({
method: "POST",
url: formsubmission,
data: data,
success: function(response){
$("#Item_"+id).find('option').not(':first').remove();
$("#Item_"+id+" option:first").after(response);
$("#Item_"+id).prop("disabled", false);
var item_value = '<?php echo $Items["+phpvalue+"]["Item"]?>';
$("#Item_"+id).val('<?php echo $Items[0]["Item"]?>');
$("#Item_"+id).selectpicker('refresh');
}
})
}
Anyone can please tell me How can I use phpvalue as an array index?
-
3Possible duplicate of Passing javascript value to PHPanteAdamovic– anteAdamovic2017年10月11日 08:41:51 +00:00Commented Oct 11, 2017 at 8:41
-
in simple words you cannot do it that way - when the javascript is to be executed (and phpvalue exist), the php would have already run.Calimero– Calimero2017年10月11日 08:42:38 +00:00Commented Oct 11, 2017 at 8:42
-
2Possible duplicate of What is the difference between client-side and server-side programming?Calimero– Calimero2017年10月11日 08:44:46 +00:00Commented Oct 11, 2017 at 8:44
-
Can be achieved by converting php array to json then javascript array.Naga– Naga2017年10月11日 08:45:22 +00:00Commented Oct 11, 2017 at 8:45
2 Answers 2
You want to use the PHP variable in your Javascript script so you don't want to convert javascript value to PHP but the opposite : convert php value to javascript like :
var formsubmission = <?php echo base_url() ?> + 'index.php/AJAX/get_item';
Comments
If you want to transfer a javascript variable to a php variable, there are 2 ways of doing the same.
Here's How
You can run an ajax request and then shoot an ajax request to your server and save the data into a variable using $_SESSION and then get the data back onto 'a php backed page' using the same session variable.
P.S. Make sure you wrap everything inside an if block because if the ajax request fails due to any reason,then you won't be very glad to have an unassigned php variable being used somewhere.