最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to prevent from page refresh on submit button click - Stack Overflow

programmeradmin1浏览0评论

i have form with one input and one submit button.

<form method='POST' action='' enctype='multipart/form-data' id="form_search">
<input type='hidden' name="action" id="form_1" value='1' />
</span><input id="query" type="text" name="mol" value="">
<input type='submit' value='Search' name="Search" id="Search" />

on form submission form input data goes to php below code

if (isset($_POST['Search'])) {
$_SESSION["query"] = $_POST["mol"];
$_SESSION["action"] = $_POST["action"];
}

i want to avoid page refresh on form submission. i tried e.preventDefault() and return false; methods in my java script but not working(this methods helping me from page refresh but does not allowing me to send data to php code)

please help me out of this problem, please suggest working ajax code for this problem.

i have form with one input and one submit button.

<form method='POST' action='' enctype='multipart/form-data' id="form_search">
<input type='hidden' name="action" id="form_1" value='1' />
</span><input id="query" type="text" name="mol" value="">
<input type='submit' value='Search' name="Search" id="Search" />

on form submission form input data goes to php below code

if (isset($_POST['Search'])) {
$_SESSION["query"] = $_POST["mol"];
$_SESSION["action"] = $_POST["action"];
}

i want to avoid page refresh on form submission. i tried e.preventDefault() and return false; methods in my java script but not working(this methods helping me from page refresh but does not allowing me to send data to php code)

please help me out of this problem, please suggest working ajax code for this problem.

Share Improve this question edited Dec 30, 2014 at 4:58 sujit asked Dec 25, 2014 at 14:36 sujitsujit 111 gold badge1 silver badge3 bronze badges 3
  • You just have to prevent default form submiting by using return false or e.preventDefault(). And send your form info to PHP side through AJAX – user3154370 Commented Dec 25, 2014 at 15:01
  • Hi, @Hossein Sh could you please provide me ajax code to send form info to PHP side (as you mentioned). Please provide me working code. – sujit Commented Jan 12, 2015 at 11:39
  • Hi @sugit, Exactly I don't know what you mean. But as I understand you can gathers inputs and send them as JSON data, or serialize your form data and send them again as a JSON to your PHP file and access it with $_POST or $_GET – user3154370 Commented Jan 13, 2015 at 16:18
Add a ment  | 

3 Answers 3

Reset to default 5

Page refresh will delete you previous data so to reserve it you can use $.post() or $.ajax()

You can prevent page refreshing by adding one of these two things in event handler function

for pure js

 return false;

for jquery you can use

e.preventDefault(); // e is passed to handler

Your plete code will be something like

using $.post() in js

function checkfunction(obj){
$.post("your_url.php",$(obj).serialize(),function(data){
 alert("success");
 });
 return false;
 }

html

<input type='submit' onclick="return checkfunction(this)" />

or same effect with onsubmit

<form  onsubmit="return checkfunction(this)" method="post">

Without ajax you can simply add the checked attribute in PHP. So for example if your radio group has the name radio and one has value a, the other b:

<?php
$a_checked = $_POST['radio'] === 'a';
$b_checked = $_POST['radio'] === 'b';
?>

<input type="radio" name="radio" value="a"<?=($a_checked ? ' checked' : '')?>></input>
<input type="radio" name="radio" value="b"<?=($b_checked ? ' checked' : '')?>></input>

So when a user submits the form and you display it again, it will be like the user submitted it even the page refreshes.

<input type="radio" name="rbutton" id="r1">R1
<input type="radio" name="rbutton" id="r2">R2
<input type="button" id="go" value="SUBMIT" />
<div id="result"></div>

<script src="//ajax.googleapis./ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">

$(document).ready(function(){
    $('#go').click(function(){
        var val1 = $('input:radio[name=rbutton]:checked').val();
        var datastring = "partialName="+val1;
        $.ajax({
            url: "search.php",
            type: "POST",
            data: datastring,
            success: function(data)
            {
                $("#result").html(data);
            }
        });
    });
});
</script>
发布评论

评论列表(0)

  1. 暂无评论