0

I am building an array in javascript, using JSON.stringify() on it, and then setting a hidden input field's value attribute to the resulting string.

function setSelectedArray(){

        var ticketInfo = new Array();   //creates new array for later
        var num=0;                      //num accumulates the number of the next row
        for(var i=0;i<hlRows.length;i++){
            if(hlRows[i]){  //hlRows is an array with boolean values telling me
                            //which dataArray values to set ticketInfo to
                ticketInfo[num] = dataArray[i];
                //dataArray is a pre-defined array and contains a large amount of data
                //ticketInfo is a smaller array that is gathering all data specified from hlRows
                num++;
            }
        }
        //line below sets 'tix'(hidden input field).value to the JSON version of ticketInfo
        document.getElementsByName('tix').value = (JSON.stringify(ticketInfo));

        //At this point, my alert below displays everything I want to see.
        //It looks like correct JSON to me
        alert("The VALUE of 'tix' is: "+document.getElementsByName('tix').value);

        //this submits the form that the hidden field is inside of.
        //it wouldn't go to the next page(which it does) without
        //this calling the form's action attribute correctly (i think..)
        document.forms["hiddenForm"].submit();
    }

You can read my comments if you need more description. Here is the HTML part of the process:

<!-- This button, when clicked, calls the setSelectedArray() function -->
<input class="button" type="button" id="btnContact" value="Select Tickets" onclick="setSelectedArray()" style="width:20em;float:right;"/>

<form id="hiddenForm" action="email.php" method="post">
    <input type="hidden" name="tix" value=""/> <!-- I tried this with no value specified too -->
</form>

I don't know how many different things I tried in email.php to just get any sort of output. I tried echos and print and print_r and print from loops and printing values inside the "array", but nothing seemed to work. Here is where email.php is at right now:

<?php
    $jsArray = json_decode($_POST['tix']);
    var_dump($jsArray);
?>

The only output I get from email.php is "NULL"(which it wasn't 2 seconds earlier in the alert) or "Array". Otherwise, it says nothing and I just get an empty screen. What am I doing wrong here? Do I need to use AJAX to post the data? I feel like that isn't right.

6
  • Suggestion: put a debug call to "phpinfo()" in your email.php and see what's being sent.
    – paulsm4
    Commented Jun 13, 2012 at 18:39
  • Try using an ID attribute on the hidden field. <input type="hidden" name="tix" id="tix" value=""/> Commented Jun 13, 2012 at 18:42
  • 1
    comment out the form submission and use your browser dev tools to inspect the hidden element to see if its value has changed
    – Lizard
    Commented Jun 13, 2012 at 18:43
  • It says it is receiving "no value".
    – cNovak
    Commented Jun 13, 2012 at 18:43
  • @Lizard Do you mean using inspect element(i'm using chrome)? Where do I see the value of the text field in that?
    – cNovak
    Commented Jun 13, 2012 at 18:47

1 Answer 1

5

You problem is that getElementsByName returns an array of elements.

document.getElementsByName('tix')[0].value = (JSON.stringify(ticketInfo));

alert("The VALUE of 'tix' is: "+document.getElementsByName('tix')[0].value);
2
  • 1
    I suggest using getElementById when even possible. Commented Jun 13, 2012 at 18:48
  • I tried using getElementById at first and the same thing happened. BUT I just tried it again and now it is letting my data through. I'm receiving it on the PHP page now.
    – cNovak
    Commented Jun 13, 2012 at 18:51

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.