2015-12-25 113 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