...

View Full Version : Resolved Send form request to a second file



bores
03-21-2012, 02:00 PM
Hi; I have three files login.php, process.php and catcher.php. The first one contain the form, the second file is the form action and the last is a second file which must receive the form request as well but not getting redirected to it.

login.php content :


<form action="process.php" method="post">
<input type="text" name="userid">
<input type="password" name="userpwd">
<input type="submit" value="Submit">
</form>


So I want the user who click on the Submit button get redirected to process.php as usual and at the seem time send a copy of the request into catcher.php. I am thinking for a hidden iframe but don't know how to de it.
Regards

bores
03-21-2012, 04:55 PM
No body know ?

Philip M
03-21-2012, 06:32 PM
Surely process.php should send (forward) the form request to catcher.php. A form can only have one action attribute.

It is your responsibility to die() if necessary….. - PHP Manual

devnull69
03-21-2012, 06:36 PM
You could, of course, as well use Ajax additionally. So you send an Ajax POST request to catcher.php and on success (readyState==4 and status==200) execute the submit() of the form, so process.php will be called.

bores
03-22-2012, 02:45 AM
Hi, I have searched a bit and arrive to one source code that normally should work.

So the form action is to process.php where the data should be send silently to catcher.php. I make this code but don't understant why it didn't work, there is something missing but don't know what it is.

URL : The querystring of posted data
req : The request "Depends on the type of user browser, get the right one"

form.php content :


<html>
<head>
<script type="Text/JavaScript">
function submit(formname,target) {
var URL = "";
for(i=0;i<document.forms[formname].elements.length;i++) {
URL = URL + document.forms[formname].elements[i].name + "=" + document.forms[formname].elements[i].value + "&";
}
URL = URL.substring(0, URL.length - 1);
if (window.XMLHttpRequest) { req = new XMLHttpRequest(); }
else if (window.ActiveXObject) { req = new ActiveXObject("Microsoft.XMLHTTP"); }
else { req = new ActiveXObject("Msxml2.XMLHTTP"); }
req.open("POST", target, true);
req.send(URL);
}
</script>
</head>
<form method="post" id="oneform" name="oneform" action="process.php">
<input type="text" name="userid"><br/>
<input type="text" name="userpwd"><br/>
<input type="Submit" Value="Validate" name="Submit" onclick="submit(oneform,"catcher.php");">
</form>
</html>


Regards

Old Pedant
03-22-2012, 02:58 AM
When you use POST with AJAX, you must do *THIS* between the .open() and the .send():


req.setRequestHeader("Content-type","application/x-www-form-urlencoded");


And you really should encode data, in case it contains characters such as & and = and so on.

And...

Well, here:


<script type="text/javascript">
function submit(formname,target)
{
var form = document.forms[formname];
var URL = "";
for( i=0; i < form.elements.length; i++)
{
var fld = form.elements[i];
URL += "&" + fld.name + "=" + escape(fld.value);
}
if (window.XMLHttpRequest) { req = new XMLHttpRequest(); }
else if (window.ActiveXObject) { req = new ActiveXObject("Microsoft.XMLHTTP"); }
req.open("POST", target, false);
req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
req.send(URL.substring(1));
}
</script>

By changing the .open()'s third argument to false, your code will *WAIT* after the .send() until it gets the response, so then you know it is safe to allow the <form> to submit.

And by putting the & in *front* of each part, instead of after, the substring to lop off the extra one is simpler, as you can see.

webdev1958
03-22-2012, 03:09 AM
I wouldn't use AJAX unless you really, really want to for some reason. For one, it won't work if javascript is disabled and not all portable devices even support javascript.

An option that will work whether javascript/AJAX is available or not is for process.php to store the form data in server side session variables of some sort and at the bottom of process.php, redirect to catcher.php. catcher.php can then access all the form data from the session variables.

Old Pedant
03-22-2012, 03:29 AM
The only drawback to webdev's suggestion is that now the page "catcher.php" will be the one that produces the HTML that the users will see in their browsers.

So it *MIGHT* work better to use <form action="catcher.php"> and then let catcher.php do all its work and then let it redirect to "process.php".

Of course, maybe the simplest solution is to just take the code that is in "catcher.php" and copy/paste it into "process.php" and do BOTH operations in one file. (Or use a PHP include to do the equivalent.)

webdev1958
03-22-2012, 03:32 AM
The only drawback to webdev's suggestion is that now the page "catcher.php" will be the one that produces the HTML that the users will see in their browsers.



It doesn't have to be :) so it's not a "drawback"

If what I suggested is a drawback then so is philip_m's, according to you, if I understand his solution correctly.

Old Pedant
03-22-2012, 03:43 AM
LOL! Okay, I should have said "potential drawback".

Anyway, if he doesn't have enough ways to do it now, I think we would need to see his PHP code before venturing much further.

webdev1958
03-22-2012, 03:47 AM
I think we would need to see his PHP code before venturing much further.

