Solved

required fields in PHP

Posted on 2007-04-05
10
223 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:Vel Eous
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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

860 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