【问题标题】:Hidden form wont pass variable to php隐藏表单不会将变量传递给 php
【发布时间】:2012-04-17 18:59:38
【问题描述】:

我正在使用 js 将变量分配给隐藏表单的值。但是,当我尝试在 php 中访问它时,什么也没有出现。我知道 javascript 正在正确地将值添加到表单中,但是由于某种原因 php 无法得到它。代码如下:

<html>
<head>

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="textualizer.min.js"></script>

</head>

<style type="text/css">

#txtlzr{color:#585856; font-size:50px; width:1200px; height:100px;
margin-left:10%;
margin-top:80px;
font-family:"futura";
position: fixed;
}
</style>

<body>


<div id="txtlzr"></div>

<form name="kwoteForm" action="main.php" method="post"/>
<input type="hidden" id="kwoteString" name="kwoteString" value="bob" />
<input class="kwote" type="text" maxlength="40" id="kwote"placeholder="Enter a something here."/>
<input class="name" type="text" maxlength="17" id="name" placeholder="Enter your name."/>
<input class="post" type="submit" value="Add comment" onclick="add_comment();" />
</form>


<script language="javascript">



var COMMENTS_FOR_DISPLAY = new Array('OOOOOOO ITS WORKING : NICK');

// Adds a new comment, name pair to the Array feeding textualizer.
function add_comment() {
  // Retrieve values and add them to Array.
  var new_comment = $('#kwote').val();
  var new_name = $('#name').val();

   COMMENTS_FOR_DISPLAY.push(new_comment + ': ' + new_name);


  // Reset <input> fields.
  $('#kwote').val('');
  $('#name').val('');

  //Take comment array and put it into a string.
  var arrayAsString = JSON.stringify(COMMENTS_FOR_DISPLAY);

  //Assign value of the string to a hidden form.
  document.getElementById("kwoteString").value = arrayAsString; 

  //checks output.
  alert(document.kwoteForm.kwoteString.value);

  }


$(document).ready(function() {
  var txt = $('#txtlzr');  // The container in which to render the list

  var options = {
    duration: 5,          // Time (ms) each blurb will remain on screen
    rearrangeDuration: 5, // Time a character takes to reach its position
    effect: 'random',     // Animation effect the characters use to appear
    centered: true        // Centers the text relative to its container
  }

  txt.textualizer(COMMENTS_FOR_DISPLAY); // textualize it!
  txt.textualizer('start'); // start
});



</script>


</body>
</html>

PHP:

<?php
$saveKwote = $_POST["kwoteString"];
echo saveKwote;
?>

由于某种原因没有回显!

【问题讨论】:

  • 我认为你的 PHP 代码中没有那个错字? echo saveKwote; 应该是 echo $saveKwote;
  • error_reporting(E_ALL) 是你的朋友!!!
  • 这是一个错字!!!,感谢 GAZILION。太尴尬了。

标签: php javascript forms


【解决方案1】:

你需要改变:

echo saveKwote;

echo $saveKwote;

没有美元符号 ($),PHP 会尝试输出它认为不存在的常量或函数,因此输出为空。

【讨论】:

  • @Gazillion 在第一条评论中已经指出
  • @Lion 直到我发布答案后才看到评论。
猜你喜欢
  • 1970-01-01
  • 2023-03-03
  • 2016-05-23
  • 1970-01-01
  • 1970-01-01
  • 2016-05-27
  • 2011-06-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多