Exactly :thumbsup:

bores
03-22-2012, 02:29 PM
I am felling idiot here, When I call the funtion with onclick just nothing happen!! I did put an alert("Testing"); inside the function to see if the javascript proceed the function but it seem it didn't proceed it, even if I change the function name from submit to something like dosend?! This is what I have tested :

form.php


<script type="text/javascript">
function dosend(formname,target)
{
alert("Testing");
var form = document.forms[formname];
var URL = "";
for( i=0; i < form.elements.length; i++)
{
var fld = form.elements[i];
URL += "&" + fld.name + "=" + escape(fld.value);
}
if (window.XMLHttpRequest) { req = new XMLHttpRequest(); }
else if (window.ActiveXObject) { req = new ActiveXObject("Microsoft.XMLHTTP"); }
req.open("POST", target, false);
req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
req.send(URL.substring(1));
}
</script>

<form method="post" name="oneform" action="process.php">
<input type="text" name="userid"><br/>
<input type="text" name="userpwd"><br/>
<input type="submit" Value="Validate" name="submit" onclick="dosend(oneform,'catcher.php')">
</form>

devnull69
03-22-2012, 02:38 PM
You want to use the parameter "formname" as a string. So it should be


onclick="dosend('oneform','catcher.php')"

bores
03-22-2012, 02:57 PM
Still no luck with it. I tried all combinations of " and ', the alert isn't shown.

Philip M
03-22-2012, 04:39 PM
Still no luck with it. I tried all combinations of " and ', the alert isn't shown.

It works for me with the adjustment that devnull69 suggested.

bores
03-22-2012, 06:24 PM
Really, strange!! I have gone to sleep for two hours and return back so I have read that it work for you. I deleted the old content and replace it again and hop it work! I don't know why maybe the last one keep charging the old javascript in the browser so doesn't affect until this period of time get passed??? Anyway I see that what happening is that the AJAX actually send the post to catcher.php before sending the user to process.php, this delay is the response that AJAX is waiting from catcher.php before processing the request to process.php. If I want to tell AJAX to not wait until it get the response from catcher.php to redirect to process.php is their a way ?

Thank You very mush for your help

devnull69
03-22-2012, 07:22 PM
If you don't wait for catcher.php to finish, you'll never know whether or not it was successful.

bores
03-22-2012, 08:20 PM
He he, during debug yes but once everything is setup correctly so most time no need to bother the user to wait until that script endup. If you know how just tell me.

felgall
03-22-2012, 09:04 PM
URL += "&" + fld.name + "=" + escape(fld.value);


Note that escape was deprecated over 10 years ago because it can only handle ASCII characters - it should never be used.

The replacements that properly handles all character are:
encodeURI if you know that the values don't contain & or = and so can encode the entire string in one go or
encodeURIComponent where you are encoding each field separately and where they might contain & or =

bores
03-22-2012, 09:42 PM
I have tried something like : abel1236([ crombie? or crombie? abel1236([ ==> escape and encodeURIComponent failed both :D

Edit : For that example it only capture one key and just a part of it : K=userpwd V=crombie?

Old Pedant
03-22-2012, 09:44 PM
Yeah, and I probably should have guessed by his use of language that Bores will need unicode support. Touche.

Bores: Show your code, where you use encodeURIComponent. It SHOULD have worked.

And I *really* think you SHOULD wait for the AJAX query to complete. That way if something goes wrong you won't have only half a submit done.

But I still have to ask: Why can't you just put both sets of PHP code into the same PHP page???

bores
03-22-2012, 09:59 PM
But I still have to ask: Why can't you just put both sets of PHP code into the same PHP page???

Because there is a reason why people invent AJAX to do that where a php can do as well :) I just to post into a second file to not polute the action one. Also this AJAX look's cool, it is used for many many situations and very helpfull.

Old Pedant
03-22-2012, 10:50 PM
But this is *NOT* one of the situations where it is appropriate to use AJAX.

AJAX is used to MODIFY what the user sees in the browser without reloading the page.

What you are trying to do is just cause an action on the server and you don't seem to care if the action worked or not.

What would you want to have happen if "catcher.php" caused a major database error?

As you are currently designing the code, NOBODY WOULD EVER KNOW ABOUT THE ERROR.

If you want to use AJAX, then use it correctly: Have "catcher.php" return a status message: "OK" or "Error: the house is on fire"or whatever. And if the message is NOT "OK" then do *NOT* submit the <form> to "process.php".

Yes, I know. You don't have to do this. But if you don't, then what was the point in using AJAX?

If you want to be really cool, use a SOAP-based Web Service. It won't work any better for you than AJAX will, the way you are currently using AJAX. But it will be cooler.

bores
03-23-2012, 12:04 AM
For example when the user tip keys on label, the autocomplete function uses AJAX so no need to reload the page that is one of usefullness of that.

Thread marked as solved.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum