2014-01-15 60 views
0

我在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... 

?> 

所以第一页包含表单按钮删除,当我按下它传递,我想页面值deletepost.php成功(我使用echo来查看它,就像你看到的那样)。 我决定在第一页中为我的表单使用JavaScript Lightbox。我已经改变了我的代码是:

<?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。任何想法,为什么这可能happend?

+1

从这一行删除onclick事件 – Kishorevarma

+0

我还没有做任何事 – user2491321

+0

关闭第一个<?php标记与?>在下一行并使用如下> Kishorevarma

回答

1

我没有看到JavaScript上的提交按钮。您可以在<a><button>上执行此操作,并且您应该参考表单名称。

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

... 


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

调用submit()只会不做任何事,因为没有这样的内置方法。您可以aliase的闯民宅的包装方法

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) 
+0

我只是使用一个简单的JavaScript灯箱怎么可能不允许我将var传递给下一页? – user2491321

+0

你需要哪个变种? –

+0

我想将$ comment_id传递给下一页,并且在使用href javascript的时候,灯箱不通过它 – user2491321

0

我不认为你需要onClick财产为您提交输入。特别是如果你实际上没有在你的JS的任何地方定义一个submit方法。设置该属性可能足以阻止表单的默认提交行为,并会阻止您的php运行。