POST Method Problem - absolute beginner :)

Discussion in 'PHP' started by gentlefoot, Jul 7, 2007.

  1. #1
    Hi all,

    I'm very new to PHP (started yesterday lunchtime) so please let me know if I'm being stupid :)

    I have an example of how to use the POST method which works fine but when I attempt to use this method with my own form I cannot get the value passed across. Here is the code from the example that works:

    1st Page Code:

    <form action="processtest.php" method="post">
    Your Name: <input type="text" name="name"><br>
    </form>

    2nd Page Code (processtest.php):
    $name=$_POST['name'];
    echo $name

    This works fine and displays the value I enter in the text box on the first page.

    My version looks like this but doesn't work:

    1st Page Code:

    <form id="form1" name="form1" method="post" action="ProcessEnquiry.PHP">
    <label>
    <input type="text" name="forename" id="forename" />
    </label>
    </form>

    2nd Page Code (ProcessEnquiry.PHP):

    $forename=$_POST['forename'];
    print $forename;

    Any help would be much appreciated.
     
    gentlefoot, Jul 7, 2007 IP
  2. uglyboy

    uglyboy Peon

    Messages:
    1,963
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Wont this work..

    1st Page Code:
    <form action="ProcessEnquiry.php" method="post">
    Your Name: <input type="text" name="forename"><br>
    </form>

    2nd Page Code (ProcessEnquiry.php):
    $forename=$_POST['forename'];
    echo $forename

    ??????????
     
    uglyboy, Jul 7, 2007 IP
  3. gentlefoot

    gentlefoot Guest

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    No it doesn't seem to. It must be something to do with the other forms or something.
     
    gentlefoot, Jul 7, 2007 IP
  4. gentlefoot

    gentlefoot Guest

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    The rest of the body looks like this:

    <body>
    <div class="style1" id="apDiv1">Please Fill in this enquiry form and a representative will contact you</div>
    <div id="apDiv2">Forename:</div>
    <div id="apDiv3">
    <form action="ProcessEnquiry.php" method="post">
    <input type="text" name="forename"><br>
    </form>

    </div>
    <div id="apDiv4">Country:</div>
    <div id="apDiv6">
    <form id="form3" name="form3" method="post" action="">
    <label></label>
    <label>
    <input name="radio" type="radio" id="UK" onclick="MM_showHideLayers('apDiv7','','show','apDiv8','','show')" value="UK" checked="checked" />
    In UK</label>
    <label></label>
    <label>
    <input name="radio" type="radio" id="OutsideUK" onclick="MM_showHideLayers('apDiv7','','hide','apDiv8','','hide')" value="OutsideUK" />
    Outside UK</label>
    </form>
    </div>
    <div id="apDiv7">County:</div>
    <div id="apDiv8">
    <form id="form4" name="form4" method="post" action="">
    <label>
    <select name="County" size="1" id="County">
    <option>Surrey</option>
    <option>East Sussex</option>
    <option>West Sussex</option>
    <option>Devon</option>
    <option>Someset</option>
    <option>Nottinghamshire</option>
    <option>Bedfordshire</option>
    </select>
    </label>
    </form>
    </div>
    <div id="apDiv9">Please enter your enquiry here:</div>
    <div id="apDiv10">
    <form id="form2" name="form2" method="post" action="">
    <label>
    <textarea name="Enquiry" id="Enquiry" cols="45" rows="5"></textarea>
    </label>
    </form>
    </div>
    <div id="apDiv11">
    <form id="form5" name="form5" method="post" action="ProcessEnquiry.PHP">
    <label>
    <input type="submit" name="Submit" id="Submit" value="Submit" />
    </label>
    </form>
    </div>
    </body>

    But I'm not doing anything with the other info as yet.
     
    gentlefoot, Jul 7, 2007 IP
  5. gentlefoot

    gentlefoot Guest

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    The ProcessEnquiry.PHP body looks like this (I've removed some credentials for obvious reasons)

    <body>
    <?php
    // Connects to your Database
    mysql_connect("localhost", "********_*******", "*****") or die(mysql_error());
    mysql_select_db("*********_*******") or die(mysql_error());
    //Update db
    $forename=$_POST['forename'];
    echo $forename;

    ?>
    <div id="apDiv1">Your enquiry has been forwarded. A representative will contact you shortly.</div>
    <?php
    print $forename;
    print hdsjhdfsjjksfd
    ?>
    </body>
     
    gentlefoot, Jul 7, 2007 IP
  6. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #6
    <form action="edit.php" method="post">
    <input type="text" name="forename"><br>
    <input type="submit">
    </form>
     
    MMJ, Jul 7, 2007 IP
  7. gentlefoot

    gentlefoot Guest

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I've tried everything now short of starting all over again. Any more suggestions?
     
    gentlefoot, Jul 7, 2007 IP
  8. gentlefoot

    gentlefoot Guest

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8

    Yes, just tried adding a submit button with the text box and that made no difference either.

    I even tried simply copying across my working example code into the two new scripts and just chaning the action value and that doesn't even work.

    I know the pages are updating because I have added some changes to the text just to make sure and have also been checking the page source each time to be double sure nothing is hidden or anything. This is really drving me nuts now. Nearly 4 hours on such a simple thing! lol
     
    gentlefoot, Jul 7, 2007 IP
  9. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #9
    That code worked fine for me.

    Post all your current code enclosed in [html] & [php] tags.
     
    MMJ, Jul 7, 2007 IP
  10. gentlefoot

    gentlefoot Guest

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    It's quite different now :). I went drastic and copied all the code for my forms across into my working example and now I think I've narrowed down the problem.

    If I enter some value into the forename textbox it now passes it over so long as I hit the return button. If however I click the submit button it does not.

    Here is the snippet for the submit button:

    <form id="form5" name="form5" method="post" action="ProcessEnquiry.php">
    <label>
    <input type="submit" name="Submit" id="Submit" value="Submit" />
    </label>
    </form>

    And here is the snippet from the text box:

    <form action="ProcessEnquiry.php" method="post">
    <input type="text" name="forename" />
    <br />
    </form>

    So my question now is this. How do I get the submit button to pass the value of forename across in the same way that the return button does?

    Here is the whole lot as it is in the page:

    <div id="apDiv11">
    <form id="form5" name="form5" method="post" action="ProcessEnquiry.php">
    <label>
    <input type="submit" name="Submit" id="Submit" value="Submit" />
    </label>
    </form>
    </div>
    <div class="style1" id="apDiv1">Please Fill in this enquiry form and a representative will contact you</div>
    <div id="apDiv3">
    <form action="ProcessEnquiry.php" method="post">
    <input type="text" name="forename" />
    <br />
    </form>
    </div>

    Thanks for your help so far by the way :)

    GF
     
    gentlefoot, Jul 7, 2007 IP
  11. gentlefoot

    gentlefoot Guest

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    btw - now I can't stop the David Bowie song going through my head so I'm off to get some vino :)

    ...and we just absolute beginners....
     
    gentlefoot, Jul 7, 2007 IP
  12. gentlefoot

    gentlefoot Guest

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    OK after a bit of testing I have found that the submit button only passes the value over if it is part of the same form. My problem is I am using dreamweaver to create the textboxes and button and it adds them in as separate forms. So how do I get round this problem?

    If I create 1 ApDiv and then add a textbox and a form in together then it works fine but I want them positioned nicely. Any ideas?
     
    gentlefoot, Jul 7, 2007 IP
  13. gentlefoot

    gentlefoot Guest

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    <title>EnquiryForm</title>
    <style type="text/css">
    <!--
    body,td,th {
    font-family: Tahoma;
    font-size: 14px;
    color: #FFFFFF;
    }
    body {
    background-color: #000000;
    }
    #apDiv11 { position:absolute;
    left:75px;
    top:420px;
    width:59px;
    height:41px;
    z-index:11;
    }
    .style1 {font-size: 16px}
    #apDiv1 { position:absolute;
    left:227px;
    top:60px;
    width:484px;
    height:25px;
    z-index:1;
    }
    #apDiv3 { position:absolute;
    left:134px;
    top:146px;
    width:158px;
    height:39px;
    z-index:3;
    background-color: #000000;
    }
    #apDiv2 { position:absolute;
    left:69px;
    top:151px;
    width:63px;
    height:22px;
    z-index:2;
    }
    #apDiv10 { position:absolute;
    left:73px;
    top:314px;
    width:294px;
    height:110px;
    z-index:10;
    }
    #apDiv9 { position:absolute;
    left:73px;
    top:295px;
    width:178px;
    height:22px;
    z-index:9;
    }
    #apDiv6 { position:absolute;
    left:135px;
    top:189px;
    width:170px;
    height:48px;
    z-index:6;
    }
    #apDiv8 { position:absolute;
    left:136px;
    top:242px;
    width:158px;
    height:78px;
    z-index:8;
    }
    #apDiv7 { position:absolute;
    left:73px;
    top:240px;
    width:56px;
    height:36px;
    z-index:7;
    }
    #apDiv4 { position:absolute;
    left:76px;
    top:191px;
    width:51px;
    height:24px;
    z-index:4;
    }
    #apDiv5 {
    position:absolute;
    left:511px;
    top:177px;
    width:379px;
    height:269px;
    z-index:12;
    }
    #apDiv12 {
    position:absolute;
    left:493px;
    top:169px;
    width:318px;
    height:126px;
    z-index:12;
    }
    -->
    </style></head>
    <body>
    <div id="apDiv11">
    <form id="form5" name="form5" method="post" action="ProcessEnquiry.php">
    <label>
    <input type="submit" name="Submit" id="Submit" value="Submit" />
    </label>
    </form>
    </div>
    <div class="style1" id="apDiv1">Please Fill in this enquiry form and a representative will contact you</div>
    <div id="apDiv3">
    <form action="ProcessEnquiry.php" method="post">
    <input type="text" name="forename" />
    <br />
    </form>
    </div>
    <div id="apDiv2">Forename:</div>
    <div id="apDiv10">
    <form id="form2" name="form2" method="post" action="">
    <label>
    <textarea name="Enquiry" id="Enquiry" cols="45" rows="5"></textarea>
    </label>
    </form>
    </div>
    <div id="apDiv9">Please enter your enquiry here:</div>
    <div id="apDiv6">
    <form id="form3" name="form3" method="post" action="">
    <label></label>
    <label>
    <input name="radio" type="radio" id="UK" onclick="MM_showHideLayers('apDiv7','','show','apDiv8','','show')" value="UK" checked="checked" />
    In UK</label>
    <label></label>
    <label>
    <input name="radio" type="radio" id="OutsideUK" onclick="MM_showHideLayers('apDiv7','','hide','apDiv8','','hide')" value="OutsideUK" />
    Outside UK</label>
    </form>
    </div>
    <div id="apDiv8">
    <form id="form4" name="form4" method="post" action="">
    <label>
    <select name="County" size="1" id="County">
    <option>Surrey</option>
    <option>East Sussex</option>
    <option>West Sussex</option>
    <option>Devon</option>
    <option>Someset</option>
    <option>Nottinghamshire</option>
    <option>Bedfordshire</option>
    </select>
    </label>
    </form>
    </div>
    <div id="apDiv7">County:</div>
    <div id="apDiv4">Country:</div>
    </body>
    </html>
     
    gentlefoot, Jul 7, 2007 IP
  14. ansi

    ansi Well-Known Member

    Messages:
    1,483
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    100
    #14
    well, seeing as how a submit button submits the parent form, it makes perfect sense. i would suggest actually learning html instead of just clicking around in dreamweaver and hoping that whatever you come up with works. as for the code... i just got up and it would help a ton if you pasted it at www.pastebin.ca or something. they highlight and include indentation :) or just use the html bbcode. [html] ... [/html]
     
    ansi, Jul 7, 2007 IP
  15. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Yup, thats what my post fixed.

    To get around that just don't close the <form> tag until the last item.

    Or if you are really desperate you can use javascript.
     
    MMJ, Jul 7, 2007 IP