Jump to content

Form going haywire


Ninjakreborn

Recommended Posts

I was helping a graphic designer on something, I was just building a basic form (no real styling)
Normally everything goes smoothly, I built 2 for him and everything, seemed to work fine.
form2.php
[code]<?php require_once($docroot . "master/config/config.php"); ?>
<form action="form2processor.php" method="post" name="inputform" onsubmit="return checkFields()">
<label for="firstname"><b>First Name</b></label><br />
<input type="text" name="firstname" size="30" maxlength="255" value="<?php echo stripslashes($_GET['firstname']); ?>" /><br />
<label for="lastname"><b>Last Name</b></label> <br />
<input type="text" name="lastname" size="30" maxlength="255" value="<?php echo stripslashes($_GET['lastname']); ?>" /><br />
<label for="telephone"><b>Telephone</b></label> <br />
<input type="text" name="telephone" size="30" maxlength="255"
value="<?php echo stripslashes($_GET['telephone']); ?>" /><br />
<label for="email"><b>Email Address</b></label><br />
<input type="text" name="email" size="30" maxlength="255" value="<?php echo stripslashes($_GET['email']); ?>" /><br />
<label for="pricerange">Price Range:</label><br />
<select name="pricerange">
<option selected="selected">Select one...</option>
<option value="Less than $500,000">Less than $500,000</option>
<option value="$500,000 to $600,000">$500,000 to $600,000</option>
<option value="$600,000 to $700,000">$600,000 to $700,000</option>
<option value="$700,000 to $800,000">$700,000 to $800,000</option>
<option value="$800,000 plus">$800,000 plus</option>
</select><br />
<label for="movedate">Move Date:</label><br />
<select name="Movedate">
<option selected="selected">Select one...</option>
<option value="Less than 3 Months">Less than 3 Months</option>
<option value="3 to 6 Months">3 to 6 Months</option>
<option value="6 to 9 Months">6 to 9 Months</option>
<option value="9 Months to 1 Year">9 Months to 1 Year</option>
<option value="1 Year or More">1 Year or More</option>
</select><br />
<label for="squarefoot">Approximate Square Foot:</label><br />
<select name="squarefoot" id="squarefoot">
<option value="up to 2500 Sq Ft">up to 2500 Sq Ft</option>
<option value="2500 to 3000">2500 to 3000</option>
<option value="3000 to 3500">300 to 3500</option>
<option value="3500 to 4000">3500 to 4000</option>
<option value="4000+">4000+</option>
</select><br />
<label for="sidingmaterial">Siding Material:</label><br />
<select name="sidingmaterial" id="sidingmaterial">
<option value="Brick">Brick</option>
<option value="Stone">Stone</option>
<option value="Hardie">Hardie</option>
<option value="Cedar">Cedar</option>
<option value="Vinyl">Vinyl</option>
</select><br />
<?php
select("granitekitchen", "Granite Kitchen", "Yes");
select("stainlesssteelappliances", "Stainless Steel Appliances", "Yes");
select("highceilings", "High Ceilings", "Yes");
select("openfloorplan", "Open Floor Plan", "Yes");
select("cargarage_2", "2 Car Garage", "Yes");
select("cargarage_3", "3 Car Garage", "Yes");
select("doublelevelfoyer", "Double Level Foyer", "Yes");
select("hardwoodfloors", "Hardwood Floors", "Yes");
select("marblebaths", "Marble Baths", "Yes");
?>
<label for="comments"><b>What may I help you with?</b></label><br />
<input type="text" name="comments" size="30" maxlength="255" value="<?php echo stripslashes($_GET['message']); ?>" /><br />
<input name="submit" type="submit" value="submit"><br />
[/code]

