Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Apr 2007
    Posts
    123
    Thanks
    1
    Thanked 0 Times in 0 Posts

    passing javascript array and var to php

    I'm submitting data from a form to another page. Using the onsubmit event I'm generating a javascript array and var which I want to access both the array and var using $_POST. The array and var I'm assuming need to be passed as POST parameters. If so, in what format exactly, as they are not values within the submitting document?

  • #2
    Regular Coder
    Join Date
    Mar 2011
    Posts
    148
    Thanks
    0
    Thanked 20 Times in 20 Posts
    Hy
    You can pass the Array easily with JSON format:
    var aray = ["elm1", "elm2", ...];
    Then, in php you can use json_decode() function.

    About vars, I think you can put them in an JSON object
    var vars = {"var1":"val1", "var2":"val2", ...};
    Then, in php
    Code:
    $vars = json_decode($_POST['vars']);
    foreach($vars as $k => $v) {
      $$k = $v;
    }

  • #3
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    Another option is to create dynamically hidden textbox elements in the onsubmit function and attach them to the form before the form is submitted. The hidden textboxes can contain the values in the var and the array. Make sure the name of the hidden textboxes for the array values have the same name and have [] appended to the name so they are sent as an array to the php script.

  • #4
    Regular Coder
    Join Date
    Apr 2007
    Posts
    123
    Thanks
    1
    Thanked 0 Times in 0 Posts
    My tabular form has rows
    Code:
    <tr>
    <td><input type'checkbox' name='checkbox' value'x-y-z'></td>
    <td>x</td>
    <td>y</td>
    <td>z</td>
    <td><textarea name='text'>Text goes here</textarea></td>
    </tr>
    I originally had the textarea disabled to prevent users from editing the text. This however was preventing $_POST from reading the text. So now the textarea is not disabled, the text is now visible to $_POST, but the user can edit the text which I don't want. If there is no way of displaying and posting the text without preventing the user from editing the text, I might have to just display the text like x, y and z, and collect the text from a database instead. Unless there is any other solution?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •