Forum Moderators: coopster
I have a simple form post script, and I'm in trouble with it.
My form is a newsletter script, when you input an email, it gets regexp checked, then it gets checked in my MySql db, if its not valid the users gets an error, if the email was found in my db the user gets an error that he's already registered, if not he gets registered and the script says thank you :)
my website urls look like the following
www.domain.com/?page=home
www.domain.com/?page=contact
etc..
the problem is that when the form is sent, I get redirected to www.domain.com/index.php and the output from my form is shown there.
Is it possible for the form not to reload the page when I send it? So that the error would just "pop" up?
the problem is that when the form is sent, I get redirected to www.domain.com/index.php and the output from my form is shown there.
You can send the visitor back to the registration back with its old values on the form if the registration is unsuccessful. I think this part has been a problem to you. If this is correct, post the relevant part of the code and you will get help here.
Is it possible for the form not to reload the page when I send it? So that the error would just "pop" up?
I think we are talking about Ajax here.
Habtom
</form>
<?php
if (isset($_POST['submit'])) {
$db_host = '';
$db_user = '';
$db_pass = '';
$db_name = '';
$email = $_POST['email'];
if (!preg_match("/^([a-zA-Z0-9])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/", $email))
{
print ("
Wrong syntax
");
exit();
}
$dbDB = mysql_connect($db_host, $db_user, $db_pass) or die("Error connecting!");
mysql_select_db($db_name) or die("Error connecting!");
$query = "select * from mailing where email like '$email'";
$result=mysql_query($query);
$rows=mysql_num_rows($result);
if ($rows > 0){
print ("
This e-mail is already registered
");
exit();
}
mysql_query("INSERT INTO `stats`.`mailing` (`id`, `email`) VALUES (NULL, '$email')");
print ("Thank You for registering");
$subject ="Newsletter Confirmation ¦ MySite";
$message="
Thank You
.......
.....
";
$mail_from="no-reply@domain.com";
$header="from: MySite <no-reply@domain.com>";
$to ="$email";
$send_contact=mail($to,$subject,$message,$header);
}
?>
if ($rows > 0){
$error = "This e-mail is already registered";
header("Location: myform.php?message=$error");
exit();
}
And after the email is sent, instead of the following line:
print ("Thank You for registering");
Use the following:
$message= "Thank You for registering";
header("Location: thankyoupage.php?message=$message");
exit();
Habtom
Is there a way to incorporate the domain.com/?page=PAGEID so that the mail form would reload that page instead of going from domain.com/?page=pageid to domain.com/index.php?
How about javascript? Is it possible to make it show the text I want on the page without reloading the page at all? And how about AJAX, is it difficult?
I used this form without problems when I had my website like domain.com/home.php; domain.com/contact.php etc, but now I made it more dynamic so I have to use the current method I'm using. It used to just pop-up right there.
Is there a way to incorporate the domain.com/?page=PAGEID so that the mail form would reload that page instead of going from domain.com/?page=pageid to domain.com/index.php?
Yes, how about trying that as you just mentioned. Make your form to submit to the same page. Include the thing you want to see on your URL on the action part of the form. If you find the data to be valid, you can use the parameter you passed to display the page you need to. The if condition can include the following:
if ($_REQUEST['page'] = '1') {
include("this.php"); // or anyother code
} else if ($_REQUEST['page'] = '2') {
include("that.php"); // or anyother code
}
It depends on the code you have, but that could be customized to work properly.
Habtom
<form action="<?php echo $PHP_SELF;?>" method="post" >
<form action="<? echo $_SERVER['PHP_SELF'];?>" method="post">
You can also add a hidden value to your form
<input type="hidden" name="_form_submitted" value="1"/>
<?php
if (array_key_exists('_form_submitted', $_POST)) {
echo("Form has been added!");
}
?>
alternatively, you can direct to a specific page by doing this
<form action="<? echo $_SERVER['PHP_SELF'];?>[b][i]/pagetodirect[/i][/b]"
[edited by: jatar_k at 12:35 pm (utc) on Oct. 29, 2007]
[edit reason] no specifics thanks [/edit]
'PHP_SELF'The filename of the currently executing script, relative to the document root. For instance, $_SERVER['PHP_SELF'] in a script at the address http://example.com/test.php/foo.bar would be /test.php/foo.bar. The __FILE__ constant contains the full path and filename of the current (i.e. included) file.
If PHP is running as a command-line processor this variable contains the script name since PHP 4.3.0. Previously it was not available.
edit:
I fixed it so I there wouldn't be the index.php and added the?
<form action="?<?php echo $_SERVER['QUERY_STRING']?>" method="post">
thanks a bunch mate!
[edited by: CodilX at 7:45 pm (utc) on Nov. 1, 2007]