javascript - Unsolved PHP page refreshing without Ajax [Solved - Thanks!!] -


please, can publish mistakes corrected , tested code problem? program - 22.php has form. when user enter , click submit button, result should taken 23.php , displayed in div on 22.php

i tried solutions below , none of them solve problem;

1) changed to: $("#testform").submit(function(event){

2) included "return false;" @ end prevent submit form , reload page.

3) clear browser cache

i can see happen program computer;

1) not error message after click submit.

2) can see tab of page reloads , entered text fields cleared.

3) no error message or result shows.

<html>          <head>        <title>my first php page</title>        <script type="text/javascript"            src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>        <script>        $(document).ready(function() {                      $("#btn").click(function(event){             event.preventdefault();             var myname = $("#name").val();             var myage = $("#age").val();             yourdata ='myname='+myname+'&myage='+myage;                  $.ajax({                    type:'post',                    data:yourdata,//without serialized                                      url: '23.php',                    success:function(data) {                       if(data){                          $('#testform')[0].reset();//reset form  			$('#result').val(data);                          alert('submitted');                       }else{                          return false;                        }                                             };                   });                                  });                        });      </script>     </head>      <body>        <form method="post" id="testform">          name:          <input type="text" name="name" id="name" />age:          <input type="text" name="age" id="age" />          <input type="submit" name="submit" id="btn" />        </form>  	<div id='result'></div>      </body>  </html>

<?php if ( isset($_post['name']) ) { // form submitted?     echo "welcome ". $_post["name"] . "<br>";     echo "you ". $_post["age"] . "years old<br>"; } 

?>

try formatting post data inside ajax function.

$.ajax({   type:'post',   data : {      myname: myname      myage: myage   }   ... } 

edit

try removing ; in

 return false;           }                            }; 

to

return false;           }                            } 

Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -