2015-12-25 109 views
0

我想實現,當我點擊提交按鈕,它不會刷新頁面。 我的提交按鈕只會在提交時調用一組代碼。 但是,當它被提交時,它會刷新頁面,我的變量會丟失它的值。不要刷新頁面提交PHP

我知道這可以用java-script或ajax實現,但這是我沒有知識的地方。需要開始學習它。 我試圖用JavaScript的一些其他建議,但它並不適用於我,因爲JavaScript代碼是爲大多數高級表單構建的,而不僅僅是一個提交按鈕。

我的按鈕:

<form action='{$_SERVER['PHP_SELF']}' method='post'> 
     <input type='submit' class='submit' name='confirm_points' value='Confirm Points' /> 
    </form>"; 

我的代碼執行時提交按鈕被按下:

if(isset(&_POST['confirm_points'])) { 
    // my code is here 
} 

感謝所有幫助

+0

馬塞爾,你k現在如何使用jQuery? –

+0

你必須爲你想要的AJAX。用@Makville提到的jQuery很容易。 –

+0

@Makville - Jquery只是執行,因爲我現在正在學習它從頭做一些事情。 – Marcel

回答

0

這裏有一個簡單的例子:

<?php 
if (isset($_POST['points'])) { 
    echo "I'm AJAX and I have " . $_POST['points']; 
    exit(); 
} 
?> 

<html> 
<head> 
    <script src="https://code.jquery.com/jquery-1.11.3.min.js"></script> 
    <script> 
     $(document).ready(function(){ 
      $('form').submit(function(event){ 
       event.preventDefault(); 
       $.ajax({ 
        type: $(this).attr('method'), 
        data: $('form').serialize(), 
        success: function(data) { 
         $('#result').html(data); 
        } 
       }); 
      }); 
     }); 
    </script> 
</head> 
<body> 

<form method="POST"> 
    <input type="text" name="points" placeholder="Some data" value="25 points"/> 
    <input type='submit' class='submit' name='confirm_points' value='Confirm Points' /> 
</form> 

<div id="result"></div> 

</body> 
</html> 
+1

謝謝Switcher ...現在我有幾個提交沒有刷新的選項... – Marcel

+0

不客氣! –

0

與應阿賈克斯

<html> 
    <head></head> 
    <body> 
     <form id="my-form"> 
      <input type="text" name="username" /> <br/> 
      <input type="submit" value="Save" /> 
     </form> 
     <script src="https://code.jquery.com/jquery-1.11.3.min.js"></script> 
     <script> 
      $(function() { 
       $('#my-form').submit(function (e) { 
        var $data = $(this).serialize(); 
        var $url = 'path_to_processing_script'; 
        $.post($url, $data, function ($response, $status) { 
         if ($status == 'success') { 
          alert($response); 
         } 
        }); 
        e.preventDefault(); 
       }); 
      }); 
     </script> 
    </body> 
</html> 

處理腳本

<?php 
    var_dump($_POST); 
?> 
+1

謝謝馬克維爾...它的工作原理...我也試過切換器代碼,它也可以工作... – Marcel

+0

太棒了!很高興有幫助。乾杯 –

0

首先要停止刷新提交表單,表單頭更改爲:

<form action='{$_SERVER['PHP_SELF']}' method='post' onsubmit="javascript:return false;" id="myform"> 

接下來,使用Ajax提交表單:

<script src="path/to/jquery.js"></script> 
<script type="text/javascript"> 
$(document).ready(function(){ 
    $("#myform").submit(function(){ 
     var elem1=$("#elem1").val(); //get values of inputs for validation 
     if(elem1.length<5){alert("Length too short!");}//Sample Validation 
     else{ 
      $.ajax({ 
       url:"verify.php",//link to your submission page 
       type:"post", //change to your needs 
       data:{"elem1":elem1}, //add all variable 
       success:function(response){alert(response);} //Manage Response 
      }); 
    }); 
}); 
</script> 
+1

感謝Manikiran ...我已經嘗試了所有的代碼人,他們工作正常... – Marcel