please refer to the code below.
$dArr = '<script>document.write(volunteerDist);</script>';
$dArr gets the value of volunteerDist. echo $dArr prints the value 4.1,9.4,2.3,4.7,9.1,14.7,3.2,7.1,4.1,0.5
I wanted to split this into elements in an Array so I used:
$dArr = preg_split("/,/", $dArr);
But $dArr is an array with only one element, which is all of the values.. meaning no preg_split took place. I also tried
$temp=explode(",", $dArr);
but still did not work.
I tried to paste the value (i.e. the numbers above) and preg_split it directly
$temp=explode(",", '4.1,9.4,2.3,4.7,9.1,14.7,3.2,7.1,4.1,0.5');
and it was successful. What is wrong? TAKE NOTE that when I echo $dArr, the numbers are printed.
You are mixing server side and client side code and logic here.
You see the “correct” output because of the
document.writemethod that writes the output to the browser.You can’t interact directly with client side variable in a server side code.
You will have to either use AJAX to send the client side value to the server, or parse it in the client side:
Edit: although I’m not PHP developer, reading some other posts gave me enough sample codes.
To send the value of the client side
volunteerDistvariable to PHP and save to database, first of all include jQuery in your code:Now have this somewhere after the variable is defined and created:
And to read this data and add to database:
Of course, you need to change the page name, table name and field name to your real values.