使用“自动提交”发布值无效

时间:2014-01-11 06:38:59

标签: javascript php

我有一个来自另一个页面的帖子值并发布到index2.php。我想使用自动提交脚本再次将该值发布到autosubmit.php。但为什么不工作?

这是我的代码

index2.php

<?php
$mysqli = new mysqli("localhost", "root", "", "app");
if (isset($_POST['submit'])) {
$drop=$_POST['drop'];
$tier_two=$_POST['tier_two'];

echo'
<form method="post" action="autosubmitform.php" id="dateForm" name="dateForm">
<input name="drop" type="" value="'.$drop.'" style="background-color:blue;"><input name="tier_two" type="" value="'.$tier_two.'">
<input type="submit" name="editsubmit">
</form>';

echo'<script type="text/javascript">
    document.getElementById("dateForm").submit(); // SUBMIT FORM
</script>';

}
?>

autosubmitform.php

<?php
$mysqli = new mysqli("localhost", "root", "", "app");
if (isset($_POST['editsubmit'])) {
$drop=$_POST['drop'];
$tier_two=$_POST['tier_two'];

echo $drop; echo $tier_two;
}
echo'
<input name="drop" type="" value="'.$drop.'" style="background-color:red;"><input name="tier_two" type="" value="'.$tier_two.'">
<input type="submit" name="editsubmit">';
?>

3 个答案:

答案 0 :(得分:1)

代码中的更改

   /*
    if you submit your form through javascript your button value is not submited
    so you have to click that by javascript to submit it value 
    */

 <script type="text/javascript">

   document.getElementsByName("editsubmit")[0].click(); // SUBMIT FORM
 </script>

同样在php中移动你的echo代码,因为如果它不在if里面而不是你的变量$drop$tier_twoif以外的echo语句中保持不定义

if (isset($_POST['editsubmit'])) {
    $drop=$_POST['drop'];
    $tier_two=$_POST['tier_two'];

    echo $drop; 
    echo $tier_two;
    echo'
    <input name="drop" type="text" value="'.$drop.'" style="background-color:red;"><input name="tier_two" type="text" value="'.$tier_two.'">
    <input type="submit" name="editsubmit">';
}

以上PHP代码的替代解决方案

$drop = ''; 
$tier_two = ''; 
if (isset($_POST['editsubmit'])) {
    $drop=$_POST['drop'];
    $tier_two=$_POST['tier_two'];

    echo $drop; 
    echo $tier_two;

}
echo'
    <input name="drop" type="text" value="'.$drop.'" style="background-color:red;"><input name="tier_two" type="text" value="'.$tier_two.'">
    <input type="submit" name="editsubmit">';

答案 1 :(得分:0)

用户此脚本

window.onload = function(){
  document.forms['dateForm'].submit()

}

如下

echo"<script type="text/javascript">
    window.onload = function(){
      document.forms['dateForm'].submit()

    }
</script>";

答案 2 :(得分:0)

我讨厌不能发表评论。唯一的问题是,有些人可能会被抓到一半。

如果有人关闭了javascript,那么他们会被原始数据卡在此页面上。还有其他方法可以写出来吗?

您也可以只传递url中的值(GET而不是POST)并对值进行编码(如果需要加密)。