Forum Moderators: coopster

Message Too Old, No Replies

automatically going to another url after submitting a form

         

guzzi

4:06 pm on Apr 28, 2004 (gmt 0)

10+ Year Member



I am designing a php website in DW (what a pain in the @$$), i have created a form, and when the user clicks submit i want the data to be verified, a sql query to be run and then the resulting info must be passed to the next page (eg thank_you_for_registering.php?userID=6) automatically ie i don't have to click a link to execute the php script or go to the next page

charlier

4:34 pm on Apr 28, 2004 (gmt 0)

10+ Year Member



I'm not quite clear on what you mean. If you mean you want the thank you page displayed for some amount of time and then go automatically to another page then you can put a meta-refresh tag in the thank you page. If you just want to do the script actions (verify, sql etc) then that is just a regular form action page, ie in the form tag have
action=/myformpage.php and in myfrompage.php you have

<? some php script commands to verify etc
?>

<HTML>
the page to be shown
</HTML>

coopster

4:47 pm on Apr 28, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, guzzi!

It goes something along these lines...

if (isset [php.net]($_POST [php.net]['submit'])) { 
if (verify_data()) { // data is valid
$sql = "your SQL statement here";
mysql_query($sql); // execute the query, in this case the db is MySQL
header [php.net]("Location: thank_you_for_registering.php?userID=" . $_POST['userID']);
exit [php.net];
} else {
// do something if the data is invalid!
}
}

I've shown an example where

verify_data()
is a user-defined function [php.net] but you could edit your code right inside the
if
control structure [php.net] if you prefer.

guzzi

6:22 am on Apr 29, 2004 (gmt 0)

10+ Year Member



thanx coopster, i'll try this code i'll let you know how it goes...
:)

guzzi

3:53 pm on May 7, 2004 (gmt 0)

10+ Year Member



sorry using a header doesn't seem to work, i get an error message saying that the header information has already been sent, but i did find the solution however for anyone who needs help with this topic in future: the answer is to use a javascript as follows:

<?php //some code here
?>
<script language = "JavaScript">
parent.location = "my_next_page.php";
</script>
<?php //carry on with your php code here
?>

coopster

4:20 pm on May 7, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



sorry using a header doesn't seem to work, i get an error message saying that the header information has already been sent


Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include(), or require(), functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

<html> 
<?php
/* This will give an error. Note the output
* above, which is before the header() call */
header('Location: [example.com...]
?>

Resource:
PHP Manual: header() function [php.net]

Although your javascript solution may work, are you sure that your intended audience will allow javascript in their browser?