Click to See Complete Forum and Search --> : Submit Form


vcstarter
September 25th, 2005, 05:46 PM
I don't know much javascript. I do have a form users can fill then submit the information to me. Can someone help me get it right. I want to receive an email containing the information on the form when the user click on the submit button.

You can look at the form from the attachment. I have added a function to handle empty field. The file attached is an html file. I simply renamed it to .txt.

you can rename it to .html and open it to your browser to see the form

gilly914
September 25th, 2005, 06:48 PM
Hey There,

I don't know how, and if it is possible to send mail through Javascript because it is a Client Side language, Although it is very ease sending mail through PHP using the mail() funciton.

PHP is a Server-Side language, thus meaning you can use funcitons and procedures that give direct directions to the server...

Of course the server will need PHP support...

If you need further help, I will be glad to help...

gilly914

Kudose
September 25th, 2005, 08:16 PM
<html>
<head>
<title>My Website</title>
<script language="javascript" type="text/javascript">
<!--

function ValidateForm()
{
if (FirstName.value = "")
{
alert("Please enter your first name");
FirstName.focus();
return(false);
}
if (LanstName.value = "")
{
alert("Please enter your last name");
LastName.focus();
return(false);
}
if (EmailAddress.value = "")
{
alert("Please enter your email address");
EmailAddress.focus();
return(false);
}
if (Comment.value = "")
{
alert("Please enter your comment name");
Comment.focus();
return(false);
}
return (true);
}

// -->
</script>
</head>
<body style="text-align: left">
<span style="font-size: 24pt; color: #0000ff;">Contact Form<br />
</span>
<br />
<?php
if($!_POST['submit']){
?>
<form name="myform" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<strong>First Name:</strong><br />
<input id="FirstName" style="width: 176px" type="text" /><br />
<strong>Last Name:</strong><br />
<input id="LastName" style="font-weight: bold; width: 173px" type="text" /><br />
<strong>Email Address:</strong><br />
<input id="EmailAddress" style="width: 173px" type="text" /><br />
<strong>Subject:</strong><br />
<input id="Subject" style="width: 548px" type="text" /><br />
<strong>Comment:</strong><br />
<textarea id="Comment" style="width: 548px; height: 394px"></textarea><br />
<br />
<input id="submit" type="submit" value="Submit Form" onclick="return ValidateForm();()" style="width: 103px" /><br />
</form>
<?php } else {
$to = "me@you.com";
$subject = "My email message";
$fname = $_POST['FirstName'];
if(!eregi("^[A-Za-z0-9\'\"\x20]+$", $fname))
$bad = 1;
$lname = $_POST['LastName'];
if(!eregi("^[A-Za-z0-9\'\"\x20]+$", $lname))
$bad = 1;
$email = $_POST['EmailAddress'];
if(!eregi("^[-_A-Za-z0-9\.]+@[-A-Za-z0-9\.]+$", $email))
$bad = 1;
$comment = strip_tags($_POST['Comment']);
$message = "First Name: ".$fname."\nLast Name: ".$lname."\ne-Mail Address: ".$email."\n\n";
$message .= $comment;
if($bad !== 1)
if(mail($to, $subject, $message, $from))
echo 'Message sent.';
else
echo 'Could not send message.';
else
echo 'Please use valid form information.';
} ?>
<br />
<br />
</body>
</html>


That should work. You will need to change the $to address though...

vcstarter
September 25th, 2005, 11:07 PM
My hosting company provides the following specification

Windows: ASP/PHP
Windows: ASP.NET

So, I believe I should be able to load php in the server.

So, send me the php script to send mail so I can try it to see if it will work. I have tried the Kudose example, but did not work.

gilly914
September 26th, 2005, 01:02 PM
I think Kudose had a couple of syntax errors that needed to be fixed...
I think that now it should be ok!

If you have any more problems, with or without this specific code, I'll be glad to help...
Just try to be more specific about the problem. Report the error that you are recieving, or what part of it doesn't work.

