javascript - Run a PHP function on success of AJAX request completion -


i know in js, can use success of ajax function trigger start other events...

so have loaded new page using ajax. , through have passed parameters, have passed:

xmlhttp.open("get","register-form.php?one="+wordchoiceone+"&two="+wordchoicetwo+"&three="+wordchoicethree,true); 

so have variables $one, $two, $three in php

i want complete couple of tasks using these (concatenate, capitalise, assign etc) before outputting onto page. don't want bound click of button or anything, want bound the success of ajax load.

how can start these actions in php?

pseudo-code:

function setusername() // run on ajax success{      // capitalise first letter of 1 2 , 3      // concatenate      // store in variable $username } 

then echo username out

eta. have written ajax request , works fine. it's loaded, want perform php function, binding trigger of function success of ajax load:

<?php  $_get['one'], $_get['two'] , $_get['three'];  // capitalise first letter $one = ucfirst($one);  $two = ucfirst($two);  $three = ucfirst($three);   $username = $one . $two . $three;  ?> 

edit: ok appears can't explain myself, or haven't understood required functionality. here need happen in plain english, links.

i building registration system. instead of letting people type in own username, have username builder. can see here: http://marmiteontoast.co.uk/fyp/login-register/register-username-builder.php

step 1 in javascript

you drag tiles boxes, hit button shows up. when have three, hit big green button. until point, in jquery, , have following 3 variables stored:

wordchoiceone - first word choose wordchoicetwo - second word choose wordchoicethree - third word choose

these stored js variables.

step 2 php time

so have these 3 stored variables wish move away js , start using php. have built working registration uses username input rather when helps "build" own.

my research has led understanding because of client side server side switch way can pass these variables php through ajax request. seems pretty handy me because registration form have built loaded page asynchronously. asked advice how "share" these jquery values through ajax request , helped following, pass them through url:

xmlhttp.open("get","register-form.php?one="+wordchoiceone+"&two="+wordchoicetwo+"&three="+wordchoicethree,true); 

my understanding (and maybe i'm wrong? please let me know , explain if am...) assigns existing variables of wordchoicetwo etc php variable of $two. right?

so have php variables $one, $two , $three...

running php

perhaps because more used working js, js have do something (button click etc) able enact function. know 1 such do something successul ajaxc request isn't php. question... , question had, ** how start running of php function when you've loaded somethhing through ajax?** want start running php function called `setusername" on page. don't want user have press button make start, want bound success of ajax completion or similar this, understand might js only.

this ajax call have in place:

function saveusername(wordchoiceone, wordchoicetwo, wordchoicethree){  var xmlhttp; if (window.xmlhttprequest)   {// code ie7+, firefox, chrome, opera, safari   xmlhttp=new xmlhttprequest();   } else   {// code ie6, ie5   xmlhttp=new activexobject("microsoft.xmlhttp");   } xmlhttp.onreadystatechange=function()   {   if (xmlhttp.readystate==4 && xmlhttp.status==200)     {     document.getelementbyid("login-register-wrapper").innerhtml=xmlhttp.responsetext;     }   }   xmlhttp.open("get","register-form.php?one="+wordchoiceone+"&two="+wordchoicetwo+"&three="+wordchoicethree,true); xmlhttp.send(); } 

add json header type , echo

$_get['one'], $_get['two'] , $_get['three'];  // capitalise first letter $one = ucfirst($one);  $two = ucfirst($two);  $three = ucfirst($three);   $username = $one . $two . $three; header('content-type: application/json'); echo json_encode(array('username'=>$username)); // <--- 

then in js, bind function success of call:

xmlhttp.onreadystatechange=function() {    if (xmlhttp.readystate==4 && xmlhttp.status==200) {      //xmlhttp.responsetext;    }  } 

edit, because there seems confusion:

just because ajax call reaches php script doesn't mean it's executed. request nature sit , wait reply. reply can contain data if php echo's it.

edit 2 can wrap head around principle of it, here's oversimplification of think want:

file 'ajax.html':

<div id="name"></div> <script>     function myname(first, last) {         var req = new xmlhttprequest();         req.open('get', 'ajax.php?first='+first+'&last='+last);          req.onreadystatechange = function() {             if(req.readystate==4 && req.status==200) {                 data = json.parse(req.responsetext);                 document.getelementbyid('name').innerhtml = data.username;             }         }         req.send();     }     myname('john', 'doe') </script> 

file 'ajax.php':

$first = $_get['first']; $last = $_get['last']; header('content-type: application/json'); echo json_encode(array('username'=>ucwords($first) . ' ' . ucwords($last))); 

this how ajax used


Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -