Solved

required fields in PHP

Posted on 2007-04-05
10
221 Views
Last Modified: 2007-04-06
Can somebody tell me whats wrong with this PHP. Everytime I hit on submit on my HTML page it takes me no where. It does not do what its supposed to do. I am pasting my HTML and PHP file which its calling

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Please enter the requested infor</title>
</head>

<body bgcolor="#FFCC99">
<form action="formhandler.php" method="POST">
<p>Please enter the requested information.
</p>

<table style="width:240px" border="0">
  <tr>
    <td style="width:62px">Name:</td>
    <td><input name="name" size="30"></td>
  </tr>
  <tr>
    <td style="width:62px">Spouse Name:</td>
    <td><input name="spousename" size="30"></td>
  </tr>
  <tr>
    <td style="width:62px">Address1:</td>
    <td><input name="Address1" size="30"></td>
  </tr>
<tr>
    <td style="width:62px">Address2:</td>
    <td><input name="Address2" size="30"></td>
  </tr>
<tr>
    <td style="width:62px">City:</td>
    <td><input name="city" size="30"></td>
  </tr>
<tr>
    <td style="width:62px">State:</td>
    <td><input name="state" size="5"></td>
  </tr>
<tr>
    <td style="width:62px">Zip:</td>
    <td><input name="zip" size="11"></td>
  </tr>

  <tr>
    <td>Email:</td>
    <td><input name="email" size = "30"></td>
  </tr>
  <tr>
    <td>Phone:</td>
    <td><input name="phone" size="16"></td>
  </tr>

</table>
<br>
&nbsp;</br>
  <input type="submit" value="Send">
  <input type="reset" value="Clear the form">

</form>
</body>
</html>



PHP

<?php
$errorMsg = '';
$name = $_POST['name'];
if ($name=="") {
   errorMsg .= "Then name is required.
}
$sname = $_POST['spousename'];
$ad1 = $_POST['Address1'];
if ($ad1=="") {
   errorMsg .= "Then address is required.
}

