Jump to content

AJAX Form Post with PHP


lighton

Recommended Posts

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! :)

Link to comment
https://forums.phpfreaks.com/topic/53152-ajax-form-post-with-php/
Share on other sites

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.

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.

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);

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! ;D

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.