Jump to content


Photo

not redirecting?


  • Please log in to reply
14 replies to this topic

#1 localhost

localhost
  • Members
  • PipPipPip
  • Advanced Member
  • 152 posts

Posted 21 June 2006 - 09:23 PM

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:
<?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>





#2 Caesar

Caesar
  • Members
  • PipPipPip
  • Advanced Member
  • 1,025 posts

Posted 21 June 2006 - 09:46 PM

This:

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

Needs to be more like this:

<form  method="post" action="index.php">

...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:

<form  method="post" action="<?php $PHP_SELF ?>">

PHP Ninja

#3 localhost

localhost
  • Members
  • PipPipPip
  • Advanced Member
  • 152 posts

Posted 21 June 2006 - 09:48 PM

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

#4 Caesar

Caesar
  • Members
  • PipPipPip
  • Advanced Member
  • 1,025 posts

Posted 21 June 2006 - 09:56 PM

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

<input type="hidden" name="myform" value="1">

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

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

//--Code that needs to be executed here

}

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.
PHP Ninja

#5 localhost

localhost
  • Members
  • PipPipPip
  • Advanced Member
  • 152 posts

Posted 21 June 2006 - 10:03 PM

hmm...this line

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

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

#6 Caesar

Caesar
  • Members
  • PipPipPip
  • Advanced Member
  • 1,025 posts

Posted 21 June 2006 - 10:05 PM

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

<input type="hidden" name="myform" value="1">

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

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

//--Code that needs to be executed here

}

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.

PHP Ninja

#7 localhost

localhost
  • Members
  • PipPipPip
  • Advanced Member
  • 152 posts

Posted 21 June 2006 - 10:22 PM

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.



#8 Caesar

Caesar
  • Members
  • PipPipPip
  • Advanced Member
  • 1,025 posts

Posted 21 June 2006 - 10:36 PM

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.
PHP Ninja

#9 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 21 June 2006 - 10:38 PM

Just add...
header("Location : index.php");
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.

#10 localhost

localhost
  • Members
  • PipPipPip
  • Advanced Member
  • 152 posts

Posted 21 June 2006 - 10:52 PM

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]

#11 Caesar

Caesar
  • Members
  • PipPipPip
  • Advanced Member
  • 1,025 posts

Posted 21 June 2006 - 11:07 PM

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?
PHP Ninja

#12 localhost

localhost
  • Members
  • PipPipPip
  • Advanced Member
  • 152 posts

Posted 21 June 2006 - 11:10 PM

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.

#13 Caesar

Caesar
  • Members
  • PipPipPip
  • Advanced Member
  • 1,025 posts

Posted 21 June 2006 - 11:17 PM

Try this:

<?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');

?>

PHP Ninja

#14 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 21 June 2006 - 11:19 PM

Your code is failry problematic. Use this as an example...
<?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');
    }

?>


#15 High_-_Tek

High_-_Tek
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts

Posted 22 June 2006 - 02:57 AM

Or you could make it as painless as possible and...

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users