Antee86 Antee86 - 1 month ago 5
PHP Question

How to push variable into php array using jquery click event

The button:

<button id="animals" type="button" value="push" >Push</button>


I have a php array:

$animalArray = array($dog, $cat, $horse, $lamb, $fox);


This is the jquery:

$(document).ready(function() {
$("#animals").click(function(){
// here comes the array push something like this
array_push($animalArray, $lion);
});
});


I know the 'array_push($animalArray, $lion);' php code and not possible to run in the jquery code. I have tried session to store array, but did not work for me. I appreciate every help. Thanks.

Answer

You have to write a server side script with PHP (that handles the content of your array) which is then called via Ajax from within your jQuery code (on the client side). It is not possible to do this in a single script either on client side nor on server side.