Forum Moderators: coopster

Message Too Old, No Replies

php form submits when loaded or refreshed

         

aquanev

2:15 am on May 12, 2005 (gmt 0)

10+ Year Member



Hi,
I have a small php contact form that emails the contact info. to me.

<form name="form1" method="post" onSubmit="return formvalidation(this)" action="<?php echo $me;?>" >

I have done the javascript validation which works fine when the form is loaded.
But if I type the url while loading basically an empty form submits and I get a blank email.

How do I prevent the page from submitting when it is being loaded and secondly once I submit the data how do I call another page.
Thanx!

kazecoder

10:24 am on May 12, 2005 (gmt 0)

10+ Year Member



I am not convinced that the code you posted is the issue. Can you post all the code for that page?

As you probably know the onsubmit and action attributes should function until the form is submitted so I am think the problem is elsewhere in the code.

mcibor

3:13 pm on May 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To call another page just put it into action, or redirect after form submit. To redirect you may use headers or javascript.

The form submitting on the load isn't possible with only the code you provided. it may be done mostly by javascript. Maybe you have some onload function?

If you use mozilla it may be nice to show warnings on sending a non ssl data (menu Edit/Preferences->safety/ssl/warnings) In IE I don't know how to turn it on, but there should be a way.

Best regards and welcome to WebmasterWorld!
Michal Cibor

ramoneguru

9:34 pm on May 13, 2005 (gmt 0)

10+ Year Member



Not sure what the problem is exactly but here is something to look at I suppose.

//this is the form name and action
echo '<form name="updatePro_form" action="updateProfile.php" method="POST" onSubmit="return validate_form();" onReset ="return confirm('Are you sure you want to clear the form?')")';

//this is the form textbox
print "<tr><td width=200 align=right id='fname'><font color='red'>*</font>First Name: </td>";
print "<td width=390 align=left><input type=\"text\" name=\"fname\" value=\"$row[FName]\" ></td></tr>\n";

//javascript to check the form
if (document.updatePro_form.fname.value == "")
{
//alert ( "Please fill in the 'First Name'! " );
alert ( "First Name Error! " );
document.getElementById('fname').style.color='red';
document.updatePro_form.fname.focus();
return false ;
}
else
document.getElementById('fname').style.color='black';

Nothing special and it works. I had a similar problem in IE because I was submitting the form by pressing the enter key and not the "submit" button....
--Nick

ramoneguru

5:30 am on May 14, 2005 (gmt 0)

10+ Year Member



Forgot the submit button
echo '<input type="submit" name="submit" value="Submit">';
--Nick

aquanev

6:26 am on May 14, 2005 (gmt 0)

10+ Year Member



Hi,

I'm attaching the code for you to chack it out.

<HTML>
<HEAD>
<TITLE>SEOpartners</TITLE>
<LINK href="text.css" type=text/css rel=stylesheet>
<LINK rel="STYLESHEET" type="text/css" href="/css.css" >
<script language="JavaScript" src="gen_validatorv2.js" type="text/javascript"></script>
</HEAD>

<BODY topmargin="0" leftmargin="0" marginheight="0" marginwidth="0" bgcolor="#FFFFFF">
<br><table border="1" cellspacing="0" width="780" cellpadding="0" align="center" class="GrayBorder">
<tr>
<td width="100%">
<table border="0" cellspacing="0" width="780" cellpadding="0" align="center">
<tr>
<td width="509"><a href="http://www.example.com/offer/" title="SEOpartners">
<img src="./images/logo.jpg" alt="SEOpartners" border="0"></a></td>
<td width="271" align="right" class="small_text1">

<?php include('./pitch.htm');?>

</td>
</tr>
</table>

<table border="0" cellspacing="0" width="780" cellpadding="0">
<tr bgcolor="#CCCCCC">
<td width="60%" height=20 background="/images/bgr_topbar.gif" class=white_nav>
<font color="#CCCCCC">&nbsp; &nbsp; <a class=white_nav href=./offer.php>Home</a>
&nbsp;¦&nbsp; <a class=white_nav href=./overview.php>Overview</a> &nbsp;¦&nbsp;
<a class=white_nav href=./faq.php>FAQ</a> &nbsp;¦&nbsp;
<a class=white_nav href=./contact.php>Contact</a> </font></td>
<td width="40%" height=20 align=right background="/images/bgr_topbar.gif">
&nbsp;
<?php include('./places.htm');?>
</td>
</tr>
</table>

