Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 24
  1. #1
    New Coder
    Join Date
    Feb 2012
    Posts
    22
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Send form request to a second file

    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 :
    PHP Code:
    <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
    Last edited by bores; 03-23-2012 at 12:02 AM.

  • #2
    New Coder
    Join Date
    Feb 2012
    Posts
    22
    Thanks
    14
    Thanked 0 Times in 0 Posts
    No body know ?

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,037
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    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

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    bores (03-22-2012)

  • #4
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    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.

  • Users who have thanked devnull69 for this post:

    bores (03-22-2012)

  • #5
    New Coder
    Join Date
    Feb 2012
    Posts
    22
    Thanks
    14
    Thanked 0 Times in 0 Posts
    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 :
    PHP Code:
    <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(0URL.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"targettrue);
    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
    Last edited by bores; 03-22-2012 at 02:50 AM.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,611
    Thanks
    78
    Thanked 4,387 Times in 4,352 Posts
    When you use POST with AJAX, you must do *THIS* between the .open() and the .send():
    Code:
    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:
    Code:
    <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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    bores (03-22-2012)

  • #7
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    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.
    Last edited by webdev1958; 03-22-2012 at 03:13 AM.

  • Users who have thanked webdev1958 for this post:

    bores (03-22-2012)

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,611
    Thanks
    78
    Thanked 4,387 Times in 4,352 Posts
    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.)
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    bores (03-22-2012)

  • #9
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Quote Originally Posted by Old Pedant View Post
    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.
    Last edited by webdev1958; 03-22-2012 at 03:47 AM.

  • Users who have thanked webdev1958 for this post:

    bores (03-22-2012)

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,611
    Thanks
    78
    Thanked 4,387 Times in 4,352 Posts
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    bores (03-22-2012)

  • #11
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Quote Originally Posted by Old Pedant View Post
    I think we would need to see his PHP code before venturing much further.
    Exactly

  • Users who have thanked webdev1958 for this post:

    bores (03-22-2012)

  • #12
    New Coder
    Join Date
    Feb 2012
    Posts
    22
    Thanks
    14
    Thanked 0 Times in 0 Posts
    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
    PHP Code:
    <script type="text/javascript">
    function 
    dosend(formname,target
    {
        
    alert("Testing");
        var 
    form document.forms[formname];
        var 
    URL "";
        for( 
    i=0form.elements.lengthi++) 
        {
            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"targetfalse);
        
    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> 

  • #13
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    You want to use the parameter "formname" as a string. So it should be
    Code:
    onclick="dosend('oneform','catcher.php')"

  • Users who have thanked devnull69 for this post:

    bores (03-22-2012)

  • #14
    New Coder
    Join Date
    Feb 2012
    Posts
    22
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Still no luck with it. I tried all combinations of " and ', the alert isn't shown.

  • #15
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,037
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    Quote Originally Posted by bores View Post
    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.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    bores (03-22-2012)


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •