[php] Submitting a form in AJAX?

Soldato
Joined
18 Oct 2002
Posts
9,053
Location
London
I have my datatable with one hidden row (display:none) , and I was planning on updating this empty row with new data.
The new data is inserted from a small form just above the datatable.

So far I have an AJAX function that updates the top hidden row with the last record inserted, and displays the row.
However, when I submit the form (to post the data) it just obviously reloads the whole page (defeating the point of AJAXing the last row)....

Am I going about this all wrong? I don't see how I can submit the form without reloading the page, and when the page is reloaded, what's the point in AJAX.

The only thing I can think is to put the form into an iframe, but this seems wrong, and also I don't think it's supported by strict XHTML.

Any help would be greatly appreciated!
 
Well there's shed loads of code, which I think will just confuse the issue, but it's in the usual manner (I think)..


Code:
if (!empty($_POST)) {
//insert to db here
}

<form action="<?=$scriptName?>" method="post">
[textboxes and stuff here]
</form>


I see what you mean about the form action, that is causing the unwanted reload. But how do I get the AJAX to do the insert process?

i.e.

<form action="<?=$scriptName?>" method="post" onsubmit="ajaxcall(); return false;">
[textboxes and stuff here]
</form>
 
Yeah I understand it basically. But I can't work out how by calling another script I will be able to send the post information to that.
Or am I supposed to pass the input information as parameters into the ajax function?


i.e. for example sometihng like this?
Code:
<form onsubmit="ajaxcall(<?=$_GET['firstName']?>,<?=$_GET['lastName']?>); return false;">

And then in the ajax function...

Code:
function ajaxcall(f,l) {
	
		var url = 'insert_script.php?firstname='+f+'&lastname='+l+'';
		
		XMLHttpRequestObj.open("GET", url, true); 
                    ..............
}

Untested obviously, but is how it should be done?
I've just not done anything along these lines before so I wanted to make sure I got the 'proper' way.
 
lol
Yes I am out of my depth with AJAX, that's why I'm asking for help! :D
But you're not telling me if what I'm suggesting is correct or not? Which is all I need.

I'm asking about the process in general. So I'm obviously missing out lots of details such as escaping input... Listing all the input fields in my form... The fact that my script I haven't created (yet) called 'insert_script' - will process the variables from the querystring (get) and insert them into the DB. And of course the eventual return of this record.

If you're saying thiis is the 'correct' way of going about things, then I'll do that :) But I'm not sure if what you're saying is that I'm doing it wrong...

Thanks for your help so far though!
 
Back
Top Bottom