Tuesday, 15 January 2013

javascript - Data not getting passed via Ajax to PHP script -



javascript - Data not getting passed via Ajax to PHP script -

i'm trying send value of variable number via ajax php script. php script not printing desired output on opening on browser. tried couldn't find whats wrong here. pointers ?

index.html

<button type="submit" class="btn btn-success" id = 'first' onclick='process();'>submit</button> <script> var number = 0; function process() { number++; var xhr; if (window.xmlhttprequest) { xhr = new xmlhttprequest(); } else if (window.activexobject) { xhr = new activexobject("microsoft.xmlhttp"); } var info = "num=" + number; xhr.open("post", "index.php", true); xhr.send(data); } </script>

index.php

<?php session_start(); $number = $_post['num']; $_session['numb'] = $number; echo $_session['numb'] ; ?>

editing long winded lame reply since fixed close curly bracket... bloodyknuckles right need in 'process' function take response php page , output it... or whatever want do. can utilize method 'onreadystatechange' in xmlhttprequest object , 'readystate' property value of 4 means done. here simple illustration of outputting results console (which can view using developer tools in browser of choice).

<script> var number = 0; function process() { number++; var xhr; if (window.xmlhttprequest) { xhr = new xmlhttprequest(); } else if (window.activexobject) { xhr = new activexobject("microsoft.xmlhttp"); } var info = "num=" + number; xhr.onreadystatechange = function(){ if (xhr.readystate==4 && xhr.status==200){ console.log('xhr.readystate=',xhr.readystate); console.log('xhr.status=',xhr.status); console.log('response=',xhr.responsetext); } else if (xhr.readystate == 1 ){ xhr.send(data) } }; xhr.open("post", "ajax-test.php", true); } </script>

as go farther may want update php page update session when post value there.

<?php //ini_set('display_errors',1); //ini_set('display_startup_errors',1); //error_reporting(-1); if(isset($_post['num'])){ session_start(); $_session['numb'] = $_post['num']; echo $_session['numb']; } ?>

you can uncomment ini_set , error_reporting lines seek figure out going on php script.

javascript php html ajax

No comments:

Post a Comment