Jump to content

not redirecting?


localhost

Recommended Posts

I need to have this code redirect them to index.php once they click submit, I would like to do this as simple as possible.

CODE:
[code]
<?php



include('global/global_header.php');

    include('ban.php');

include('includes/connect.php');



$name = $_POST['name'];

$email = $_POST['email'];

$subject = $_POST['subject'];

$message = $_POST['message'];



$ipaddress = $_SERVER['REMOTE_ADDR'];

$sentdate = date('m/d/Y');



if($name==NULL || $email==NULL || $subject==NULL || $message==NULL) {

echo ' All fields must be fulfilled ';

} else {



$query = "INSERT INTO contact (`name`, `email`, `subject`, `message`, `ipaddress`, `sentdate`) VALUES ('$name', '$email', '$subject', '$message', '$ipaddress', '$sentdate')";

$result = mysql_query($query) or die('Could not insert details into database');



include('global/global_footer.php');



}



if(isset($_POST['submit'])){



echo "success message";



}



?>



<form action="" method="POST">

Name:<input type="text" name="name" />

<Br />

eMail:<input type="text" name="email" />

<Br />

Subject:<input type="text" name="subject" />

<br />

Message:<input type="text" name="message" />

<br />

<input type="submit" name="submit" value="Contact" />
</form>



[/code]
Link to comment
Share on other sites

This:

[code]<form action="" method="POST">[/code]

Needs to be more like this:

[code]<form  method="post" action="index.php">[/code]

...You have to define where the information in the form is being posted to.

Edit: Wait,....is this form on "index.php" too? If that is the case, you also need to add an if statement so the code at the begining of the page executes only when the form is submitted. In which case, the form tag can actually look like this:

[code]<form  method="post" action="<?php $PHP_SELF ?>">[/code]
Link to comment
Share on other sites

Just an example but, below the submit button in your form, add something like:

[code]<input type="hidden" name="myform" value="1">[/code]

Then in your if statement in your PHP code at the top, add something like:

[code]if ($_POST[myform] == 1) {

//--Code that needs to be executed here

}[/code]

I mean, you can do this diferently....but that should get you going in the right direction. Also...note my edit in the above post I made.
Link to comment
Share on other sites

[!--quoteo(post=386602:date=Jun 21 2006, 04:56 PM:name=Caesar)--][div class=\'quotetop\']QUOTE(Caesar @ Jun 21 2006, 04:56 PM) [snapback]386602[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Just an example but, below the submit button in your form, add something like:

[code]<input type="hidden" name="myform" value="1">[/code]

Then in your if statement in your PHP code at the top, add something like:

[code]if ($_POST[myform] == 1) {

//--Code that needs to be executed here

}[/code]

I mean, you can do this diferently....but that should get you going in the right direction. Also...note my edit in the above post I made.
[/quote]

Show me the entire updated code you have.
Link to comment
Share on other sites

Wow. :-) Try to PM me the code.

Just make sure the "if" stement wraps around the code you need executed. I see you already have one in there, to show a successfull submit. But if this is all on the same PHP file...you only want the code to execute, if the form is submitted. This isn't a question of redirection. And also, I asked whether this was all on the same page/file.
Link to comment
Share on other sites

Just add...
[code]
header("Location : index.php");
[/code]
After your query is executed.
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]But if this is all on the same PHP file...you only want the code to execute, if the form is submitted.[/quote]
Yeah... this code really is a mess.
Link to comment
Share on other sites

Try this:

[code]<?php

if(isset($_POST['submit'])) {

include('global/global_header.php');

    include('ban.php');

include('includes/connect.php');



$name = $_POST['name'];

$email = $_POST['email'];

$subject = $_POST['subject'];

$message = $_POST['message'];



$ipaddress = $_SERVER['REMOTE_ADDR'];

$sentdate = date('m/d/Y');



if($name==NULL || $email==NULL || $subject==NULL || $message==NULL) {

echo ' All fields must be fulfilled ';

} else {



$query = "INSERT INTO contact (`name`, `email`, `subject`, `message`, `ipaddress`, `sentdate`) VALUES ('$name', '$email', '$subject', '$message', '$ipaddress', '$sentdate')";

$result = mysql_query($query) or die('Could not insert details into database');

header("Location: index.php");

}



echo "success message";



}

echo"

<form action=\"$PHP_SELF\" method=\"POST\">

Name:<input type=\"text\" name=\"name\" />

<Br />

eMail:<input type=\"text\" name=\"email\" />

<Br />

Subject:<input type=\"text\" name=\"subject\" />

<br />

Message:<input type=\"text\" name=\"message\" />

<br />

<input type=\"submit\" name=\"submit\" value=\"Contact\" />
</form>";

include('global/global_footer.php');

?>[/code]
Link to comment
Share on other sites

Your code is failry problematic. Use this as an example...
[code]
<?php

    include 'ban.php';

    if (isset($_POST['submit'])) {

        include 'includes/connect.php';  
        $name = (isset($_POST['name'])) ? $_POST['name'] : FALSE;
        $email = (isset($_POST['email'])) ? $_POST['email'] : FALSE;
        $subject = (isset($_POST['subject'])) ? $_POST['subject'] : FALSE;
        $message = (isset($_POST['message'])) ? $_POST['message'] : FALSE;
        $ipaddress = $_SERVER['REMOTE_ADDR'];
        $sentdate = date('m/d/Y');

        if ($name && $email && $subject && $message) {
            $query = "INSERT INTO contact (`name`, `email`, `subject`, `message`, `ipaddress`, `sentdate`) VALUES ('$name', '$e
            if (mysql_query($query)) {
                header("Location: index.php");
        
            } else {
                echo "Could not insert details into database";
        
            }      
    
        } else {
            echo "All fields must be fulfilled";
    
        }      

    } else {
        include 'global/global_header.php';
        echo "  
            <form action=\"\" method=\"POST\">
                Name:<input type=\"text\" name=\"name\" />
                <br />
                eMail:<input type=\"text\" name=\"email\" />
                <br />
                Subject:<input type=\"text\" name=\"subject\" />
                <br />
                Message:<input type=\"text\" name=\"message\" />
                <br />
                <input type=\"submit\" name=\"submit\" value=\"Contact\" />
            </form>";
        include('global/global_footer.php');
    }

?>
[/code]
Link to comment
Share on other sites

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.