user836910
user836910

Reputation: 484

submit form without page refresh

is it possible to submit this php form without refreshing the page. how would i go about setting a jquery code that can do this.

<?php
   if(!isset($_POST['sthis']))
    {
?>  
<div id="show">
<form method="post" action="">
something<input name="sthis" type="text" id="sthis" />
<input name="s" type="submit" value="submit" id="submit" />
</form>
</div>
<?php
    }
     else
  {
    if(isset($_POST['sthis']))
    $sthis = $_POST['sthis'];
    if(empty($sthis)){echo 'put something in this box';}
    else echo 'ready';
  }

?>

Upvotes: 0

Views: 1650

Answers (2)

jayellos
jayellos

Reputation: 691

This is how to use jQuery:

HTML

<form id="frmSample">
...
</form>

<div id="divResult"></div>

JQuery

$(function(){
  $("#frmSample").submit(function(){
    //some code here

    $("#divResult").load("yourPhpCode.php", {variableName: value, variableName1: value});
  });
});

Check out the documentation.

Upvotes: 1

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100205

You will need ajax for that:

$.ajax({
  type: "POST",
  url: "your url here",
  data: "sthis=someValue",
  success: function(response) {
    alert(response);
  }
});

Hope it helps

Upvotes: 1

Related Questions