隐藏的形式不会将变量传递给php

时间:2012-04-17 18:59:39

标签: php javascript forms

我使用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;
?>

由于某种原因没有回应!

1 个答案:

答案 0 :(得分:1)

您需要更改:

echo saveKwote;

echo $saveKwote;

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