And note : The file must be saved as a php file to run the PHP, thus naming the file : MyFile.php

Hope I Helped, gilly914

<html>
<head>
<title>My Website</title>
<script language="javascript" type="text/javascript">
<!--
function ValidateForm()
{
if (FirstName.value = "")
{
alert("Please enter your first name");
FirstName.focus();
return(false);
}
if (LanstName.value = "")
{
alert("Please enter your last name");
LastName.focus();
return(false);
}
if (EmailAddress.value = "")
{
alert("Please enter your email address");
EmailAddress.focus();
return(false);
}
if (Comment.value = "")
{
alert("Please enter your comment name");
Comment.focus();
return(false);
}
myform.submit();
}

// -->
</script>
</head>
<body style="text-align: left">
<span style="font-size: 24pt; color: #0000ff;">Contact Form<br>
</span>
<br>
<?php
if(!$_POST['submit']){
?>
<form name="myform" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<strong>First Name:</strong><br>
<input name="FirstName" style="width: 176px" type="text"><br>
<strong>Last Name:</strong><br>
<input name="LastName" style="font-weight: bold; width: 173px" type="text"><br>
<strong>Email Address:</strong><br>
<input name="EmailAddress" style="width: 173px" type="text"><br>
<strong>Subject:</strong><br>
<input name="Subject" style="width: 548px" type="text"><br>
<strong>Comment:</strong><br>
<textarea name="Comment" style="width: 548px; height: 394px"></textarea><br>
<br>
<input name="submit" type="submit" value="Submit Form" onclick="javascript:ValidateForm();" style="width: 103px"><br>
</form>
<?php
} else {
$to = "me@you.com";
$subject = "My email message";
$message = "First Name: ".$_POST['FirstName']."\nLast Name: ".$_POST['LastName']."\ne-Mail Address: ".$_POST['EmailAddress']."\n\n";
$message .= $_POST['Comment'];
$from = "From: Website Customer";
if(mail($to, $subject, $message, $from))
echo "Message sent.";
else
echo "Could not send message.";
}
?>
<br>
<br>
</body>
</html>

vcstarter
September 27th, 2005, 09:04 PM
It doesn't work. I got the following error from the url link

http://www.speaklogic.org/<?php%20echo%20$_SERVER['PHP_SELF'];%20?>

I changed this me@you.com to my address which is info@speaklogic.org

I would appreciate your help if you can fix it.

gilly914
September 28th, 2005, 12:27 AM
It seems as if your server does not support PHP. Maybe the hosting company gives you the option, but they did not install it yet and you must request it specificly.
Or maybe you didn't save the file as *.php

If that's not the problem, then I can't help you anymore with the information I have...

Maybe you should learn about the basic functions and procedures in PHP, and try to fix it yourself.
Sending an email is very simple, but it is hard to fix someone elses bugs...

vcstarter
September 28th, 2005, 10:51 AM
I did not save the file as .php. I will do that when I get home later today and put it on the server. I will let you know later this evening if it works or not.

gilly914
September 28th, 2005, 01:59 PM
then that is probably the reason why it did not work...

If it still doesn't work, then give me the error that you get, or what exactly does not work, and I will try to help you again...

vcstarter
September 28th, 2005, 09:29 PM
I tried it on the server, it seems like the script ran, but it did not work.

I received the following error.



Could not send message.



So it seems like the server does support php since they say they support both php and asp.

this is the over all code. I simply renamed it to contact.php


<html>
<head>
<title>My Website</title>
<script language="javascript" type="text/javascript">
<!--
function ValidateForm()
{
if (FirstName.value = "")
{
alert("Please enter your first name");
FirstName.focus();
return(false);
}
if (LanstName.value = "")
{
alert("Please enter your last name");
LastName.focus();
return(false);
}
if (EmailAddress.value = "")
{
alert("Please enter your email address");
EmailAddress.focus();
return(false);
}
if (Comment.value = "")
{
alert("Please enter your comment name");
Comment.focus();
return(false);
}
myform.submit();
}

