Ltj_bukem Posted October 11, 2007 Share Posted October 11, 2007 Hi, I have a page with two seperate forms. When submit is pressed on either it always directs to the 'action' of the first form.Obviously this is far from ideal so I've done the following to direct them accordingly, both forms now go to this page. <?php if (isset($_POST['login'])) {header('Location: http://localhost/htdocs/sheets.php' ) ;} else if(isset($_POST['go'])) {header('Location: http://localhost/htdocs/sheets.php') ;} ?> { This works to some degree in that the forms now go to the pages they are meant to, however the $_POST variable values don't get sent to the new pages, which ruins the whole script. Has anyone got any ideas to get around this? Cheers Quote Link to comment https://forums.phpfreaks.com/topic/72806-form-trouble-with-_post/ Share on other sites More sharing options...
wildteen88 Posted October 11, 2007 Share Posted October 11, 2007 When you use header you redirect the user to sheets.php. All POST/GET data from the page you was redirected from will be lost. You'll probably want to use include instead. Also you may aswell use a basic if statement if you want the user to go to sheets.php if $_POST['login'] or $_POST['go'] exists: if(isset($_POST['login']) || isset($_POST['go'])) { include 'sheets.php'; } else { // code here to be executed if $_POST['login'] and $_POST['go'] doesn't exist } Quote Link to comment https://forums.phpfreaks.com/topic/72806-form-trouble-with-_post/#findComment-367249 Share on other sites More sharing options...
Ltj_bukem Posted October 11, 2007 Author Share Posted October 11, 2007 Cheers for that. Sorry, my code is incorrect, the two submit buttons should go to different pages. <?php if (isset($_POST['login'])) {header('Location: http://localhost/htdocs/index.php' ) ;} else if(isset($_POST['go'])) {header('Location: http://localhost/htdocs/sheets.php') ;} ?> { I guess I could use include & create another page, I was trying to keep extra pages to a minimum. Quote Link to comment https://forums.phpfreaks.com/topic/72806-form-trouble-with-_post/#findComment-367271 Share on other sites More sharing options...
kenrbnsn Posted October 11, 2007 Share Posted October 11, 2007 Can you post the source of the form? Ken Quote Link to comment https://forums.phpfreaks.com/topic/72806-form-trouble-with-_post/#findComment-367318 Share on other sites More sharing options...
kenrbnsn Posted October 11, 2007 Share Posted October 11, 2007 I just did a quick test. The following script has two forms which go to two different scripts: <?php header("Cache-control: private"); ?> <html> <head> <title> Title </title> </head> <body> <form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>"> <input name="which" type="hidden" value="form 1"> <select name="bar[]" multiple> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> </select> <input type="submit" name="submit1" value="Submit Test Form1" /> </form><br> <form method="post" action="form_test2.php"> <input name="which" type="hidden" value="form 2"> <select name="bar[]" multiple> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> </select> <input type="submit" name="submit2" value="Submit Test Form2" /> </form><br> <?php echo '<pre>' . print_r($_POST,true) . '</pre>'; ?> </body> </html> Ken Quote Link to comment https://forums.phpfreaks.com/topic/72806-form-trouble-with-_post/#findComment-367328 Share on other sites More sharing options...
Ltj_bukem Posted October 11, 2007 Author Share Posted October 11, 2007 That does indeed work ken,thanks. My form is somewhat different in that one is a login form that I want to update the 'index' page (i.e main page) and the other is a search form that I links to another page. Te reason I wanted to keep the $_POST info is for the $_SESSION variable, to maintain logged in / out information. Here's the script to maintain logged in info if (isset($_SESSION['valid_user'])) { echo 'Logged in as '.$_SESSION['valid_user'].'.'; echo '<br />'; } else { display_login_form(); } Here's the login form <form method='post' action='index.php'> <td>Username:</td> <td><input type='text' name='username'></td></tr> <tr> <td>Password:</td> <td><input type='password' name='passwd'></td></tr> <tr> <td> <input type='submit'name ='login' value='Log In'></td></tr> Here's the search form <form method='post' action='direct.php'> <td>Tune Search:</td> <td><input type='text' name='search'></td> </td> input type='submit'name = "go" value='Go!'></td> Many thanks Quote Link to comment https://forums.phpfreaks.com/topic/72806-form-trouble-with-_post/#findComment-367365 Share on other sites More sharing options...
kenrbnsn Posted October 11, 2007 Share Posted October 11, 2007 Do you end the first form before you start the second? If you don't, then that's your problem. Ken Quote Link to comment https://forums.phpfreaks.com/topic/72806-form-trouble-with-_post/#findComment-367413 Share on other sites More sharing options...
hamza Posted October 11, 2007 Share Posted October 11, 2007 Listen i think you want to redirect to the page right. its quite simple write the same code just when you submit the form from first page just pass the one hidden value.like if($_POST['hidden_value']==1 ) //checking form is submitted.or not { if (isset($_POST['login'])) { header('Location: sheets.php' ) else if(isset($_POST['go'])) { header('Location: sheets.php') } else {} } NOTE : there is no need to mention the local server name like you have use the localhost and http dont use this and try the above code. And increase the output buffer size and you will got your answer . take care buddy} Quote Link to comment https://forums.phpfreaks.com/topic/72806-form-trouble-with-_post/#findComment-367426 Share on other sites More sharing options...
prime Posted October 11, 2007 Share Posted October 11, 2007 try this <?php if (isset($_POST['login'])){ header('Location: http://localhost/htdocs/index.php' ) ; exit();} else if(isset($_POST['go'])){ header('Location: http://localhost/htdocs/sheets.php') ; exit(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/72806-form-trouble-with-_post/#findComment-367431 Share on other sites More sharing options...
roopurt18 Posted October 11, 2007 Share Posted October 11, 2007 Stop trying to tell the OP to fix this with calls to header(), kenrbnsn has already pointed out the source of the problem. Quote Link to comment https://forums.phpfreaks.com/topic/72806-form-trouble-with-_post/#findComment-367434 Share on other sites More sharing options...
prime Posted October 11, 2007 Share Posted October 11, 2007 there was nothing wrong with his original code............. the only problem was that he didn't exit before he went to the elseif. How he wants to handle it is his decision, not yours, don't tell me not what to say Quote Link to comment https://forums.phpfreaks.com/topic/72806-form-trouble-with-_post/#findComment-367453 Share on other sites More sharing options...
littledragon Posted October 11, 2007 Share Posted October 11, 2007 That does indeed work ken,thanks. My form is somewhat different in that one is a login form that I want to update the 'index' page (i.e main page) and the other is a search form that I links to another page. Te reason I wanted to keep the $_POST info is for the $_SESSION variable, to maintain logged in / out information. I'm not sure I understand ltj .. do you want to post data to two different locations at once? Quote Link to comment https://forums.phpfreaks.com/topic/72806-form-trouble-with-_post/#findComment-367481 Share on other sites More sharing options...
roopurt18 Posted October 12, 2007 Share Posted October 12, 2007 there was nothing wrong with his original code............. the only problem was that he didn't exit before he went to the elseif. How he wants to handle it is his decision, not yours, don't tell me not what to say I don't even know why I'm taking the time to respond to this, but oh wells. You're right, there's nothing wrong with using header() to redirect to a different page. But since the OP is wanting two forms on the same page to each direct to a different page, which is a perfectly valid thing to do, it would make sense to figure out what is wrong with the HTML. The benefits of this include helping the OP have a page that validates as well as teaching them to be more careful when writing their HTML. Plus, the solution is simpler and more direct. Place the proper closing <form> in the proper place and you're done, the problem is solved. If you go with the PHP band-aid of using the header method, you're left with an HTML page that doesn't validate. Then you have to go to the trouble of setting some sort of $_SESSION variable so the $_POST data will be available on the next page. Bleh. When giving someone advice you should always strive to give them the correct solution to the problem, rather than leading them down some convoluted path. If you came to these forums and asked how to import a CSV file into MySQL and cumulatively you and I spent 4 hours and 15 posts going over a PHP script to open the file, parse it, generate SQL, and then execute it, that would be fine. But then someone else comes along and says, "Hey, MySQL has a built in query for this: http://dev.mysql.com/doc/refman/5.0/en/load-data.html" You'd have wished they'd come along sooner and helped you. But you're right, it was rude of me to tell you what not to say. I won't let that happen again. Ltj_bukem, stop listening to anyone telling you to fix this particular problem with header(); ken's advice is much more preferable. If you still haven't got it sorted, post the HTML source that your script is generating. Quote Link to comment https://forums.phpfreaks.com/topic/72806-form-trouble-with-_post/#findComment-367618 Share on other sites More sharing options...
prime Posted October 12, 2007 Share Posted October 12, 2007 I prefer to handle forms as well as display the form on a single page myself, as its a lot more portable and easier to maintain, so yes I agree with your logic. The only time I'll bounce off a file like that is if I want one funtion or such to be available to all the pages in a site then it'll save space then I'll usualy bounce back to the http referer usualy. What I had a problem with was the way you worded the reply, I was posting in direct reply to his problem with the script, as that way he knows what he forgot in his script. Which isn't it importtant for people to know where they went wrong? Anyhow I also apologise for getting confrontational here. Quote Link to comment https://forums.phpfreaks.com/topic/72806-form-trouble-with-_post/#findComment-367628 Share on other sites More sharing options...
roopurt18 Posted October 12, 2007 Share Posted October 12, 2007 Which isn't it importtant for people to know where they went wrong? Good point! Anyways, no harm no foul. Quote Link to comment https://forums.phpfreaks.com/topic/72806-form-trouble-with-_post/#findComment-367634 Share on other sites More sharing options...
Ltj_bukem Posted October 12, 2007 Author Share Posted October 12, 2007 Thanks guys, some usefu linfo there. kenrbnsn had the correct solution in the the first form didn't end properly, html error really. If anyone has a solution to the following it would be great. The login form is displayed on all pages, I'd like to have the submit button direct to the current page it is on (i.e this_page) so it works for any page. Quote Link to comment https://forums.phpfreaks.com/topic/72806-form-trouble-with-_post/#findComment-367696 Share on other sites More sharing options...
Ltj_bukem Posted October 12, 2007 Author Share Posted October 12, 2007 this can be done by replacing action <form method='post' action='whateverpage.php'> with <form method="post"> To update the current page Quote Link to comment https://forums.phpfreaks.com/topic/72806-form-trouble-with-_post/#findComment-367710 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.