javascript
  • php
  • html
  • css
  • 2015-10-29 301 views 1 likes 
    1

    我正在使用Javascript創建一個PHP頁面。我創建了一個HTML表單,並將其稱爲代碼的PHP部分。當我顯示JSON數組時,我可以看到顯示的元素一毫秒,然後頁面刷新到初始頁面。代碼有效,但我不想自動刷新。我怎麼做?Html頁面自動刷新

    HTML:

    <div data-role="collapsible"> 
        <h1>ADD INVNTORY</h1> 
        <form action='<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>' method="post"> 
        <fieldset> 
         <legend> 
         <span class="number">1</span> 
         Your basic info 
         </legend> 
         <label for="name">Name:</label> 
         <input type="text" id="name" name="product_name"/> 
         <label for="serial">Serial number:</label> 
         <input type="text" id="serial" name="serial"/> 
         <label for="amount">Amount:</label> 
         <input type="number" id="amount" name="amount"/> 
         <input type="submit" name="submit1" value="Submit"/> 
        </fieldset> 
        </form> 
    </div> 
    <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post"> 
        <div data-role="collapsible"> 
        <h1>VIEW ALL</h1> 
        <fieldset> 
         <legend> 
         <span class="number">1</span> 
         Current Inventory list 
         </legend> 
         <input type="submit" name="submit2" value="Show all"> 
         <div data-role="popup" id="myPopup" class="ui-content"> 
         <h1 id="here"></h1> 
         </div> 
        </fieldset> 
        </div> 
        <div data-role="collapsible"> 
        <h1>VIEW INVENTORY DETAILS(GRAPH)</h1> 
        <fieldset></fieldset> 
        </div> 
        <button type="submit">Sign Up</button> 
    </form> 
    

    PHP:

    <?php 
        if (isset($_POST['submit1'])) { 
        $nname = ""; 
        $sserial = ""; 
        $aamount = ""; 
        if ($_SERVER["REQUEST_METHOD"] == "POST") { 
         if (empty($_POST["product_name"])) { 
         { 
          echo "Name is required"; 
          break; 
         } 
         } else { 
         $nname = test_input($_POST["product_name"]); 
         } 
         if (empty($_POST["serial"])) { 
         { 
          echo "Serial code is required"; 
          break; 
         } 
         } else { 
         $sserial = test_input($_POST["serial"]); 
         } 
         if (empty($_POST["amount"])) { 
         { 
          echo "Amount is required"; 
          break; 
         } 
         } else { 
         $aamount = test_input($_POST["amount"]); 
         } 
         insertdata($nname,$sserial,$aamount); 
        } 
        } 
        function test_input($data) { 
        return $data; 
        } 
        function insertdata($nname,$sserial,$aamount) { 
        $servername = "localhost"; 
        $username = "root"; 
        $password = ""; 
        $dbname = "mydb"; 
        $conn = new mysqli($servername, $username, $password, $dbname); 
        if ($conn->connect_error) { 
         die("Connection failed: " . $conn->connect_error); 
        } 
        $sql = "INSERT INTO inventory_list (name, serialno, amount) VALUES ('$nname', '$sserial', '$aamount')"; 
        if ($conn->query($sql) === TRUE) { 
         echo "New record created successfully"; 
        } else { 
         echo "Error: " . $sql . "<br>" . $conn->error; 
        } 
        $conn->close(); 
        } 
        if (isset($_POST['submit2'])) { 
        $servername = "localhost"; 
        $username = "root"; 
        $password = ""; 
        $dbname = "mydb"; 
        $conn = new mysqli($servername, $username, $password, $dbname); 
        if ($conn->connect_error) { 
         die("Connection failed: " . $conn->connect_error); 
        } 
        $sql = "SELECT * from inventory_list"; 
        $result = $conn->query($sql); 
        $return_arr=array(); 
        if ($result->num_rows > 0) { 
         // output data of each row 
         while ($row=$result->fetch_assoc()) { 
         $row_array['name'] = $row['name']; 
         $row_array['serialno'] = $row['serialno']; 
         $row_array['amount'] = $row['amount']; 
         array_push($return_arr, $row_array); 
         }  
        } else { 
         echo "0 results"; 
        } 
        $conn->close(); 
        } 
    ?> 
    

    ^^ PHP的一部分,

    的Javascript:

    var arr = <?php echo json_encode($return_arr);?>, out = "<table>"; 
    
    for (var i = 0; i < arr.length; i++) { 
        out += "<tr><td>" + 
        arr[i].name + 
        "</td><td>" + 
        arr[i].serialno + 
        "</td><td>" + 
        arr[i].amount + 
        "</td></tr>"; 
    } 
    
    out += "</table>"; 
    document.querySelector("#myPopup #here").innerHTML = out; 
    
    +2

    有上面沒有代碼可以刷新頁面 –

    +1

    你的地方trigging表單提交您所提供的代碼之外? –

    +0

    正如@Dagon所說,這裏沒有任何東西可以引起你所描述的行爲。 –

    回答

    0

    取決於你如何綁定使用jQuery形式,你可以返回fal本身,以防止從形式提交/清爽或使用的preventDefault()

    http://api.jquery.com/event.preventDefault/

    2

    自動刷新是從使用輸入類型=「提交」。按下按鈕時,類型爲「submit」的按鈕或輸入將自動刷新。

    要解決此問題,請將輸入的類型更改爲「按鈕」或單擊按鈕時使用jQuery preventDefault函數。

    爲的preventDefault函數的文檔: http://api.jquery.com/event.preventDefault/

    相關問題