Jump to content

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/72806-form-trouble-with-_post/
Share on other sites

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  
}

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.

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

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

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}

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?

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.

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.

 

 

 

 

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.

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.