Solved

required fields in PHP

Posted on 2007-04-05
10
220 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
So the following errors occurs in 2 ways that I am aware of at this stage, and you receive one of the following error messages: ERROR 1. When trying to save a rule: No Web listener is specified for the Web publishing rule Autodiscovery Publishin…
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 create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

707 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

11 Experts available now in Live!

Get 1:1 Help Now