0

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:

  • select client get its GPS coordinates
    • recuperates them in php variable
    • use them as jquery variable
    • display marquer on map

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 .

8
  • If i understood your question correctly, you can use AJAX for your task... Commented Mar 2, 2015 at 11:26
  • You can use ajax for submit pages without refresh the page. Commented Mar 2, 2015 at 11:26
  • @phpfresher can you see my edit ? Commented Mar 2, 2015 at 12:30
  • Hii i@ItShine... i saw it.. am not much aware of AJAX with JQuery, but when i checked it in internet it seems correct only.. Is javascript enabled in your browser, did u check it ? Commented Mar 2, 2015 at 12:39
  • @phpfresher yes it's enabled Commented Mar 2, 2015 at 12:59

1 Answer 1

1

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 */

}
Sign up to request clarification or add additional context in comments.

3 Comments

when i add e.preventDefault my following code doesn't work , please see my edit
can you post your complete js code in pastebin, it will be helpful
share the link of pastebin

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.