2014-03-03 27 views
1

我所擁有的是php在客戶端簽署表單後更新數據庫字段。這可以工作,但我希望它在確認用戶通過單擊「確定」進行簽名後重定向到新頁面。他們點擊取消,它會讓他們在同一頁面上。在php/mysql數據庫更新後彈出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> 
<?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函數。

我很感激您對此有任何建議或意見。

+0

PHP在服務器上執行。 Javascript在客戶端執行。你想要的是不可能的。您可以**不** **服務器端PHP代碼觸發客戶端警報,然後等待客戶端進行響應。 –

+0

用戶提交表單後,是否有重定向回頁面?我非常樂於接受建議。 – tashworth19191

回答

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

+0

這幫我找到了一個解決方案,我發佈在底部。 – tashworth19191

+0

好的,太好了。我很高興能夠提供幫助。 – Anonymous

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

這會在點擊提交按鈕之前加載確認。 – tashworth19191

+0

@ user3376610我爲您的單頁解決方案更新了我的答案。這個想法是讓你的SQL事務完成時,window.onload的內容依賴於一個變量集。當然,像其他人提出的多頁面解決方案會更清晰。 – user2793390

0

你可以通過AJAX發佈形式,並且在你的PHP可以返回的JavaScript的響應。那是你可以使用回調來觸發新窗口。

繼承人jQuery的例子:

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

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

}); 
+0

對不起,我沒有跟蹤這個。 – tashworth19191

0

我相信你正在使用相同的頁面進行登陸和更新。你需要的是更新你的回聲來添加onload。

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

希望這是你在找什麼。

+0

這會在單擊提交按鈕之前加載確認。 – tashworth19191

+0

我打算從代碼片段中添加回顯。如果(isset($ _ POST ['submit'])){.....},你只有1個,它爲什麼會默認運行? – Ducane

0

嘗試:

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

這部分腳本已經正常工作,試圖弄清楚人員如何在提交表單之後如何執行所有php,然後用戶將看到彈出的確認窗口。 – tashworth19191