-1

I have a form with two fields name & email & i need to submit that form values on specific url, like below

http://www.abc.co.uk/acy/xyzerequest&name='me'&email='[email protected]'

for this i made the form & post it to php file.. where i am getting the value by using $_POST but where i feel stuck is how can i send these values to the specific URL, but user should not know.

So the end user just enter name & his email id & just directed to that URL with his values..

Kindly guide me how can i achieve..I was trying from last 3 hours & no good luck.

7
  • using POST as the form method will hide the variables from the url, you'll only see the variables in the url (like http://www.abc.co.uk/acy/xyzerequest&name='me'&email='[email protected]') if you use GET as the form method
    – Stu
    Jan 7, 2013 at 13:38
  • Your question is not clear. Can please just elaborate it a little more Jan 7, 2013 at 13:38
  • You can use header("location:your_url");
    – sandip
    Jan 7, 2013 at 13:39
  • @Stu Cool i tried get method but that php file taking that field values..from that php file how can redirect.. in html form in action i use action="myphp"
    – mobi001
    Jan 7, 2013 at 13:42
  • @sandip is location allow me to pass the url with the variable values??
    – mobi001
    Jan 7, 2013 at 13:44

6 Answers 6

0

As I understand it, you want your script to collect user's name and email, create a URL and redirect the user.

So the script should look something like this (this is just a guide line).

    <?php
    if(isset($_POST['submit'])){
        $name = $_POST["name"];
        $email = $_POST["email"];

        //create the URL
        $url = "http://www.example.com/yourpage.php?name=" . urlencode($name) . "&email=" . urlencode($email);

        //send the header
        header("Location: " . $url);
        //exit the script
        exit();
    }
    ?>
    <html>
      <body>
        <form method="post" action="" name="name">
          Name:<input type="text" name="name">
          Email:<input type="text" name="name">
          <input type="submit">
        </form>
      </body>
    </html>

As far as the URL goes, the ? denotes where your "variables" start.

You should never create a URL from raw user data, especially if you are sending them to one of your other scripts. You should always use urlencode($data), this will make the data URL safe. You should also look into data validation and scrubbing.

So what this script does is first check to see if anything has been posted to it, and if so, if collects the data, creates the URL, sends the header for redirection, and exits. If not, it presents a form for the user to enter their info.

0

You redirect to another page by setting the Location: header, using the header function. An example:

header("Location: http://stackoverflow.com/");

As @lampwins pointed out, usually this is used together with exit. So that the rest of the page is not executed (because that might cause unusual side-effects, and unnecessary server load).

1
  • Adding and exit() to the line will exit the script at this point and the client will hopefully redirect. header("Location: http://stackoverflow.com/") and exit();`
    – lampwins
    Jan 7, 2013 at 13:50
0

As others have said your question is not very clear.

If i understand it right, you want that url to appear when the user clicks on the submit button.

In that case you should use GET and not post for the form. Note that if you are passing information such as passwords it is not advisable to use GET as the information would be directly visible on the url.

Also you should not use get it the result of the above action is the modification of some resource on the server side, you get only to view and not modify resources.

EDIT: it seems that others have had a better understanding of the question, the header("Location: someurl") with the parameters appended seems the way to go.

0

One way to do this is to use the form as such as form.php to display your form and then redirect to itself to do the processing of that form instead of action=myphp just put the form.php and it will redirect to itself then do what you need at the top. This way you will not need to send the password in the URL.

<?php 
if(isset($_POST['submit'])) {
   $name  = $_POST['name'];
   $email = $_POST['email'];

   // do here whatever you wanted to do with the user and email in your form
   // you can show a different html page or redirect on, or login or whatever.
}  else {
   // the form html elements will only show if the form is not already submitted by the user.  This allows you to do what you need to do on one page without processing and then redirecting using the header.
?>
<!-- form.php -->
<form method=post action="form.php">
name <input name="name" value="" size="10" /><br />
email <input name="email" value="" size="25" /><br />
<input type="submit" name="submit" value="go" />
</form>
<?php
   }
?>
0
0

As some people already suggested, easiest way to do this ( if you need to redirect witht post data ), is to create a hidden form and submit it automatically:

$form = "<form name='autoForm' method='post' action='http://www.abc.co.uk/acy/xyzerequest'>
             <input type='hidden' name='name' value='{$_POST['name']}' />
             <input type='hidden' name='email' value='{$_POST['email']}' />
             <script type='text/javascript' language='javascript'>
                 document.autoForm.submit();
             </script>
         </form>";

echo $form;

This will automaticaaly create your form with the post data and submit it.

0

Let me clear up things,

  1. You get two values(name, email) in your submit.
  2. Now you are going to process these two in another url.

If i am right, you can either use PHP "header" function like

<?php
    header("Location:http://www.abc.co.uk/acy/xyzerequest?name=".$_POST['name']."&email=".$_POST['email']);
?>

In this case, a blank response with header value send to the client or browser as

<some header value>
Contet-Type:text/plain
Location:http://www.abc.co.uk/acy/xyzerequest?name=foo@[email protected]
<some header value>

so that, Those name and email values know by every one.

Since PHP is looses the variable scope at the end of rendering page to client. So storing the values in variable is useless. But you have another option, By using $_SESSION variable you achieve this.

  1. On form submit set session value as follows,

    <?php $_SESSION['name'] = $_POST['name']; $_SESSION['email'] = $_POST['email']; ?>

then redirect to specific url as,

<?php
  header("Location:http://www.abc.co.uk/acy/xyzerequest");
?>

2. Now in handler of redirected url, you can read those values from the session,

<?php
  $name = $_SESSION['name'];
  $email = $_SESSION['email'];
?>

So that, The values are hidden and we can use different page handler.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.