1

What I have is php that updates the database field after a client signs a form. This works, but I want it to redirect to a new page after it is confirmed that the user signed it by clicking OK. It they click CANCEL it will leave them on the same page.

<?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> 

My problem is the javascript function does not execute after the php updtates the database.

I appreciate any advice or comments you have about this.

Armel Larcier
  • 15,747
  • 7
  • 68
  • 89
  • PHP executes on the server. Javascript executes on the client. What you want is impossible. You can **NOT** have server-side PHP code trigger a client-side alert and then WAIT for the client to respond. – Marc B Mar 03 '14 at 21:26
  • Is there anyway to redirect to back to a page after the user submits the form. I am very open to suggestions. – tashworth19191 Mar 03 '14 at 21:37

5 Answers5

2

The problem is that you are using separate <script> tags and calling the function before it is defined. These two together do not work well. Also, I'm pretty sure that <script type="text/x-javascript"> does not work anyway since it's outdated and that you want <script type="text/javascript">

You can do the following:

Move function up and fix 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: Fiddle

Community
  • 1
  • 1
Anonymous
  • 11,748
  • 6
  • 35
  • 57
1

You could post the form via ajax and in your php you can return a response to the javascript. That was you could use a callback to fire the new window.

Heres a jquery example:

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

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

});
Cjmarkham
  • 9,484
  • 5
  • 48
  • 81
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> 
user2793390
  • 741
  • 7
  • 29
  • This loads the confirmation before the submit button is clicked. – tashworth19191 Mar 03 '14 at 21:36
  • @user3376610 I updated my answer for your single page solution. The idea is to make the content of window.onload dependent on a variable set when your SQL transaction completes. Of course, multiple page solutions like others suggested would be much cleaner. – user2793390 Mar 03 '14 at 21:51
0

I believe you are using same page for landing and updating. What you need is to update your echo to add an onload.

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

Hope this what you are looking for.

Ducane
  • 129
  • 1
  • 7
0

Try:

if (r==true) {
    window.open("http://smartpathrealty.com/smart-path-member-page/");
}
else {
    window.location = "PAGE THAT YOU WANT"; 
}
Jeff B
  • 8,572
  • 17
  • 61
  • 140
Adam
  • 1,684
  • 14
  • 18
  • This part of script already works correctly, just trying to figure out how after the person submits the form and all the php is executed next the user will see a pop up confirmation window. – tashworth19191 Mar 03 '14 at 21:43