简体   繁体   English

使用javascript后的表单提交

[英]form submition after using javascript

I have two pages in php. 我在php中有两个页面。 First page is the following: 第一页如下:

<?php

// --- please consider that my form is inside a while statement...


<form action='deletepost.php' method='post' >
  <input type='hidden' name='var' value='$comment_id;'>
  <input type='submit' value='delete' >
</form>

?>

and page deletepost.php is the following: 页面deletepost.php如下:

<?php

    $comment = mysql_real_escape_string($_POST['var']);
    echo" $comment ";

    // --- then starts successfully the delete process I have created...

?>

So first page includes a form button delete that when I press it passes the value that I want to page deletepost.php successfully (I use echo to see it as you can see). 因此,第一页包含一个表单按钮delete,当我按下它时,它将传递我要成功分页deletepost.php的值(如您所见,我使用echo来查看它)。 I have decided to use a javascript lightbox for my form in first page. 我决定在首页中的表单中使用javascript灯箱。 I have changed my code to that: 我将代码更改为:

<?php

<a href = javascript:void(0) onclick = document.getElementById('light').style.display='block';document.getElementById('fade').style.display='block'><h2><font color=green size=3>Delete All</font></h2></a>
<div id=light class=white_content>


<form action='deletepost.php' method='post' >
  <input type='hidden' name='var' value='$comment_id'>
  <input type='submit' value='delete' onClick=submit(); >
</form>


<a href=javascript:void(0) onclick=document.getElementById('light').style.display='none';document.getElementById('fade').style.display='none'><button style='margin-left:250px; width:95px;'>Cancel</button></a>
</div>
<div id=fade class=black_overlay></div>  

the problem is that after using javascript, values are not passed to deletepost.php .Any idea why this might happend? 问题是使用javascript后,值未传递到deletepost.php。任何想法为什么会发生这种情况?

I see no point of having javascript over a submit button. 我认为在提交按钮上没有JavaScript是没有意义的。 You either do it on <a> or <button> , and you should refer to the form name. 您可以在<a><button> ,并且应参考表单名称。

<form name='form1' action='deletepost.php' method='post'>

...


<button onclick='document.form1.submit()'>delete</button>

Calling submit() only will do nothing, because there is no such built in alone method. 仅调用submit()不会执行任何操作,因为没有单独的内置方法。 You can aliase the refering in a wrapper method 您可以在包装方法中为引用添加别名

function submit() {
    document.form1.submit();
}

Here are my tests: 这是我的测试:

test1.php: test1.php:

<?php $comment_id = 1 ?>
<?php while($comment_id < 10): ?>
    <a href="javascript:void(0)" onclick="document.getElementById('light<?=$comment_id;?>').style.display='block';document.getElementById('fade<?=$comment_id;?>').style.display='block'"><h2><font color=green size=3>Delete All</font></h2></a>
    <div id="light<?=$comment_id;?>" class="white_content" style="display: none">


    <form action="test2.php" method="post" name="form1">
      <input type="hidden" name="var" value="<?=$comment_id;?>" />
      <button onClick="document.form1.submit();">Delete</button>
    </form>


    <a href="javascript:void(0)" onclick="document.getElementById('light<?=$comment_id;?>').style.display='none';document.getElementById('fade<?=$comment_id;?>').style.display='none'"><button style="margin-left:250px; width:95px;">Cancel</button></a>
    </div>
    <div id=fade class=black_overlay></div>
<?php $comment_id++; ?>
<?php endwhile;?>

It generates me 9 comment lightboxes. 它产生了9个评论灯箱。 On fifth one I click on Delete all it expands the button Delete . 在第五张上,我单击Delete all它会展开Delete按钮。 Then I click the button and it redirects me to test2.php Where I have 然后,我单击按钮,它会将我重定向到test2.php

<?php var_dump($_POST); ?>

And the output in the webpage is: 网页中的输出为:

array (size=1)
  'var' => string '5' (length=1)

I don't think you need the onClick property for your submit input. 我认为您不需要onClick属性来提交输入。 ESPECIALLY if you don't actually have a submit method defined anywhere in your JS. 特别是如果你实际上并没有submit你的任何地方JS定义的方法。 Setting that attribute could be enough to stop the default submit behavior of the form and will stop your php from running. 设置该属性可能足以停止表单的默认提交行为,并且将阻止您的php运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM