[英]form submition after using javascript
我在php中有兩個頁面。 第一頁如下:
<?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>
?>
頁面deletepost.php如下:
<?php
$comment = mysql_real_escape_string($_POST['var']);
echo" $comment ";
// --- then starts successfully the delete process I have created...
?>
因此,第一頁包含一個表單按鈕delete,當我按下它時,它將傳遞我要成功分頁deletepost.php的值(如您所見,我使用echo來查看它)。 我決定在首頁中的表單中使用javascript燈箱。 我將代碼更改為:
<?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>
問題是使用javascript后,值未傳遞到deletepost.php。任何想法為什么會發生這種情況?
我認為在提交按鈕上沒有JavaScript是沒有意義的。 您可以在<a>
或<button>
,並且應參考表單名稱。
<form name='form1' action='deletepost.php' method='post'>
...
<button onclick='document.form1.submit()'>delete</button>
僅調用submit()
不會執行任何操作,因為沒有單獨的內置方法。 您可以在包裝方法中為引用添加別名
function submit() {
document.form1.submit();
}
這是我的測試:
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;?>
它產生了9個評論燈箱。 在第五張上,我單擊Delete all
它會展開Delete
按鈕。 然后,我單擊按鈕,它會將我重定向到test2.php
<?php var_dump($_POST); ?>
網頁中的輸出為:
array (size=1)
'var' => string '5' (length=1)
我認為您不需要onClick
屬性來提交輸入。 特別是如果你實際上並沒有submit
你的任何地方JS定義的方法。 設置該屬性可能足以停止表單的默認提交行為,並且將阻止您的php運行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.