php / mysql数据库更新后弹出javascript

时间:2014-03-03 21:25:05

标签: javascript php mysql

我拥有的是在客户签名表单后更新数据库字段的php。这可行,但我希望它在确认用户通过单击确定签名后重定向到新页面。他们点击取消它会将它们留在同一页面上。

<?php
    $username = 'joeblow';
    require_once ("/mypath/include/connnect_db.php");
?>

    <p>Please only submit this after above form is completely signed.</p>
    <form id="item_complete" method="post">
      <input name="submit" type="submit" form="item_complete"  value="Submit After Signing">
    </form>
<?php
    if(isset($_POST['submit'])) {  //if the submit button is clicked
        $complete = 'c';
        $query = "UPDATE mytbale SET mycolumn='c' WHERE username='$username'";
        mysqli_query($con,$query) or  die("Cannot Update");
        echo "<script> confirmFunction(); </script>";
    }
    require_once ("/mypath/include/disconnect_db.php");
?>
    <script type="text/x-javascript">
        function confirmFunction(){
            var r=confirm("Confirm that you have signed the form!");
            if (r==true){
              window.open("http://smartpathrealty.com/smart-path-member-page/");
              }
            else {
              }
            }
    </script> 

我的问题是php更新数据库后javascript函数没有执行。

我感谢你对此有任何建议或意见。

5 个答案:

答案 0 :(得分:2)

问题是您使用单独的<script>标记并在定义之前调用该函数。这两个在一起效果不好。此外,我非常确定<script type="text/x-javascript">无论如何都不起作用since it's outdated并且您想要<script type="text/javascript">

您可以执行以下操作:

向上移动功能并修复x-javascript

<?php
    $username = 'joeblow';
    require_once ("/mypath/include/connnect_db.php");
?>
<p>Please only submit this after above form is completely signed.</p>
<form id="item_complete" method="post">
    <input name="submit" type="submit" form="item_complete"  value="Submit After Signing">
</form>
<script type="text/javascript">
    function confirmFunction(){
        var r=confirm("Confirm that you have signed the form!");
        if (r==true){
          window.open("http://smartpathrealty.com/smart-path-member-page/");
          }
        else {
          }
        }
</script> 
<?php
if(isset($_POST['submit'])) {  //if the submit button is clicked
    $complete = 'c';
    $query = "UPDATE mytbale SET mycolumn='c' WHERE username='$username'";
    mysqli_query($con,$query) or  die("Cannot Update");
    echo "<script> confirmFunction(); </script>";
}
require_once ("/mypath/include/disconnect_db.php");
?>

小提琴 Fiddle

答案 1 :(得分:0)

<script type="text/javascript">
    function confirmFunction(){
        var r=confirm("Confirm that you have signed the form!");
        if (r==true){
          window.open("http://smartpathrealty.com/smart-path-member-page/");
          }
        else {
          }
        }
</script> 
<?php
if(isset($_POST['submit'])) {  //if the submit button is clicked
    $complete = 'c';
    $query = "UPDATE mytbale SET mycolumn='c' WHERE username='$username'";
    mysqli_query($con,$query) or  die("Cannot Update");
    echo "<script> confirmFunction(); </script>";
}
require_once ("/mypath/include/disconnect_db.php");
?>

<script type="text/javascript">


    window.onload = function(){
        <?php
            if(submitted){
                echo "confirmFunction();";
            }
        ?>
    }
</script> 

答案 2 :(得分:0)

您可以通过 ajax 发布表单,在您的php中,您可以返回对javascript的回复。那就是你可以使用回调来启动新窗口。

下面是一个jquery例子:

$.post('/myscript.php', {foo: 'bar'}).done(function (response) {

window.open(......);

});

答案 3 :(得分:0)

我相信您使用相同的页面进行登陆和更新。你需要的是更新你的回声以添加一个onload。

echo "<script> window.onload=confirmFunction(); </script>";

希望这是你想要的。

答案 4 :(得分:0)

尝试:

if (r==true) {
    window.open("http://smartpathrealty.com/smart-path-member-page/");
}
else {
    window.location = "PAGE THAT YOU WANT"; 
}