Jump to content
jamesadrian

PHP Script and HTML Contact Form

Recommended Posts

I have another problem with the script which I find is also a problem with the similar script provided by godaddy.com. The script succeeds and clears the entered data but when you use the back arrow to go back to the page that you came from it fills the values back into the form and you go back only by hitting the back arrow a second time.

 

Is there any way to correct this?

 

Thank you for your help.

 

Jim Adrian

Share this post


Link to post
Share on other sites

Yes. This problem exists at futurebeacon.com and also in http://www.needsnotmet.com/contactnnm.php

 

Futurebeacon uses a script provided by godaddy.com (my hosting service) called gdform.php. I have not access to its details.

 

The letter page contains a script written by me. Here is the code for the whole page:

 

<?php
if (isset($_POST['Submit']))

{
// get posted data into local variables
$From = "Contact Form at http://www.needsnotmet.com/contactnnm.php";
$EmailTo = "contact@needsnotmet.com";
$Subject = "Update Request";
$Name = Trim(stripslashes($_POST['Name']));
$Email = Trim(stripslashes($_POST['Email']));
$Comments = Trim(stripslashes($_POST['Comments']));

// validation
$validationOK=true;
if (Trim($Name)=="") $validationOK=false;

if (Trim($Email)=="") $validationOK=false;

if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=http://www.needsnotmet.com/error.htm\">";
exit;
}

// prepare email body text
$Body ="";
$Body .= "\n";
$Body .= "\n";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "\n";
$Body .= "Email Address: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "\n";
$Body .= "Comments: ";
$Body .= $Comments;
$Body .= "\n";
$Body .= "\n";
$Body .= "From: ";
$Body .= $From;
$Body .= "\n";
$Body .= "\n";


// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$From>");

// Print style redirect to success page:
if ($success){
print "<meta http-equiv=\"refresh\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=http://www.needsnotmet.com/error.htm\">";
}



}

?>


<!DOCTYPE html>
<html>
<head>
<title>Contact</title>
<meta name="description" content="">
<meta name="keywords" content="">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>

<body style="background-image: url(http://www.needsnotmet.com/bluesky.png);">
<div style="width: 3000px; height: 3000px; font-size: 20px; line-height: 28px; color: #000000; font-family: times new roman;">
<div style="margin-left: 60px; margin-right: 0px;">


<br /><br />

<div style="font-size: 30px; line-height: 5px;">This is your contact with NeedsNotMet.com
</div>


<br /><br />

<br /><br />

<div style="width: 1000px;">

Your email address will not be shared with others.
<br /><br />

<br /><br />

<!-- Website Contact Form-->

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

Name<br />
<input type="text" name="Name" value="" size="29" maxlength="75">
<br />
Email Address<br />
<input type="text" name="Email" value="" size="29" maxlength="75">
<br /><br />
<div style="position: absolute; top: 440px; left: 60px;">
<br /><br />
Questions and Comments:
<br /><br />
<textarea name="Comments" rows="12" cols="70">
</textarea>
<br /><br />
<br /><br />
<input type="submit" name="Submit" value="Submit Form">
<br /><br />
<input type="reset" value="Clear Form">
</div>

</form>

</div>
</div>
</div>


</body>
</html>

 

I can't spot the feature that is responsible.

 

Thank you for your help.

 

Jim Adrian

Share this post


Link to post
Share on other sites

problems:

code in red is html meta tags and supposed to appear in between <head>...</head>

 

Apply these html tags to a php variable and then echo them out between <head>...</head>.

 

The issue you have is the norm in all browsers. You could in theory assign the current inputs as a temporary holding input with name like name="NameTmp", and then on form submission have JavaScript copy these values to correctly named hidden inputs, after which you would clear the temp inputs when submitted. If JavaScript is disabled the hidden form inputs can't be read because JavaScript creates them, so the form would not validate and will not be processed.

 

<?php
if (isset($_POST['Submit']))

{
// get posted data into local variables
$From = "Contact Form at http://www.needsnotm.../contactnnm.php";
$EmailTo = "contact@needsnotmet.com";
$Subject = "Update Request";
$Name = Trim(stripslashes($_POST['Name']));
$Email = Trim(stripslashes($_POST['Email']));
$Comments = Trim(stripslashes($_POST['Comments']));

// validation
$validationOK=true;
if (Trim($Name)=="") $validationOK=false;

if (Trim($Email)=="") $validationOK=false;

if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=http://www.needsnotmet.com/error.htm\">";
exit;
}

// prepare email body text
$Body ="";
$Body .= "\n";
$Body .= "\n";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "\n";
$Body .= "Email Address: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "\n";
$Body .= "Comments: ";
$Body .= $Comments;
$Body .= "\n";
$Body .= "\n";
$Body .= "From: ";
$Body .= $From;
$Body .= "\n";
$Body .= "\n";


// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$From>");

// Print style redirect to success page:
if ($success){
print "<meta http-equiv=\"refresh\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=http://www.needsnotmet.com/error.htm\">";
}



}

?>


<!DOCTYPE html>
<html>
<head>
<title>Contact</title>
<meta name="description" content="">
<meta name="keywords" content="">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>

