Posting JavaScript Value to PHP via Ajax issue -


i need value jquery php can search function site.

i have tried:

<script>  $(document).ready(function () {     $('#search_button').click(function(e){         e.preventdefault();         e.stoppropagation();         carsearch();    }); });  function carsearch()  {     $.ajax({         type: "post",         url: 'cars.php',         data:          {             mpg : $('.mpg').val()         },         success: function(data)         {             alert("success! "+$('.mpg').val()+"mpg");         }     });  }  </script> 

this ajax running when button pressed , js value there displayed in alert.

however

if(isset($_post['mpg']))                 {                     $query = "select * cars mpg =< ".($_post['mpg'])."";                     echo "<div class='test'></div>";                 }                 else                 {                     $query = "select * cars";                 }  

the isset doesn't trigger, div big blue box testing purposes. ajax posting cars.php ajax is, posting own file. i've not read being done, i've posted within same file before not ajax.

i have tried posting value ajax file:

<?php session_start();  $conn = mysql_connect('localhost', 'root', ''); mysql_select_db('cdb', $conn);  if(isset($_post['mpg'])) {     $r = mysql_query  ("insert test values ".($_post['mpg'])."", $conn);} } ?> 

just test if doing , isn't.

so

data:          {             mpg : $('.mpg').val()         },  

appears wrong, though got looking @ many many other questions on here passing js php. i've tried variations i've seen on here, , above code results in success function alert triggering.

i see mistake here

$r = mysql_query  ("insert test values ".($_post['mpg'])."", $conn);} 

the "}" @ end of line throw error. there error in .php file when ajax running ?

to check if there errors or see .php file returning follow these steps:

  1. using chrome right click on page , choose "inspect element"
  2. click on "network" in menu
  3. if not active click on filter icon (depending on chrome version)
  4. click on xhr element of submenu
  5. refresh page , .php file should appear
  6. click on , see file returning

(it done in firefox not same titles)

tell got here.

also, should go extern file way. because ajax data returning correspond .php file content. if .php file contains else need return, there problem. example: html on .php file returning in ajax data , far want.


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -