dongyou5271 2012-06-13 18:34
浏览 32
已采纳

当我通过$ _POST将它发送到PHP时,我丢失了隐藏输入的数据?

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.

  • 写回答

1条回答 默认 最新

  • download201401 2012-06-13 18:47
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度