// -->
</script>
</head>
<body style="text-align: left">
<span style="font-size: 24pt; color: #0000ff;">Contact Form<br>
</span>
<br>
<?php
if(!$_POST['submit']){
?>
<form name="myform" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<strong>First Name:</strong><br>
<input name="FirstName" style="width: 176px" type="text"><br>
<strong>Last Name:</strong><br>
<input name="LastName" style="font-weight: bold; width: 173px" type="text"><br>
<strong>Email Address:</strong><br>
<input name="EmailAddress" style="width: 173px" type="text"><br>
<strong>Subject:</strong><br>
<input name="Subject" style="width: 548px" type="text"><br>
<strong>Comment:</strong><br>
<textarea name="Comment" style="width: 548px; height: 394px"></textarea><br>
<br>
<input name="submit" type="submit" value="Submit Form" onclick="javascript:ValidateForm();" style="width: 103px"><br>
</form>
<?php
} else {
$to = "info@speaklogic.org";
$subject = "My email message";
$message = "First Name: ".$_POST['FirstName']."\nLast Name: ".$_POST['LastName']."\ne-Mail Address: ".$_POST['EmailAddress']."\n\n";
$message .= $_POST['Comment'];
$from = "From: Website Customer";
if(mail($to, $subject, $message, $from))
echo "Message sent.";
else
echo "Could not send message.";
}
?>
<br>
<br>
</body>
</html>

PeejAvery
September 28th, 2005, 10:16 PM
Try this...

Put this in contact.php...
<html>
<title>Contact</title>

<?php

$fn = @$_POST['fn'];
$ln = @$_POST['ln'];
$email = @$_POST['addr'];
$sub = @$_POST['sub'];
$com = @$_POST['com'];

$mailto = "youremailaddress@domain.com";
if($fn<>"" and $ln<>"" and $email<>"" and $com<>""){

$message = "$fn $ln\n$email\n--\n$com";

if(mail($mailto, $sub, $message)){
echo "Message sent.";
}
else{
echo "Message not sent.";
}

}

?>

<script language="javascript">
function checkform(){
var x;
x = 0
if(info.fn.value==""){
window.alert("Please enter your first name");
x = x + 1;
info.fn.focus();
}
if(info.ln.value==""){
window.alert("Please enter your last name");
x = x + 1;
info.ln.focus();
}
if(info.addr.value==""){
window.alert("Please enter your email address");
x = x + 1;
info.addr.focus();
}
if(info.com.value==""){
window.alert("Please enter your comment name");
x = x + 1;
info.com.focus();
}

if(x==0){document.info.submit()}
}
</script>

<body style="text-align: left">
<form name="info" action="mailer.php" method="post">
<span style="font-size: 24pt; color: #0000ff;">Contact Form<br></span><br>
<strong>First Name:</strong><br>
<input name="fn" style="width: 176px" type="text"><br>
<strong>Last Name:</strong><br>
<input name="ln" style="font-weight: bold; width: 173px" type="text"><br>
<strong>Email Address:</strong><br>
<input name="addr" style="width: 173px" type="text"><br>
<strong>Subject:</strong><br>
<input name="sub" style="width: 548px" type="text"><br>
<strong>Comment:</strong><br>
<textarea name="com" style="width: 548px; height: 394px"></textarea><br>
<input type="button" value="Submit Form" onclick="checkform()" style="width: 103px"><br>
</form>
</body>
</html>
Put this in mailer.php...
<html>
<title>Contact</title>
<body>
<?php

$fn = $_POST['fn'];
$ln = $_POST['ln'];
$email = $_POST['addr'];
$sub = @$_POST['sub'];
$com = $_POST['com'];

if($sub==""){$sub = "blank";}

$mailto = "youremailaddress@domain.com";
if($fn<>"" and $ln<>"" and $email<>"" and $com<>""){

$message = "$fn $ln\n$email\n--\n$com";

if(@mail($mailto, $sub, $message)){
echo "Message sent.";
}
else{
echo "Message not sent.";
}

}

?>

<a href="contact.php">Return</a>

</body>
</html>

vcstarter
September 28th, 2005, 10:50 PM
I don't have a file name mailer.php in my directory. Do I have to create it?

vcstarter
September 28th, 2005, 11:09 PM
Anyway, I created the mailer file and copy both of them in the root directory, however when I ran it, I got the following error.


Message not sent. Return

PeejAvery
September 28th, 2005, 11:26 PM
That means that the failure is on the PHP mail configuration side. The server you are working with must support PHP and ASP but not have a working SMTP server.

vcstarter
September 28th, 2005, 11:48 PM
When I applied for the hosting, they give me an smtp out which is the following. Since I am using the server below as my smtp out, does that mean it is a working smtp server?

smtpout.secureserver.net

vcstarter
September 29th, 2005, 12:32 AM
It is still not working, but it seems like I am in better shape now. I went to enable webform mail from the hosting panel. After I enable that option, I get 2 file from my directory. One is gdform.asp and the other one is gdform.php. It seems like what you have told me before to point the form to that form. Anyway, here is the scrip of the gdform.php

You do have both now, let me know if there is any problem


<%

Dim landing_page, host_url
Dim fso, outfile, filename, dirname, myFolder
Dim req_method, key, value
Dim bErr, errStr, bEmpty
On Error resume next
bErr = false
bEmpty = true
errStr = ""
Set fso = Server.CreateObject("Scripting.FileSystemObject")
host_url = Request.ServerVariables("HTTP_HOST")
req_method = Request.ServerVariables("REQUEST_METHOD")
dtNow = Now()
filename = Server.MapPath("ssfm")
dirname = filename
filename = filename & "/gdform_" & DatePart("M", dtNow) & DatePart("D", dtNow) & DatePart("YYYY", dtNow) & DatePart("N", dtNow) & DatePart("S", dtNow)

Function FormatVariableLine(byval var_name, byVal var_value)
Dim tmpStr
tmpStr = tmpStr & "<GDFORM_VARIABLE NAME=" & var_name & " START>" & vbCRLF
tmpStr = tmpStr & var_value & vbCRLF
tmpStr = tmpStr & "<GDFORM_VARIABLE NAME=" & var_name & " END>"
FormatVariableLine = tmpStr
end function

Sub OutputLine(byVal line)
outfile.WriteLine(line)
end sub

if err.number = 0 then
Set outfile = fso.CreateTextFile(filename, true, false)
if err.number <> 0 then
bErr = true
errStr = "Error creating file! Directory may not be writable or may not exist.<br>Unable to process request."
else
if(req_method = "GET") then
for each Item in request.QueryString
if item <> "" then
bEmpty = false
key = item
value = Request.QueryString(item)
if(lcase(key) = "redirect") then
landing_page = value
else
line = FormatVariableLine(key, value)
Call OutputLine(line)
end if
end if
next
elseif (req_method = "POST") then
for each Item in request.form
if item <> "" then
bEmpty = false
key = item
value = Request.form(item)
if(lcase(key) = "redirect") then
landing_page = value
else
line = FormatVariableLine(key, value)
Call OutputLine(line)
end if
end if
next
end if
outfile.close
end if
if(bEmpty = true) AND errStr = "" then
bErr = true
errStr = errStr & "<br>No variables sent to form! Unable to process request."
end if
if(bErr = false) then
if (landing_page <> "") then
response.Redirect "http://" & host_url & "/" & landing_page
else
response.Redirect "http://" & host_url
end if
else
Response.Write errStr
end if
set fso = nothing
else
Response.Write " An Error Occurred creating mail message. Unable to process form request at this time."
end if
%>

<html>
<title>Contact</title>
<body>
<?php

$fn = $_POST['fn'];
$ln = $_POST['ln'];
$email = $_POST['addr'];
$sub = @$_POST['sub'];
$com = $_POST['com'];

if($sub==""){$sub = "blank";}

$mailto = "info@speaklogic.org";
if($fn<>"" and $ln<>"" and $email<>"" and $com<>""){

$message = "$fn $ln\n$email\n--\n$com";

if(@mail($mailto, $sub, $message)){
echo "Message sent.";
}
else{
echo "Message not sent.";
}

}

?>

<a href="contact.php">Return</a>

</body>
</html>

PeejAvery
September 29th, 2005, 12:55 AM
It is still not working, but it seems like I am in better shape now. I went to enable webform mail from the hosting panel. After I enable that option, I get 2 file from my directory. One is gdform.asp and the other one is gdform.php. It seems like what you have told me before to point the form to that form. Anyway, here is the scrip of the gdform.php
The script I sent you will either work or fail depending on the Apache's server settings for SMTP. It is the webserver's problem, not the scripting.

vcstarter
September 29th, 2005, 11:39 PM
I was in the phorne with the tech support. The guy told me I have to use asp not php. Eventhough the plan I have says it supports both php and asp. He said I have to use the following script; by the way, I don't know vb. So, what can I do with the scrip below to make it work?

<%

Dim landing_page, host_url
Dim fso, outfile, filename, dirname, myFolder
Dim req_method, key, value
Dim bErr, errStr, bEmpty
On Error resume next
bErr = false
bEmpty = true
errStr = ""
Set fso = Server.CreateObject("Scripting.FileSystemObject")
host_url = Request.ServerVariables("HTTP_HOST")
req_method = Request.ServerVariables("REQUEST_METHOD")
dtNow = Now()
filename = Server.MapPath("ssfm")
dirname = filename
filename = filename & "/gdform_" & DatePart("M", dtNow) & DatePart("D", dtNow) & DatePart("YYYY", dtNow) & DatePart("N", dtNow) & DatePart("S", dtNow)

Function FormatVariableLine(byval var_name, byVal var_value)
Dim tmpStr
tmpStr = tmpStr & "<GDFORM_VARIABLE NAME=" & var_name & " START>" & vbCRLF
tmpStr = tmpStr & var_value & vbCRLF
tmpStr = tmpStr & "<GDFORM_VARIABLE NAME=" & var_name & " END>"
FormatVariableLine = tmpStr
end function

Sub OutputLine(byVal line)
outfile.WriteLine(line)
end sub

if err.number = 0 then
Set outfile = fso.CreateTextFile(filename, true, false)
if err.number <> 0 then
bErr = true
errStr = "Error creating file! Directory may not be writable or may not exist.<br>Unable to process request."
else
if(req_method = "GET") then
for each Item in request.QueryString
if item <> "" then
bEmpty = false
key = item
value = Request.QueryString(item)
if(lcase(key) = "redirect") then
landing_page = value
else
line = FormatVariableLine(key, value)
Call OutputLine(line)
end if
end if
next
elseif (req_method = "POST") then
for each Item in request.form
if item <> "" then
bEmpty = false
key = item
value = Request.form(item)
if(lcase(key) = "redirect") then
landing_page = value
else
line = FormatVariableLine(key, value)
Call OutputLine(line)
end if
end if
next
end if
outfile.close
end if
if(bEmpty = true) AND errStr = "" then
bErr = true
errStr = errStr & "<br>No variables sent to form! Unable to process request."
end if
if(bErr = false) then
if (landing_page <> "") then
response.Redirect "http://" & host_url & "/" & landing_page
else
response.Redirect "http://" & host_url
end if
else
Response.Write errStr
end if
set fso = nothing
else
Response.Write " An Error Occurred creating mail message. Unable to process form request at this time."
end if
%>