ItShine
ItShine

Reputation: 349

submit form php without refresh page

I'm working on a PHP application i want to submit form without refresh page. Actually, i want my php code to be written on the same page as the one containing html and jquery code. In order to submit form using jquery i've written this code

$(document).ready(function(){

  $("#btn").click(function(){

var vname = $("#selectrefuser").val();
$.post("php-opt.php", //Required URL of the page on server
{   // Data Sending With Request To Server
selectrefuser:vname,

},
function(response,status){  // Required Callback Function
//alert("*----Received Data----*\n\nResponse : " + response+"\n\nStatus : " + status);//"response"  receives - whatever written in echo of above PHP script.

});
php_lat = <?php echo $resclient_alt; ?>;
  php_long = <?php echo $resclient_long; ?>;
  var chicago = new google.maps.LatLng(parseFloat(php_lat), parseFloat(php_long));
  addMarker(chicago);
  //return false;
 //e.preventDefault();
//$("#monbutton:hidden").trigger('click');
});


});

and my php code is :

<?php
$resclient_alt = 1;
$resclient_long = 1;
if(isset($_POST['selectrefuser'])){
$client = $_POST['selectrefuser'];
echo $client;
$client_valide = mysql_real_escape_string($client);
$dbprotect = mysql_connect("localhost", "root", "") ; 
$query_alt= "SELECT altitude FROM importation_client WHERE nom_client='$client_valide' ";
$query_resclient1_alt=mysql_query($query_alt, $dbprotect);
$row_ss_alt = mysql_fetch_row($query_resclient1_alt);
$resclient_alt = $row_ss_alt[0];
//echo $resclient_alt;
$query_gps= "SELECT longitude FROM importation_client WHERE nom_client='$client_valide' ";
$query_resclient1=mysql_query($query_gps, $dbprotect);
$row_ss_ad = mysql_fetch_row($query_resclient1);

    $resclient_long = $row_ss_ad[0];
}
 ?>

My form is as below

<form id="form1" name="form1" method="post"  >
  <label>
 <select name="selectrefuser" id="selectrefuser">

    <?php
    $array1_refuser = array();
    while (list($key,$value) = each($array_facture_client_refuser)) {

       $array1_refuser[$key] = $value;   
    ?>  
    <option value="0" selected="selected"></option>
    <option value="<?php echo $value["client"];?>"> <?php echo $value["client"];?></option>
      <?php

    }
   ?> 
</select>

</label>
<button id="btn">Send Data</button>  
</form>

My code does these actions:

So since i do this steps for many clients i don't want my page to refresh. When i add return false or e.preventDefault the marquer is not displayed, when i remove it the page refresh i can get my marquer but i'll lost it when selecting another client. is there a way to do this ?

EDIT I've tried using this code, php_query.php is my current page , but the page still refresh.

 $("#btn").click(function(){

var vname = $("#selectrefuser").val();
var data = 'start_date=' + vname;
var update_div = $('#update_div');

$.ajax({
        type: 'GET',
        url: 'php_query.php',
        data: data,   
        success:function(html){
           update_div.html(html);
        }
    });

Edit

When adding e.preventDfault , this code doesn't seem to work

$( "#monbutton" ).click(function() {

  php_lat = <?php echo $resclient_alt; ?>;
  php_long = <?php echo $resclient_long; ?>;


$('#myResults').html("je suis "+php_long);

var chicago = new google.maps.LatLng(parseFloat(php_lat), parseFloat(php_long));

addMarker(chicago);
});

This code recuperate this value var vname = $("#selectrefuser").val(); get result from sql query and return it to jquery .

Upvotes: 0

Views: 486

Answers (1)

Saqueib
Saqueib

Reputation: 3520

It will refresh since you have not prvent default action of <button> in script

 $("#btn").click(function(e){ //pass event 
    e.preventDefault(); //this will prevent from refresh 

    var vname = $("#selectrefuser").val();
    var data = 'start_date=' + vname;
    var update_div = $('#update_div');

    $.ajax({
        type: 'GET',
        url: 'php_query.php',
        data: data,   
        success:function(html){
           update_div.html(html);
        }
    });

Updated

Actually, i want my php code to be written on the same page as the one containing html and jquery code

You can detect the ajax call on php using below snippet

/* AJAX check  */
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    /* special code here */

}

Upvotes: 1

Related Questions