Jump to content

Archived

This topic is now archived and is closed to further replies.

localhost

not redirecting?

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]

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
the script itself works...and the action is "" because its that php file i just need to make it so once it submits the form...it redirects them to the index page

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
hmm...this line

if ($_POST[redirect] == 1) {

gives me this error:
Parse error: parse error, unexpected T_STRING, expecting ',' or ';'

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
it wont let me post...it gives me this error even when i use the code tags

An appropriate representation of the requested resource /forums/index.php could not be found on this server.

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
yeah istillg et the erorr trying to PM it...so here is a link to the txt file with it:

[a href=\"http://www.dotomega.com/forums/contact.txt\" target=\"_blank\"]http://www.dotomega.com/forums/contact.txt[/a]

Share this post


Link to post
Share on other sites
Ok...now that I can see your code. I ask this....do you want this form to post to the same file it is in...and then redirect to another page once it's done executing? Is that the goal here?

Share this post


Link to post
Share on other sites
well i want it to post, the insert the data into the database using the INSERT query...and then redirect once its done executing.

that is the goal.

Share this post


Link to post
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]

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
Or you could make it as painless as possible and...

use an onSubmit="window.location('url here')"

Share this post


Link to post
Share on other sites

×

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.