Forum Moderators: coopster

Message Too Old, No Replies

form post force to not reload web?

         

CodilX

10:08 am on Oct 25, 2007 (gmt 0)

10+ Year Member



Hi there,

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?

Habtom

10:13 am on Oct 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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

CodilX

10:22 am on Oct 25, 2007 (gmt 0)

10+ Year Member



<form action="<? echo $_SERVER['PHP_SELF'];?>" method="post">
<input class="input" type="text" name="email" size="26" value="">
<input type="submit" name="submit" class="send" value="Send">

</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);
}
?>

Habtom

11:14 am on Oct 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey here are a few suggestions:

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

CodilX

7:26 pm on Oct 25, 2007 (gmt 0)

10+ Year Member



Thx, but I can't set a certain url, because I use the form as an include(), due to the amount of pages in my website. If I wanted to change the thank you message or whatever, it would be a pain changing like 40 php pages.

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.

CodilX

10:37 am on Oct 27, 2007 (gmt 0)

10+ Year Member



anyone?

Habtom

5:10 am on Oct 28, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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

jblifestyles

2:09 am on Oct 29, 2007 (gmt 0)

10+ Year Member



I'm not necessarily following exactly what you want.. but I think I may be able to help
try using this

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

instead of

<form action="<? echo $_SERVER['PHP_SELF'];?>" method="post">

so it will refresh in the same page.

You can also add a hidden value to your form


<input type="hidden" name="_form_submitted" value="1"/>

and check for the value with

<?php
if (array_key_exists('_form_submitted', $_POST)) {
echo("Form has been added!");
}
?>

to return a message... You can obviously customize the message echo'd in the if statement to recall variables for something like Thank You $firstname $lastname...etc...

alternatively, you can direct to a specific page by doing this


<form action="<? echo $_SERVER['PHP_SELF'];?>[b][i]/pagetodirect[/i][/b]"

the $_SERVER directs it back to your site index page.. you can add the /anything to direct to any other existing page on your site.

[edited by: jatar_k at 12:35 pm (utc) on Oct. 29, 2007]
[edit reason] no specifics thanks [/edit]

CodilX

3:10 pm on Nov 1, 2007 (gmt 0)

10+ Year Member



thanks alot :) I haven't had the time to test it out, but thanks ;)

PHP_Chimp

5:17 pm on Nov 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$_SERVER variables
'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.


So you are getting redirected to the index page, as the query string is not attached on the end of the PHP_SELF. You could use the $_SERVER['QUERY_STRING'] to give you the same page.
So redirect to the same page would be -
<form action="<?php echo $_SERVER['PHP_SELF'].$_SERVER['QUERY_STRING']?>" method="post" >

CodilX

7:43 pm on Nov 1, 2007 (gmt 0)

10+ Year Member



wow thanks this would be the best option for me, but using this post method my post target looks like this: index.phpid=page, without the "?"

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]