lighton Posted May 27, 2007 Share Posted May 27, 2007 Hi, In a nutshell i am stuck trying to post data using AJAX, the system as is follows: database fields are requested from the database and laid out on the page, the user can click an edit button, which uses javascript to rewrite the page into a form, now i want to be able to post that form to a php page which will update the database, all without the page refreshing hence AJAX. How do i get the information out of the form to be able to pass it to the AJAX object for posting? Any help would be greatly appreciated! Quote Link to comment Share on other sites More sharing options...
gabeg Posted May 27, 2007 Share Posted May 27, 2007 Have the form tag look like <form action="" name="" id="" onSubmit="ajaxfunction(); return false;"> Now, I'm not too sure the best way to do this, but what I do is create my xmlhttp request and then use var input1 = document.getElementById('input1').value; the above gets the input from the input with the id="input1" <input type="text" name="input1" id="input1"> then simply pass the input1 in through the xmhttp.send() after you do the xmlhttp.open with POST. Quote Link to comment Share on other sites More sharing options...
lighton Posted May 27, 2007 Author Share Posted May 27, 2007 Have the form tag look like <form action="" name="" id="" onSubmit="ajaxfunction(); return false;"> Now, I'm not too sure the best way to do this, but what I do is create my xmlhttp request and then use var input1 = document.getElementById('input1').value; the above gets the input from the input with the id="input1" <input type="text" name="input1" id="input1"> then simply pass the input1 in through the xmhttp.send() after you do the xmlhttp.open with POST. thanks for your response.. so far i have set it up as follows(i still havent got it working though) form action="javascript:function();" then wip out the form values through document.formname.fieldname.value encode the values using "value="+ urlencode(value) and append all to a var send the var as the data in ajax.open("post", data) to a php and then try using $_POST to get the values out. but i havent had much luck yet! does this look right to you? i think my main issue is trying to send the form data to the php handler so i can extract the form fields by $_POST. does onsubmit() gather all the data from the form? or is it another way of making the submit button call a javascript function. Quote Link to comment Share on other sites More sharing options...
gabeg Posted May 27, 2007 Share Posted May 27, 2007 here is a snippet of code that i'm using and works //url of the reponse page, and the GET variables var myurl = '/ajax/page.php'; var params = 'id=' + encodeURI(restid) + '&review=' + encodeURI(reviewtext) + '&recommend=' + encodeURI(recommend); //sending the request http.open("POST", myurl, true); http.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); http.setRequestHeader("Content-length", params.length); http.setRequestHeader("Connection", "close"); http.onreadystatechange = displayNewComment; http.send(params); Quote Link to comment Share on other sites More sharing options...
lighton Posted May 28, 2007 Author Share Posted May 28, 2007 cheers buddy, i had put http.setRequestHeader("Content-type", "application/x-www-forms-urlencoded"); instead of http.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); can you spot the difference, doh! Quote Link to comment Share on other sites More sharing options...
gabeg Posted May 28, 2007 Share Posted May 28, 2007 cheers buddy, i had put http.setRequestHeader("Content-type", "application/x-www-forms-urlencoded"); instead of http.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); can you spot the difference, doh! It's always one small error!! Sometimes if I can't figure out my error, and all my code looks write I'll delete it and type it in again Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.