Processor
[code]<?php require_once($docroot . "master/config/config.php"); ?>
<?php
if (isset($_POST['submit'])) {
$errorhandler = "";
validateemail($_POST['email']);
if ($_POST['firstname'] == "") {
$errorhandler .= "First Name was left blank.<br />";
}
if ($_POST['lastname'] == "") {
$errorhandler .= "Last Name was left blank.<br />";
}
if ($_POST['telephone'] == "") {
$errorhandler .= "Telephone was left blank.<br />";
}
$url = "<a href=\"form2.php?firstname={$_POST[firstname]}&lastname={$_POST[lastname]}&telephone={$_POST[telephone]}&email={$_POST[email]}&comments={$_POST[comments]}\" title=\"Fix Errors\">Fix Errors</a>";
errorhandler($errorhandler, $url);
if ($errorhandler == "") {
$subject = "Schedule A Personalized Showing Form Response";
$firstname = stripslashes($_POST['firstname']);
$lastname = stripslashes($_POST['lastname']);
$telephone = stripslashes($_POST['telephone']);
$comments = stripslashes($_POST['comments']);
$pricerange = stripslashes($_POST['pricerange']);
$movedate = stripslashes($_POST['movedate']);
$squarefoot = stripslashes($_POST['squarefoot']);
$sidingmaterial = stripslashes($_POST['squarefoot']);
$granitekitchen = stripslashes($_POST['granitekitchen']);
if ($granitekitchen != "Yes") {
$granitekitchen = "No";
}
$stainlesssteelappliances = stripslashes($_POST['stainlesssteelappliances']);
if ($stainlesssteelappliances != "Yes") {
$stainlesssteelappliances = "No";
}
$highceilings = stripslashes($_POST['highceilings']);
if ($highceilings != "Yes") {
$highceilings = "No";
}
$openfloorplan = stripslashes($_POST['openfloorplan']);
if ($openfloorplan != "Yes") {
$openfloorplan = "No";
}
$cargarage_2 = stripslashes($_POST['cargarage_2']);
if ($cargarage_2 != "Yes") {
$cargarage_2 = "No";
}
$cargarage_3 = stripslashes($_POST['cargarage_3']);
if ($cargarage_3 != "Yes") {
$cargarage_3 = "No";
}
$doublelevelfoyer = stripslashes($_POST['doublelevelfoyer']);
if ($doublelevelfoyer != "Yes") {
$doublelevelfoyer = "No";
}
$hardwoodfloors = stripslashes($_POST['hardwoodfloors']);
if ($doublelevelfoyer != "Yes") {
$doublelevelfoyer = "No";
}
$marblebaths = stripslashes($_POST['marblebaths']);
if ($marblebaths != "Yes") {
$marblebaths = "No";
}

$emailmessage = "
First Name: {$firstname}
Last Name: {$lastname}
Telephone: {$telephone}
Price Range: {$pricerange}
Move Date: {$movedate}
Square Foot: {$squarefoot}
Siding Material: {$sidingmaterial}
The following they selected as applies:

Granite Kitchen: {$granitekitchen}
Stainless Steel Appliances: {$stainlesssteelappliances}
High Ceilings: {$highceilings}
Open Floor Plan: {$openfloorplan}
2 Car Garage: {$cargarage_2}
3 Car Garage: {$cargarage_3}
Double Level Foyer: {$doublelevelfoyer}
Hardwood Floors: {$hardwoodfloors}
Marble Baths: {$marblebaths}

Comments:
{$comments}
";


if (mail($contactemail, $subject, $emailmessage)) {
echo "We have recieved your message, we will contact you within the next 48 hours.<br />";
}else {
echo "There was a problem sending the contact emaill, please try again.<br />";
}
}
}
?>
[/code]

Now my other processors are working fine, but I noticed a few things on form2.php I made about 3 forms for him and the other's worked.  Now this form has 2 issue's, and one of the issues is also present on the other 2 forms.
I could just do it the standard way, but it's something wrong I am doing with a function.

The function is
[code]<?php
function validateemail($email) {
// set regular expression to test email
if ($email == "") {
$errorhandler .= "The email address was left blank.<br />";
}
$regexemail =
"^[A-Za-z0-9\._-]+@([A-Za-z0-9][A-Za-z0-9-]{1,62})(\.[A-Za-z][A-Za-z0-9-]{1,62})+$";
if (!ereg("$regexemail", $emailaddress)) {  // test for formatting
$errorhandler .= "The email address is improperly formatted<br />";
}
// test email domain name for existence
if(!(getmxrr(substr(strstr($emailaddress, '@'), 1), $temp)) || checkdnsrr(gethostbyname(substr(strstr($emailaddress, '@'), 1)), "ANY")) {
$errorhandler .= "The Domain name for the email address does not exist<br />";
}
}
?>[/code]

Now you can see throughout form2.php where I called the function in the processor.
Now other functions are working just fine, it's only this one.
In my scripts I initialize a blank variable $errorhandler
Then I append things to the string as errors occur, I created this function to hopefully deal with everything related to emails more quickly (I even tried in the function putting return $errorhandler;, however it had no effects positively or negatively.)  As of right now it's not working on any of them, so it's obvious it's the function can someone help me figure out why it's not reading my function at all.  It's not even giving me function not declared, it's just simply not doing anything, it can't tell when the email isn't filled in, it ignores it all together.
Now the other problem is

Form2.php is not sending me an email, at all.  The other 2 form's, are sending the email, and it's coming to my box, but for some wierd reason this other form2.php is not wanting to send it, no matter how many time's I try

I fought with both of these issues for awhile, with no luck, any advice would be greatly appreciated.
Link to comment
Share on other sites

right after you start that function, try adding this:

global $errorhandler;

ALSO, you always want to return the function either true or false. So if the email doesn't validate (in any of those if statements) then do return false;
And then under all the if statements put return true; . That tells the function that everything went ok.
Link to comment
Share on other sites

[quote]right after you start that function, try adding this:

global $errorhandler;

Also, you always want to return the function either true or false. So if the email doesn't validate (in any of those if statements) then do return false;
And then under all the if statements put return true; . That tells the function that everything went ok.[/quote]

Ok now I have a couple of questions.
1. I see that the global was something I forgot, in what situations do I need this, in which one's do I not need it.  If I need it then I will always use it, but do I need to make any
I understand what global is for, I know how it's used, but I don't understand when to use it and when not to.