<table border=0 width=100% cellspacing="8" cellpadding="0">
<tr>
<!-- END HEADER -->

<td class=small_text1 valign=top>
<form name="form1" method="post" onSubmit="return formvalidation(this)" action="<?php echo $me;?>" >

<CENTER><p>&nbsp;</p>

<TABLE>
<TR>
<TD colspan="2"><img src="images/green.jpg" height="2" width="100%"></TD>
</TR>
<TR>
<TD colspan="2"><strong><font size="-5">Contact us</font></strong></TD>
</TR>
<TR>
<TD colspan="2"><img src="images/white.jpg" height="10" width="100%"></TD>
</TR>
<TR>
<TD width="144">Name:</TD>
<TD width="174"><input name="name" type="text" size="35"></TD>
</TR>
<TR>
<TD>Email:</TD>
<TD><input name="email" type="text" size="35"></TD>
</TR>
<TR>
<TD>Comment:</TD>
<TD><textarea name="comment" cols="30" rows="7">&nbsp;</textarea></TD>
</TR>
<TR>
<TD colspan="2"><CENTER>
<input name="submit" type="Submit" value="Submit">
</CENTER></TD>
</TR>
</TABLE>
</CENTER>
</form>
<script language="JavaScript">
var frmvalidator = new Validator("form1");
frmvalidator.addValidation("name","req","Please enter your First Name");
frmvalidator.addValidation("email","maxlen=50");
frmvalidator.addValidation("email","req","Please enter you E-mail address");
frmvalidator.addValidation("email","email");
frmvalidator.addValidation("comment","req","Please enter your Comments");
frmvalidator.addValidation("comment","maxlen=100");
</script>

</td>
<?php

$recipient = 'test@gmail.com';
$name = stripslashes($_POST['name']);
$email = stripslashes($_POST['email']);
$comment = stripslashes($_POST['comment']);


$msg = "Client Details:"."<br>\n"."Name: ".$name."&nbsp;".$lname."<br>"."Email: ".$email."<br>"."Comments: ".$comment;


$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

if (mail($recipient, $email, $msg, $headers))
echo nl2br("

");
else
echo "Message failed to send";
//}
?>

<!-- BEGIN FOOTER -->

</tr>
</table>

<table border="0" cellspacing="0" width="780" cellpadding="0">
<tr>
<td width="100%" background="/images/separator01.gif">
<img border="0" src="/images/pix.gif" width="1" height="1"></td>
</tr>

</table>
<table border="0" cellspacing="0" width="780" cellpadding="0">
<tr>
<td width="50%" align="left" class="bottom_nav"> &nbsp; <a class="bottom_nav" href="./overview.php">overview</a>&nbsp;
¦&nbsp; <a class="bottom_nav" href="./faq.php">faq</a>&nbsp; ¦&nbsp;
<a class="bottom_nav" href="./tos.php">terms of service</a>&nbsp; ¦&nbsp;
<a class="bottom_nav" href="./contact.php">contact</a>&nbsp;&nbsp;&nbsp;
</td>

<td width="50%" height="30" class="small_text2" align="right">&nbsp;&nbsp;&nbsp;
Copyright &copy; 2004-2005 <a href="http://www.example.com/">example.com</a>
&nbsp;</td>
</tr>
</table>
</td>
</tr>
</table>

<p class=footer_text align=center>Google and PageRank are trademarks of Google Technology Inc. <br>

example is not associated with Google in any way.
</body>
</html>

[edited by: jatar_k at 3:19 pm (utc) on May 14, 2005]
[edit reason] generalized url [/edit]

mblair

6:40 am on May 14, 2005 (gmt 0)

10+ Year Member



I'm not sure if it's so good to post that source here with the URLs for your site in it as I believe that is against the TOS here.

But it seems to me you need to do some server side validation. Javascript validation is really only good as a convenience for the user. It can't effective guarantee proper input. Robots pretty much don't proccess Javascript and so some of them might trip the script and some people surf the web without Javascript enabled.

If you extend you PHP script to do similiar checks to your Javascript and then only run the mail routine if your checks pass then you should be in good shape.