gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 13:50:56 26 4
gpt4 key购买 nike

我正在 JavaScript 中构建一个数组,在其上使用 JSON.stringify(),然后将隐藏输入字段的 value 属性设置为结果字符串。

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();
}

如果您需要更多说明,可以阅读我的评论。以下是该过程的 HTML 部分:

<!-- 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>

我不知道我在 email.php 中尝试了多少种不同的方法来获得任何类型的输出。我尝试了 echo 、 print 、 print_r 、循环打印和打印“数组”内的值,但似乎没有任何效果。这是 email.php 现在的位置:

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

我从 email.php 得到的唯一输出是“NULL”(它不是早在警报中 2 秒)或“Array”。否则,它什么也没说,我只会得到一个空屏幕。我在这里做错了什么?我需要使用 AJAX 来发布数据吗?我觉得这样不对。

最佳答案

您的问题是 getElementsByName 返回一个元素数组。

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

alert("The VALUE of 'tix' is: "+document.getElementsByName('tix')[0].value);

关于php - 当我通过 $_POST 将隐藏输入发送到 PHP 时,我丢失了数据吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11021195/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com