I have many pages which has the same form so I made a page called contactsGo.php and all the forms on the other pages should send to this page what I need is to redirect from contactsGo.php to each page sent the form

this is my code

<?php
    $name=$_POST['name'];
    $email=$_POST['email'];
    $phone=$_post['phone'];
    $company=$_POST['company'];
    $message=$_POST['message'];

    if(isset($_GET['host'])){$host=$_GET['host'];}
    if(isset($_GET['uri'])){$uri=$_GET['uri'];}

    $recipient='info@siliconerubber-rtv2.com';
    $subject="Contact us"; 

        $content = "New Message Form Contact us\n From: ".$name.",\n Email: ".$email.",\n Phone: ".$phone.",\n Company: ".$company.",\n Message: ".$message;
        $headers = 'From: info@siliconerubber-rtv2.com'."\r\n" .
        'Reply-To: info@siliconerubber-rtv2.com'."\r\n".
        'X-Mailer: PHP/' . phpversion();
        $retval=mail($recipient, $subject, $content, $headers);
        if( $retval == true ){header('Location:'.$host.$uri.'?msg=Message Has been sent successfully');}
        else{header('Location:'.$host.$uri.'?msg=Error sending message please try again later');}
?>

and this is the form code

<form id="form1" name="form1" method="get" action="contactsGo.php?<?php echo $_SERVER['HTTP_HOST']; echo rtrim(dirname($_SERVER['PHP_SELF']), '/\\'); ?>">

Recommended Answers

All 4 Replies

hi you can use something like an identifier that will be send together with the data in contactsGo.php where you will use a simple IF statement to check if the data is from that specific form or not. and base on the IF Statement you can use a simple header redirect

something like

if ($identifier=="from_form1"){
 header("Location: form1.php")
}
else{
header("Location: form2.php")
}

hi ehpratah there is no 'form2.php' it's just the name of the form, I need to get the url of the pages that send this form and redirect to this pages, beside it will be long code if, else I believe there is aother simple way to do that

there are really other way to do it. i just show you a simple solution. can you elavorate what your trying to achieve without using this and this. from what i understand you just wanna know what page send the contact info and when the info is emailed succesfully it will return to the form where the info originated.

Member Avatar for diafol

If you have a reusable form, I suggest that you have a php file just for the form (let's call it form.php for now). The form then has a hidden redirect control:

<form action="process.php" method="post">
    <!--other controls here-->
    <input type="hidden" name="redirect" value="<?php echo (isset($form_redirect)) ? $form_redirect : '';?>" /> 
</form>

IN your pages you can then do...

In contact.php

$form_redirect = "contact.php"; //this could be created dynamically from $_SERVER vars too.
require "includes/form.php";

In sendcomment.php

$form_redirect = "sendcomment.php"; //this could be created dynamically from $_SERVER vars too.
require "includes/form.php";

Alternatively, you could do something like this...

<form action="process.php" method="post">
    <!--other controls here-->
    <input type="hidden" name="redirect" value="<?php echo basename($_SERVER['PHP_SELF']);?>" /> 
</form>

And then just this in every page that needs it...

require "includes/form.php";

As users could spoof your form or use curl to send data including a malicious redirect, you would do well to provide a "whitelist" of allowable redirect values in your process.php:

$redirect = $_POST['redirect'];
$allowed_redirects = array("contact.php","sendcomments.php");

if(in_array($redirect, $allowed_redirects))
{
    ...do processing... then...
    header("Location: $redirect");
    exit;
}else{
    ...bad call...
}
commented: I second this approach +4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.