For instance
[code]<?php
function errorhandler($errorhandler, $url) {
if ($errorhandler != "") {
echo "<span style=\"color:red\">";
echo $errorhandler;
if ($url != "no") {
echo $url;
}
echo "</span>";

}
}
?>[/code]

Ok here is hte thing with this function, I create the url variable before it, and then I pass the function the errorhandler and the url variable.
Now I don't use global on either one of these, but it works fine.

Other functions seem to work like this similarly, so when does the global have an affect on it, for whether it works or not.  SO you see why I would have wonderings about this.

2.  I understnad return true; and return false; but that's primarily from javascript.
I always did php programming, without functions until not to very long ago, so I am hazy on the return true, return false.
When I see return true, then I normally recall it returning the function as false, as the function itself failed.
Like the mail function it returns false if it fails, and true if it succeed's, that's it.
That function I have above, the one with the email's, it's not really suppose to return whether the function worked or not, if it run's into an error it's just suppose to append the specific error into the error handler, and after the function is totally ran through, it continues on with the rest of the script doing other form's of error handling.
Can you explain (in this situation), what the different would be in having the return false, and return true in those locations, would differ from not having them there (I am asking for learning purposes)

That should answer all my questions.

Does anyone also have any advice on why the mail on form2.php is not sending, the other's are, but that isn't.

Now thnanks Jocka for all the help so far, I have read tutorials, and ton's of books on functions before coming over to them, I guess there were just a few things I didn't understand, I am trying to get them down very well, then moving over into object oriented programming, but I have to have a full understanding of all concepts behind functions, and have used them awhile before I can move into that.

Any more advice, or answers would be appreciated.

MAJOR EDIT - Above was my original message, and it wouldn't let me post then, and kept me from the site for a few minutes, so a few things have changed on this,

1. The form finally sent me emails, I think it was my email provider going slow.
2. The Function, I went ahead and tried everything you said, the global, the return true's, and false's, and everything, but nothing made it work, I tried all combinations with the 2 things you said, but it's still not reading the function.

Everything is working, but with this function broke (it being in my framework), then all 3 of the forms are not working (the email validation part), I could just go ahead and pull the stuff into the file's and do it the way I use to without functions, but i know this is something I am doing wrong, and something I would like to learn from.

Any help, or answers to those first couple of questions, I would greatly appreciate it.
Link to comment
Share on other sites

[code]
<?php
// Function:
function validateemail($email) {
global $errorhandler;
// set regular expression to test email
if ($email == "") {
$errorhandler .= "The email address was left blank.<br />";
}
$regexemail =
"^[A-Za-z0-9\._-]+@([A-Za-z0-9][A-Za-z0-9-]{1,62})(\.[A-Za-z][A-Za-z0-9-]{1,62})+$";
if (!ereg("$regexemail", $email)) {  // test for formatting
$errorhandler .= "The email address is improperly formatted<br />";

}
// test email domain name for existence
if(!(getmxrr(substr(strstr($email, '@'), 1), $temp)) || checkdnsrr(gethostbyname(substr(strstr($email, '@'), 1)), "ANY")) {
$errorhandler .= "The Domain name for the email address does not exist<br />";
}
}

// End function
?>
[/code]

Ok it worked, I had misunderstood about the global, so this time I tried it inside the function itself, and it work's.
If you have time can you still answer those question's, because I am very confused as to why it didn't work, and why the global was required, and it worked without the return true's, and false's I am a little confused here, thanks for all the help so far.
Link to comment
Share on other sites

You need to use the global statement in a function whenever you use a variable in the function that is defined outside the function and you expect the function to access the value of variable. In the case of your functions, I would change it to return the string to be appended to the variable $errorhandler.

So change the call in the main script to:
[code]<?php
$errorhandler .= validateemail(email);
?>[/code]

and change your function to:
[code]<?php
// Function:
function validateemail($email) {
        $ret = '';
// set regular expression to test email
if ($email == "") {
$ret = "The email address was left blank.<br />";
}
$regexemail =
"^[A-Za-z0-9\._-]+@([A-Za-z0-9][A-Za-z0-9-]{1,62})(\.[A-Za-z][A-Za-z0-9-]{1,62})+$";
if (!ereg("$regexemail", $email)) {  // test for formatting
$ret .= "The email address is improperly formatted<br />";

}
// test email domain name for existence
if(!(getmxrr(substr(strstr($email, '@'), 1), $temp)) || checkdnsrr(gethostbyname(substr(strstr($email, '@'), 1)), "ANY")) {
$ret .= "The Domain name for the email address does not exist<br />";
}
        return ($ret);
}
// End function
?>[/code]

Ken
Link to comment
Share on other sites

Ok thanks for the help, can you also explain one last thing

the return true, return false, I know it plays a big part in javascript functions, but where does it come into play here in php functions.
When is it needed to be used, when not to use it, what they are really for?

Is it strictly to be used when you need the function to return some form of true or false, like if you are testing whether the funciton was true or false to do something else, that sounds like the only logical situation in which to use return true, or return false, or is there something I am missing.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.