Forum Moderators: coopster

Message Too Old, No Replies

How to submit and Reset the form?

         

ravii

7:34 am on Dec 27, 2008 (gmt 0)

10+ Year Member



Hello,

I have a form with two text fields and two image buttons Send and Reset
when i click on the Send Button admin receives a mail when user clicks Reset the form should get Reset
but form me it is not working properly
here is my code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<script language="JavaScript" type="text/javascript">
<!--
function reset_form()
{
if ( confirm("Are you sure that you want to reset this form?") )
{
window.document.contact.reset();
}

else
{
window.document.contact.cname.focus();
}
}
//-->
</script>

</head>

<?php
function IsEmpty($cname1,$comp1)
{
$str1=strlen($cname1);
$str2=strlen($comp1);

$nRes=min($str1,$str2);
if ($nRes==0)
{
echo "<font size='3' face='Garamond' color='red'><b>Please enter all the details !.. </b></font> </br>";
}
return $nRes;
}
?>

<body bgcolor="#0a1936">

<div>
<form method="post" action="<?php $_SERVER['PHP_SELF'] ?>" name="contact" id="contact">
<div >
<label style="color:#FFFFFF">Name:</label>
<input style="margin:2px 0px 0px 89px" type="text" name="cname" id="cname" /><br/>

<label style="color:#FFFFFF">Company Name:</label>
<input style="margin:2px 0px 0px 26px" type="text" name="comp" id="comp" /><br/>

<input type="image" src="/hakman_Images/send.JPG" name="send1" id="send1" style="margin:5px 0px 0px 130px"/>

<script language="JavaScript" type="text/javascript">
<!--
window.document.write("<input type='image' src='/hakman_Images/reset.JPG' name='reset_button' id='reset_button' onclick='reset_form()'/>");
//-->
</script>

</div>

<?php
if (!empty($_POST))
{
$cname1=$_POST['cname'];
$comp1=$_POST['comp'];

$check=IsEmpty($cname1,$comp1);
if ($check>0)
{
//error_reporting(E_ALL);
error_reporting(E_STRICT);
date_default_timezone_set('America/Toronto');
include("class.phpmailer.php");
include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$mail = new PHPMailer();
$body =nl2br("<font color='Green'>NAME : </font>" . $_POST['cname']."\n <font color='Green'>COMPANY NAME : </font>" .$_POST['comp']);

$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->Username = "abc@gmail.com";
$mail->Password = "abc";
$mail->From = "xyz@hotmail.com";
$mail->FromName = "xyz site";
$mail->Subject = "Mail from xyz's site";
$mail->WordWrap = 50;
$mail->MsgHTML($body);
$mail->AddAddress("abc@gmail.com", "abc");

if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "<font size='4' face='Monotype Corsiva' color='yellow'><b>Your Message Sent successfully!</b> </font>";
}
}
}
?>
</form>
</div>
</body>
</html>

and in the php code I also tried to use

 if(isset($_POST['send'])) 

instead of

 if (!empty($_POST)) 

but the page does not compile why?

thanks in advance
Swetha

rockerzz501

8:55 am on Dec 29, 2008 (gmt 0)

10+ Year Member



Hi Swetha,
I am not a PHP expert but I think I should make this simple. I can write a script without those javascripts to make more user friendly (some have javascript disabled so it affects the usability)..


<HTML>
<HEAD>
<TITLE>Sample form submission with reset button</TITLE>
</HEAD>
<BODY>
<!-- Input form begin -->
<FORM NAME="test" METHOD=POST ACTION="<? echo $PHP_SELF; ?>">
<INPUT TYPE="HIDDEN" NAME="data" VALUE="submit">
Type your data here:
<BR>
<TEXTAREA CLASS="DEFAULT" NAME="data" ROWS=15 COLS=100></TEXTAREA>
<BR><BR>
<INPUT CLASS="DEFAULT" TYPE=SUBMIT VALUE="SUBMIT">
</FORM>
<!-- Reset form-->
<form action="<? echo $PHP_SELF; ?>" method="POST">
<input type="hidden" name="reset" value="RESET">
<input type="submit" value="RESET"></form
<?php
// Parse incoming information if above form was posted
if($_POST[data] == "submit") {
echo "Place your mail script here";
}
?>
<?php
//reset
if ($_POST[reset] =="RESET"){
echo '<META http-equiv="refresh" content="0;URL=http://localhost/submitreset.php">';
}
?>
</body>
</html>

You can modify the code info above to fit your needs.So everytime the reset button is pressed, it refresh the page and removes all info.I hope I get what you mean.Happy holidays.