1

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.

11
  • First off, if you're only splitting on the , you should use explode() anyway. The performance loss of preg_split() is negligible, but if you're doing this is any sort of iteration, you may as well benefit from the brevity and speed of explode(). Commented Mar 15, 2012 at 11:39
  • Works fine for me codepad.org/ooeOHHz4 Commented Mar 15, 2012 at 11:40
  • 1
    No, echo $dArr prints the <script> tag, the browser then renders that as your list of numbers. View source on the page and you'll see what I mean. You can't use Javascript within PHP this way. Commented Mar 15, 2012 at 11:40
  • Im pretty sure your first line is going to fail .... the code to the right of = is executed on the front end ... have you checked $dArr is not empty ??? Commented Mar 15, 2012 at 11:41
  • Are you expecting that JavaScript code to run when the PHP is executing so you get the numbers? Commented Mar 15, 2012 at 11:42

1 Answer 1

5

You are mixing server side and client side code and logic here.

You see the "correct" output because of the document.write method 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:

<script type="text/javascript">
var dArr = volunteerDist.split(",");
for (var i = 0; i < dArr.length; i++)
   document.write(dArr[i] + "<br />");
</script>

Edit: although I'm not PHP developer, reading some other posts gave me enough sample codes.

To send the value of the client side volunteerDist variable to PHP and save to database, first of all include jQuery in your code:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

Now have this somewhere after the variable is defined and created:

<script type="text/javascript">
$.ajax({
    type: "POST",
    url: "yourpagehere.php",
    data: "volunteerDist=" + volunteerDist,
    dataType: "html"
});
</script>

And to read this data and add to database:

<?php
    $volunteerDist = $_POST['volunteerDist'];
    if(isset($volunteerDist)) {
        $dArr = preg_split("/,/", $volunteerDist);
        foreach($dArr as $volunteer) {
            $sql = "INSERT INTO TableName (fieldname) VALUES ('".$volunteer."')";
            mysql_query($sql) or die(mysql_error());
        }
    }
?>

Of course, you need to change the page name, table name and field name to your real values.

Sign up to request clarification or add additional context in comments.

17 Comments

var_dump($dArr) returned string(47) "4.1,9.4,2.3,4.7,9.1,14.7,3.2,7.1,4.1,0.5" so that means $dArr already got the data.. i need to use it in php because i need to insert them in my databse
Right. Count the character in <script>document.write(volunteerDist);</script> = exactly 47. When sent to the browser it will show the value in the client side variable volunteerDist. Change the string to: $dArr = '<script>alert(volunteerDist);</script>'; and maybe you will see what I'm trying to say all along.
To insert this to database you will have to use AJAX. Look for jQuery AJAX it's the most simple and friendly way - include the jQuery basic library then with one line you can send the value to your PHP code.
okay okay.. so how can I transfer the values into my php code for insertion in mysql?
Like I said, jQuery AJAX is the most simple way.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.