<body style="background-image: url(http://www.needsnotm...m/bluesky.png);">
<div style="width: 3000px; height: 3000px; font-size: 20px; line-height: 28px; color: #000000; font-family: times new roman;">
<div style="margin-left: 60px; margin-right: 0px;">


<br /><br />

<div style="font-size: 30px; line-height: 5px;">This is your contact with NeedsNotMet.com
</div>


<br /><br />

<br /><br />

<div style="width: 1000px;">

Your email address will not be shared with others.
<br /><br />

<br /><br />

<!-- Website Contact Form-->

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

Name<br />
<input type="text" name="Name" value="" size="29" maxlength="75">
<br />
Email Address<br />
<input type="text" name="Email" value="" size="29" maxlength="75">
<br /><br />
<div style="position: absolute; top: 440px; left: 60px;">
<br /><br />
Questions and Comments:
<br /><br />
<textarea name="Comments" rows="12" cols="70">
</textarea>
<br /><br />
<br /><br />
<input type="submit" name="Submit" value="Submit Form">
<br /><br />
<input type="reset" value="Clear Form">
</div>

</form>

</div>
</div>
</div>


</body>
</html>

Share this post


Link to post
Share on other sites

The browser back-arrow using the page you posted above?

 

Have you tried...

 

http://www.w3schools.com/tags/att_input_autocomplete.asp

 

...and...

<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Expires" content="-1">

...and if all else fails the Javascript I posted earlier would also work as long as Javascript has not been disabled.

Share this post


Link to post
Share on other sites

It won't work because hitting the back button does not cause reload, therefore onload event is not triggered.

 

Creating temporary input to hold original value, then create hidden inputs,then copy temp values to these hidden, then clear temp inputs is the only possible solution to achieve the result you require.

Share this post


Link to post
Share on other sites

I did some investigation on this a while ago. By adding an empty onunload event on the page the browser is forced to reload the page rather than load it from cache.

http://stackoverflow.com/questions/9071838/force-reload-refresh-when-pressing-the-back-button#answer-30345009

Share this post


Link to post
Share on other sites

Interesting. The onpageshow event seems to work if it is attached to the body.

Share this post


Link to post
Share on other sites


<?php

header("Expires: Mon, 20 Dec 1998 01:00:00 GMT");

header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");

header("Cache-Control: no-cache, must-revalidate");

header("Pragma: no-cache");

$meta = $Name = $Email = $Comments = $message = "";

 

if (isset($_POST['Submit'])) {

// get posted data into local variables

$From = "Contact Form at http://www.needsnotmet.com/contactnnm.php";

$EmailTo = "contact@needsnotmet.com";

$Subject = "Update Request";

 

if (isset($_POST['Name'])) {

$Name = Trim(stripslashes($_POST['Name']));

}

if (isset($_POST['Email'])) {

$Email = Trim(stripslashes($_POST['Email']));

}

if (isset($_POST['Comments'])) {

$Comments = Trim(stripslashes($_POST['Comments']));

}

 

$_POST = array();

 

// validation

$validationOK = true;

 

if (Trim($Name) == "") {

$validationOK = false;

}

if (Trim($Email) == "") {

$validationOK = false;

}

 

if (!$validationOK) {

header('Location: http://www.needsnotmet.com/error.htm');

exit;

}

 

// prepare email body text

$Body = "";

$Body .= "\n";

$Body .= "\n";

$Body .= "Name: ";

$Body .= $Name;

$Body .= "\n";

$Body .= "\n";

$Body .= "Email Address: ";

$Body .= $Email;

$Body .= "\n";

$Body .= "\n";

$Body .= "Comments: ";

$Body .= $Comments;

$Body .= "\n";

$Body .= "\n";

$Body .= "From: ";

$Body .= $From;

$Body .= "\n";

$Body .= "\n";

 

 

// send email

$success = mail($EmailTo, $Subject, $Body, "From: <$From>");

 

// Print style redirect to success page:

if ($success) {

header('Location: http://www.needsnotmet.com/contactnnm.php?success=yes');

exit;

} else {

 

$meta.="<meta http-equiv=\"refresh\" content=\"0;URL=http://www.needsnotmet.com/error.htm\">";

}

}

?>

<!DOCTYPE html>

<html>

<head>

<title>Contact</title>

<meta name="description" content="">

<meta name="keywords" content="">

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<?php

echo $meta;

?>

<script>

window.onload = function() {

 

var myForm = document.getElementById("myForm");

 

myForm.onsubmit = function() {

 

 

for (i = 0; i < myForm.elements.length; i++)

{

var convert = "";

if (myForm.elements.type !== "submit" && myForm.elements.type !== "reset" && myForm.elements.type !== "hidden")

{

convert = myForm.elements.name.replace('Tmp', '');

 

var input = document.createElement("input");

input.setAttribute("type", "hidden");

input.setAttribute("name", convert);

input.setAttribute("value", myForm.elements.value);

myForm.appendChild(input);

 

myForm.elements.value = "";

 

}

 

 

}

return true;

};

 

};