$ad2 = $_POST['Address2'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$email = $_POST['email'];
$phone = $_POST['phone'];

if ($errorMsg=='') {
    $message = "Name: $name\n";
    $message .= "Spouse Name: $spousename\n";
    $message .= "Address1: $address1\n";
    $message .= "Address2: $address2\n";
    $message .= "City: $city\n";
    $message .= "State: $state\n";
    $message .= "Zip: $zip\n";
    $message .= "Email: $email\n";
    $message .= "Phone: $phone\n";

    if (!mail('myemailid@yahoo.com','From Website',$message)) die('Mail Failed!');
    header("Location: http://www.yahoo.com");
    exit();
} else {
    echo $errorMsg . "<a href=\"prevpage\">Go Back</a>";
}
?>
0
Comment
Question by:pratikshahse
10 Comments
 
LVL 14

Expert Comment

by:Tchuki
ID: 18859231
$errorMsg = '';
$name = $_POST['name'];
if (empty($name)) {
   errorMsg .= "Then name is required.
}
$sname = $_POST['spousename'];
$ad1 = $_POST['Address1'];
if (empty($ad1)) {
   errorMsg .= "Then address is required.
}

$ad2 = $_POST['Address2'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$email = $_POST['email'];
$phone = $_POST['phone'];

if (empty($errorMsg)) {
    $message = "Name: $name\n";
    $message .= "Spouse Name: $spousename\n";
    $message .= "Address1: $address1\n";
    $message .= "Address2: $address2\n";
    $message .= "City: $city\n";
    $message .= "State: $state\n";
    $message .= "Zip: $zip\n";
    $message .= "Email: $email\n";
    $message .= "Phone: $phone\n";

    if (!mail('myemailid@yahoo.com','From Website',$message)) die('Mail Failed!');
    header("Location: http://www.yahoo.com");
    exit();
} else {
    echo $errorMsg . "<a href=\"prevpage\">Go Back</a>";
}
0
 
LVL 23

Expert Comment

by:Stacy Spear
ID: 18859336
The following lines aren't properly closed. They should like what I put below.

  errorMsg .= "Then name is required.";
   errorMsg .= "Then address is required.";
0
 

Author Comment

by:pratikshahse
ID: 18859733
Its not working. No matter what I do, whether I put in the information and hit submit or if i dont put in infomation , it still takes me nowhere. you can check out  what I am trying to say

www.jcoco.org/new_member.htm

click on submit without entering any info and click on submit after entering info.

if there is no info entered then i want the error to show up saying required fields are required and if the info is filled in then take me back to home page and also e mail me with the info entered.

0
 
LVL 23

Expert Comment

by:Stacy Spear
ID: 18860553
If you echo some text at the top of the php file, does it show up?

Just wondering, as you are right, absolutely nothing came back
0
 

Author Comment

by:pratikshahse
ID: 18860598
Nope. nothing comes up.

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 23

Expert Comment

by:Stacy Spear
ID: 18860686
Somehow that file isn't being updated or has the wrong permissions.

does it has the same permissions as the html? Depending on your server, it may need the execute attribute.

chmod +x filename
0
 

Author Comment

by:pratikshahse
ID: 18860740
it should have the same permissions as I have on html as I have  another PHP file and I am sure i did not change any security on that file and still it runs ok.
0
 
LVL 14

Accepted Solution

by:
raja_ind82 earned 500 total points
ID: 18860946
required.php
********************************************
<?php
if(isset($_POST['hdnvalue']) && $_POST['hdnvalue']=="send")
{
$name = $_POST['firstname'];  
$sname = $_POST['spousename'];
$ad2 = $_POST['Address2'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$email = $_POST['email'];
$phone = $_POST['phone'];


$message = "Name: $name\n";
$message .= "Spouse Name: $spousename\n";
$message .= "Address1: $address1\n";
$message .= "Address2: $address2\n";
$message .= "City: $city\n";
$message .= "State: $state\n";
$message .= "Zip: $zip\n";
$message .= "Email: $email\n";
$message .= "Phone: $phone\n";

@mail('myemailid@yahoo.com','From Website',$message);
header("Location: index.php");
exit;
}

?>

<head>
<title>Please enter the requested infor</title>
<script>
function funcheck() {
 
  with(document.frmname)
  {
       if((firstname.value)=="")
        {
           alert("Please enter the name.");
             firstname.focus();return false;
        }
       else if((spousename.value)=="")
        {
           alert("Please enter the spousename.");
             spousename.focus();return false;
        }
        else
        {      
              hdnvalue.value="send";
              submit();
 
        }
  }
}
</script>
</head>

<body bgcolor="#FFCC99">
<form action="required.php" method="POST" name="frmname">
<input type="hidden" name="hdnvalue">
<p>Please enter the requested information.
</p>

<table style="width:240px" border="0">
  <tr>
    <td style="width:62px">Name:</td>
    <td><input name="firstname" size="30"></td>
  </tr>
  <tr>
    <td style="width:62px">Spouse Name:</td>
    <td><input name="spousename" size="30"></td>
  </tr>
  <tr>
    <td style="width:62px">Address1:</td>
    <td><input name="Address1" size="30"></td>
  </tr>
<tr>
    <td style="width:62px">Address2:</td>
    <td><input name="Address2" size="30"></td>
  </tr>
<tr>
    <td style="width:62px">City:</td>
    <td><input name="city" size="30"></td>
  </tr>
<tr>
    <td style="width:62px">State:</td>
    <td><input name="state" size="5"></td>
  </tr>
<tr>
    <td style="width:62px">Zip:</td>
    <td><input name="zip" size="11"></td>
  </tr>

  <tr>
    <td>Email:</td>
    <td><input name="email" size = "30"></td>
  </tr>
  <tr>
    <td>Phone:</td>
    <td><input name="phone" size="16"></td>
  </tr>

</table>
<br>
&nbsp;</br>
  <input type="button" value="Send" name="btnsubmit" onclick="javascript: funcheck();">
  <input type="reset" value="Clear the form">

</form>
</body>
</html>

index.php
***********************
<?php  
echo "index PAGE";
?>
Regards,
M.Raja
0
 
LVL 23

Expert Comment

by:Stacy Spear
ID: 18861016
Is the other PHP file in the same directory as this one? I would still look at the permissions to verify them.

If that is good, I'm not sure whats going on. I am getting nothing when accessing the file, no errors, nothing.

0
 
LVL 23

Expert Comment

by:Stacy Spear
ID: 18861070
Wow, Raja rewrote the whole thing for you!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
There are several problems reported according slow link speeds or poor performance in TMG 2010, UAG 2010 or ISA 2006. I want to collect here some of the common issues together to give a brief overview what can be the reason. Nevertheless, not all of…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now