Jump to content

Use variable/text returned via ajax in php


ask21900

Recommended Posts

My javascript experience is a little limited so I was hoping someone can help me out...

 

I have a form that is inserted into mysql by sending the data to a php script via ajax.  The php script will return data to populate the div.  I would then like to use a portion of the returned data to populate a second form on the page.

 

Thanks in advance for all your help!

Link to comment
Share on other sites

I'll guess that what you want to do is use the returned php to change some html right? Just put something like this in the function you call onreadystatechange

 


if (http.readyState == 4) {

    var result=http.responseText;

    document.getElementById('SomeDivOrSomething').innerHTML=result;

  }

Link to comment
Share on other sites

Code?

 

I haven't wrote the code yet because I want to make sure I can do what I want it to before writing it, but here is some (very) rough pseudo code:

 


//Upon initial load

open first div
    form1 start
        textbox1.1
        textbox1.2
    submit
close first div

//after ajax call

open first div
    display userId generated by php and returned by ajax
    display the data from form1 as returned by ajax
    display additional data supplied by php/ajax
close first div

open second div
    form2 start
        textbox2.1  //should be equal to the userId generated by php above
        textbox2.2
        textbox2.3
    submit form2
close second div

I'll guess that what you want to do is use the returned php to change some html right? Just put something like this in the function you call onreadystatechange

 


if (http.readyState == 4) {

    var result=http.responseText;

    document.getElementById('SomeDivOrSomething').innerHTML=result;

  }

 

Assigning the value to form2>textbox1 would work using the .value attribute, but if at all possible, I would like to do this without modifying my ajax function since I use it for hundreds of pages already.  If it is necessary to use this method, I can duplicate my ajax function and make the mods just for this page, but I would rather not.

 

Maybe I am going about this the wrong way...  is there a way to modify the url/post data of a page using ajax (without refreshing)?  For instance, can I just write the data in form1 to the url (in addition to the ajax get) so that it can be used by php during the scripting of form2?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.