</script>

 

</head>

 

<body style="background-image: url(http://www.needsnotmet.com/bluesky.png);">

<div style="width: 3000px; height: 3000px; font-size: 20px; line-height: 28px; color: #000000; font-family: times new roman;">

<div style="margin-left: 60px; margin-right: 0px;">

 

<?php

if (isset($_GET['success'])) {

 

echo '<h2>Email sent successfully</h2>';

}

?>

<br /><br />

 

<div style="font-size: 30px; line-height: 5px;">This is your contact with NeedsNotMet.com

</div>

 

 

<br /><br />

 

<br /><br />

 

<div style="width: 1000px;">

 

Your email address will not be shared with others.

<br /><br />

 

<br /><br />

 

<!-- Website Contact Form-->

 

<form id="myForm" method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">

 

Name<br />

<input type="text" name="NameTmp" value="" size="29" maxlength="75">

<br />

Email Address<br />

<input type="text" name="EmailTmp" value="" size="29" maxlength="75">

<br /><br />

<div style="position: absolute; top: 440px; left: 60px;">

<br /><br />

Questions and Comments:

<br /><br />

<textarea name="CommentsTmp" rows="12" cols="70">

 

 

</textarea>

<br /><br />

<br /><br />

 

 

<input type="submit" name="Submit" value="Submit Form">

<br /><br />

<input type="reset" value="Clear Form">

 

</div>

 

</form>

 

</div>

</div>

</div>

Share this post


Link to post
Share on other sites

In the website php contact script and html form that we have been discussing, I find a problem that I have encountered before. I haven't found a remedy.

 

In the last two lines of the form below there is a submit button and a send button. The text size for the word SEND and for Clear Form is something I would like larger, but I cannot find a way to control it from within the form.

 

<form id="myForm" method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">

Name<br />
<input type="text" name="NameTmp" value="" size="29" maxlength="75">
<br />
Email Address<br />
<input type="text" name="EmailTmp" value="" size="29" maxlength="75">
<br /><br />

Questions and Comments about Needs Not Met:
<br /><br />
<textarea name="CommentsTmp" rows="12" cols="60">


</textarea>
<br /><br />
This window will scroll for long messages.
<br /><br />
<input type="submit" name="Submit" style="width:100px; height:100px;" value="SEND" />
                
<input type="reset" style="width:100px; height:100px;" value="Clear Form">

</form>

 

I have tried many suggestions from several websites, but nothing works. Does anybody here know how to do it?

 

It can bee seen at the top of http://www.needsnotmet.com.

 

Thank you for your help.

 

Jim Adrian

Share this post


Link to post
Share on other sites

dsonesuk,

 

You included in your solution to the contact form problem a script that I assume is java script. I know nothing about it, but I am happily using it. I surely need to learn about it. Would you give me a brief description of what this particular scrip does?

 

<script>
window.onload = function() {

var myForm = document.getElementById("myForm");

myForm.onsubmit = function() {


for (i = 0; i < myForm.elements.length; i++)
{
var convert = "";
if (myForm.elements[i].type !== "submit" && myForm.elements[i].type !== "reset" && myForm.elements[i].type !== "hidden")
{
convert = myForm.elements[i].name.replace('Tmp', '');

var input = document.createElement("input");
input.setAttribute("type", "hidden");
input.setAttribute("name", convert);
input.setAttribute("value", myForm.elements[i].value);
myForm.appendChild(input);

myForm.elements[i].value = "";

}


}
return true;
};

};
</script>

 

Thank you for your help.

 

Jim Adrian

Share this post


Link to post
Share on other sites

First to learn is before an element can be referenced it needs to exist, that is where window.onload comes in, usually placed between <head>...</head> it waits until page is fully loaded or rendered before allowing code within it to reference an element.

 

But the code can be placed at bottom of page, and since the page loads or renders from top to bottom using window.onload is not required.

 

window.onload = function() {
// referencing form using id
var myForm = document.getElementById("myForm");

// now we have a variable referencing a specific form we can check for specific events
myForm.onsubmit = function() {

// or loop through all form elements whatever they are using '.elements' using index ref of 0 to less than total number of form elements
for (i = 0; i < myForm.elements.length; i++){

// create variable to hold converted name value
var convert = "";

// target form elements otherthan submit, reset or hidden type
if (myForm.elements[i].type !== "submit" && myForm.elements[i].type !== "reset" && myForm.elements[i].type !== "hidden"){

//remove 'Tmp' from temporary inputs attribute name values and assign to convert variable
convert = myForm.elements[i].name.replace('Tmp', '');

//create inputs, set type attribute, name attribute to convert variable value, and temporary input value to this created input value, and finally append new inputs under current elements within form
var input = document.createElement("input");
input.setAttribute("type", "hidden");
input.setAttribute("name", convert);
input.setAttribute("value", myForm.elements[i].value);
myForm.appendChild(input);

//clear temporary input values
myForm.elements[i].value = "";
}

// allow submission of form with new created hidden inputs type, with name attribute values 'Name', 'Email', and 'Comments'

return true;

};

};
Edited by dsonesuk

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...