2014-12-02 101 views
1

我想根據用戶選擇的任何項目將一些變量發佈到PHP腳本,然後將選擇加載到當前頁面而不重定向用戶。但是,當我單擊按鈕提交數據時,返回false不會觸發,並且我將重定向到表單中指定的操作。有人能告訴我我的代碼中的問題在哪裏嗎?JQuery .post函數沒有正確執行

<!DOCTYPE html> 
    <html> 
    <head> 
    <title> Today's Clients</title> 

    <link href="../_css/jquery-ui.min.css"> 
    <script src="../_js/jquery.min.js"></script> 
    <script src="../_js/jquery-ui.min.js"></script> 

    <script> 
    $(document).ready(function(){ 
     $("#clientSubmit").submit(function(event) { 
      var clientInformation = $(this).serialize(); 
      $.post('IRCpopulatecheckin.php',clientinformation,clientForm); 
      function clientForm(data) { 
       if (data!='') { 
        $('#clientform').load("IRCpopulatecheckin.php"); 
       } else { 
        alert("your data returned nothing!!! rewrite the code..."); 
       } 
      } // end clientForm 
     return false; 
     }); // end .submit 
    }); // end ready 

    </script> 

    <style> 

    /* css to style and remove everything but text */ 
     #hiddenInput { 
        position :relative; 
        width  :0px; 
        height  :8px; 
        top   :-40px; 
        left  :-230px;260 
        } 
     input[name="dailyClient"] { 
        background-color: white; 
        border: none; 
        font-weight :bold; 
        font-family :sans-serif; 
        font-size: 15px; 
        color: black; 
        cursor: default; 
        line-height: normal; 
        padding: 6px; 
        text-align: center; 
        text-shadow: none; 
        white-space: pre; 
        } 

     input[name="dailyClient"]:hover { 
        color: blue; 
        } 
    </style>     
    <body> 
    <div id="clientform"></div> 

    <?php 

    ini_set('display_errors',1); error_reporting(E_ALL); 

    if(isset($_POST['DATE'])) { 
     $DATE = $_POST['DATE']; 
     }else{ 
      $DATE = date('Y-m-d'); 
      } 

    require_once 'IRCconfig.php'; 

    $connection = new mysqli($db_hostname, $db_username, $db_password, $db_database); 
     if ($connection->connect_error) die($connection->connect_error); 

    $query = "SELECT * FROM CLIENT_CHECKIN WHERE DATE>='$DATE' ORDER BY F_NAME ASC"; 
     $result = $connection->query($query); 

    if (!$result) die ("Database access failed: " . $connection->error); 

    $rows = $result->num_rows; 

    for ($j = 0 ; $j < $rows ; ++$j) 
     { 
      $result->data_seek($j); 
      $row = $result->fetch_array(MYSQLI_NUM); 

      echo <<<_END 
      <pre> 
       <div id="hiddenInput"><div style="display:none"> 
       <form id="clientSubmit" action="IRCpopulatecheckin.php" method="post"><input id="date" type="hidden" name="DATE" value="$row[0]"><input id="first" type="hidden" name="F_NAME" value="$row[1]"><input id="middle" type="hidden" name="M_NAME" value="$row[2]"><input id="last" type="hidden" name="L_NAME" value="$row[3]"></div> 
       <input type="submit" name="dailyClient" value="$row[1] $row[2] $row[3]"></form> 
       </pre> 
    _END; 
     } 

    ?> 
    </body> 
    </html> 

回答

1

不是return false使用event.preventDefault。請注意,你怎麼已有的參數在onsubmit

$(document).ready(function(){ 
    $("#clientSubmit").submit(function(event) { 
     event.preventDefault(); 
     var clientInformation = $(this).serialize(); 
     $.post('IRCpopulatecheckin.php',clientinformation,clientForm); 
     function clientForm(data) { 
      if (data!='') { 
       $('#clientform').load("IRCpopulatecheckin.php"); 
      } else { 
       alert("your data returned nothing!!! rewrite the code..."); 
      } 
     } // end clientForm 
    }); // end .submit 
}); 
+0

謝謝,這工作。我曾嘗試過使用preventDefault,但將其定位錯誤。 – dan5ermou5 2014-12-02 22:01:03

+0

@ dan5ermou5非常歡迎。 – Verhaeren 2014-12-02 22:04:49

0

這個問題似乎是與您創建具有相同clientSubmit ID多種形式的事實。 (所以最有可能的正確工作的第一種形式在頁面

標識的必須在頁面獨特。

如果您想將相同的功能應用於多個元素,則應該使用一個類。

因此改變你的形式<form class="clientSubmit" action=...

和你的腳本

$(document).ready(function(){ 
    $(".clientSubmit").submit(function(event) { 
     var self = $(this), 
      clientInformation = self.serialize(); 
     $.post('IRCpopulatecheckin.php',clientinformation,clientForm); 
     function clientForm(data) { 
      if (data!='') { 
       self.load("IRCpopulatecheckin.php"); 
      } else { 
       alert("your data returned nothing!!! rewrite the code..."); 
      } 
     } // end clientForm 
    return false; 
    }); // end .submit 
}); // end ready