Question

I have this form in the index.php file :

<HTML>
<HEAD>
    <TITLE>Contact Page</TITLE>
    <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
</HEAD>

<table border="0" cellpadding="0" cellspacing="3">
    <form method="post" action="thankyou.php">
        <tr>
            <td>Name:</td>
            <td><input name="name" type="text"></td>
        </tr>

        <tr><td>Your Browser:</td>
            <td>
                <select name="Browser">
                    <option value="Internet Explorer" selected>Internet Explorer
                    <option value="Mozilla">Mozilla
                    <option value="Other">Other
                </select></td>
        </tr>



        <tr>
            <td>Software you use:</td>
            <td><input name="Microsoft Word" type="checkbox" value="yes">Microsoft Word<br>
                <input name="Microsoft Excel" type="checkbox" value="yes">Microsoft Excel<br>
                <input name="Adobe Photoshop" type="checkbox" value="yes">Adobe Photoshop<br>

            </td>
        </tr>

        <tr>
            <td>Age:</td>
            <td>
                <input name="Age" type="radio" value="10-15">10-15<br>
                <input name="Age" type="radio" value="16-20">16-20<br>
                <input name="Age" type="radio" value="21-90">21-90<br>
            </td>
        </tr>

        <tr><td>Other Comments:</td>
            <td><textarea name="Other Comments" rows=10 cols=30></textarea></td>
        </tr>

        <tr><td>&nbsp;</td><td><input type="submit" value="Send Message"></td>
        </tr></form>
</table>

and this is in my thankyou.php :

    <?php
//This command imports the values from contact.php. Please do not touch.
@import_request_variables("gpc");

//The email address the message will be sent to
$youremail = "youremail@yoursite.com";

//The subject of the email you will receive;
$subject = "Our Survey";

//The page your visitor will be redirected to.
$redirect = "http://www.yoursite.com";

//Time until the page redirects your visitor (seconds)
$secs = "5";

//This takes all of the information from the form and sorts it out. Please leave as is.
foreach ($_POST as $name => $value) {
    $thetext = $thetext . "$name : $value\n";
}
?>

<HTML>
    <HEAD>
        <TITLE>Thank you</TITLE>
        <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
    </HEAD>
    <table cellpadding=0 cellspacing=0>
        <tr><td>

<?php
//Checks to see if the name field is empty. You can delete or add fields as needed.

if ((!empty($name))) {

    $name = stripslashes($name);
    $message = stripslashes($message);
//This is where the email is sent using your values from above.
    mail("$youremail", "$subject", "$thetext");
    ?>

                    <meta http-equiv="refresh" content="<?php = $secs; ?>;URL=<?php = $redirect; ?>">

                    Thank you, we have recieved your message.
                    <p>
                        You are now being redirected to our <a href="<?php = $redirect; ?>">homepage</a>.

    <?php
} else {
    ?>

                        We require your name email address and a message in order to reply to your message. Please click <a href="javascript:history.back(1);">here</a> or your browsers back button to try again.

                        <?php
                    }
                    ?>

            </td></tr>
    </table>

I want to make "Your Browser" and/or "Other Comments:" a required field, so if the visitor doesn't fill in at least one of them, he will be redirected to some page which will say, please fill the required fields. If he fills in any of them the form should submit successfully. I know there is some basic editing needed, but unfortunately I'm just learning and couldn't do it myself.

Please help.

Was it helpful?

Solution

Try this:

<HTML>
<HEAD>
<TITLE>Contact Page</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
</HEAD>
<script>
function validateForm()
{
var x=document.forms["my_form"]["name"].value;
var y=document.forms["my_form"]["comments"].value;
if (x==null || x=="")
  {
  alert("name must be filled out");
  return false;
  }
  if (y==null || y=="")
  {
  alert("comments must be filled out");
  return false;
  }



}
</script>

<table border="0" cellpadding="0" cellspacing="3">
<form method="post" action="thankyou.php" name="my_form" onsubmit="return validateForm()">
<tr>
<td>Name:</td>
<td><input name="name" type="text"></td>
</tr>


<tr><td>Other Comments:</td>
<td><textarea name="comments" rows=10 cols=30></textarea></td>
</tr>

<tr><td>&nbsp;</td><td><input type="submit" value="Send Message"></td>
</tr></form>
</table>

This will validate on the same page, so it will be best.

EDIT:

For only one, try like this:

<HTML>
<HEAD>
<TITLE>Contact Page</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
</HEAD>
<script>
function validateForm()
{
var x=document.forms["my_form"]["name"].value;
var y=document.forms["my_form"]["comments"].value;
var count = 0;
  if (!(x==null || x==""))
  { 
    count++;
  }
  if (!(y==null || y==""))
  { 
     count++;
  }
  if(count < 1){
    alert("Please fill at least one field");
    return false;     
  }



}
</script>

<table border="0" cellpadding="0" cellspacing="3">
<form method="post" action="thankyou.php" name="my_form" onsubmit="return validateForm()">
<tr>
<td>Name:</td>
<td><input name="name" type="text"></td>
</tr>


<tr><td>Other Comments:</td>
<td><textarea name="comments" rows=10 cols=30></textarea></td>
</tr>

<tr><td>&nbsp;</td><td><input type="submit" value="Send Message"></td>
</tr></form>
</table>

Demo here>>

OTHER TIPS

You can use jquery for validations in html/php forms.Try to include jquery validations in your html code. All you have to do is just include latest version of jquery and the validate.js from github

and make the drop down list required as follows.

<select name="Browser" required="true">

And that will take care of the things. you can also explore about validate.js from this link

*Update : *

Also you can use the html5 validations for modern browsers.

<input type="text" name="username" required="required">
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top