how to call a php file from javascript? -


hi new javascript, looking help. have form, shown below. when user gives input , clicks submit, want call php file (shown below also) invoked , return hello. here code have tried, though not working intended.

 <html> <head>  <script type='text/javascript' src="http://code.jquery.com/jquery-1.8.0.min.js"></script> </head> <body> <form> <input id="name-typed" type="text"  /> <input id="name-submit" type="submit" value="submit" onclick="post()"/> </form> <div id="result"></div> </body> <script> function post() {  var hname = $('input#name-typed').val();  $.post('dat.php',{name: hname},function(data) { $('div#result').html(data); });   } </script> </html> 

the php file dat.php looks this: <?php echo "hello";?>

as in first paragraph, can suggest changes should make?

try this:

you can change dat.php say:

<?php  $name = $_post['name']; echo "hello ".$name;  ?> 

and html file be:

notice button type changed normal button execute function rather submitting form.

<html> <head>  <script type='text/javascript' src="http://code.jquery.com/jquery-1.8.0.min.js"></script> </head> <body> <form> <input id="name-typed" type="text"  /> <button type="button" onclick="post()">submit</button> </form> <div id="result"></div> </body> <script> function post() {   $.ajax({          type: "post",         url: "dat.php",         data: { name: $('#name-typed').val()              },         success: function(response) {                  $('#result').html(response);        }     }); }  </script> </html> 

Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -