[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Java to PHP form valid fields check.

Posted on 2011-05-01
8
Medium Priority
?
239 Views
Last Modified: 2012-05-11
Hi,

The code below needs to be translated to PHP.
Can anybody help me with this?
<script language="JavaScript" type="text/javascript">
<!--
// Check entered values.
function CheckForm () { 

	//Initialise variables
	var errorMsg = "";

	if (document.Info.gender.value == "-select-"){
		errorMsg += "\n  Gender \t\t-     Select a gender";	
	}

	if (document.Info.name.value == ""){
		errorMsg += "\n  Name \t\t-     Enter a name";	
	}
	 	
	if ((document.Info.email.value == "") || (document.Info.email.value.length > 0 && (document.Info.email.value.indexOf("@",0) == - 1 || document.Info.email.value.indexOf(".",0) == - 1))) { 
		errorMsg += "\n  E-mail \t\t-     Enter a valid E-mail address";
	}

	//If there is a problem with the form then display an errormessage
	if (errorMsg != ""){
		msg =  "Error: Not all fields are filled in.\n";
		msg += "\n";
		msg += "Please correct following fields:\n";
		
		errorMsg += alert(msg + errorMsg + "\n\n");
		return false;
	}
	
	return true;
}
// -->
</script>

<body>
 <form action="Contact.php" method="POST" onSubmit="return CheckForm();" name="Info">
 <table>
  <tr>
    <td><select size="1" name="gender">
	<option selected value="-select-">-select-</option>
	<option value="Dhr.">Dhr.</option>
	<option value="Mevr.">Mevr.</option>
    </td>
  <tr>
  <tr>
    <td>Name:</td>
    <td><input type="text" size="44" maxlength="256" name="name"></td>
  </tr>
  <tr>
    <td>E-mail:</td>
    <td><input type="text" size="44" maxlength="256" name="email"></td>
  </tr>
 </table>
 <input type="submit" name="Send" value="Submit" style="height:25">
 <input type="reset" value="Reset" style="height: 25">
 </form>
</body>
etc.

Open in new window

0
Comment
Question by:Stef Merlijn
8 Comments
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 35502785
You can do the server side validation similar kind javascript.  You need to get the values on the server side and validate those,

ref. Server Side Validation
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 35502828
In the page that receives data write following code:

<?php

$errorMsg = '';

if ($_POST['gender'] == '-select-'){
  errorMsg .= "\n  Gender     Select a gender";
}
if ($_POST['name'] == ''){
  errorMsg .= "\n  Name       Enter a name";  
}
if ($_POST['email'] == '' || strpos($_POST['email']) === false){
  errorMsg .= "\n  E-mail     Enter a valid E-mail address";

if (errorMsg != ""){
            msg =  "Error: Not all fields are filled in.\n";
            msg .= "\n";
            msg .= "Please correct following fields:\n";
            
            errorMsg .= alert(msg + errorMsg + "\n\n");
}
?>

This should work.

Cheers
0
 

Author Comment

by:Stef Merlijn
ID: 35502869
marcusG:
The message is currently displayed on a empty webpage.
Can it be displayd in a kind of popup-window?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 84

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 664 total points
ID: 35503007
Here is my PHP Email demo.  It includes a little bit of both javascript and PHP validation on the same page.  Save it as 'Email.php' and put in your own email address in the "$toText".
<?php
error_reporting(E_ALL);
ini_set('display_errors','On');

# some settings of POST vars
if (!isset($_POST['send']))  $send = ''; else $send = $_POST['send'];
if (!isset($_POST['toText'])) $toText = ''; else $toText = $_POST['toText'];
if (!isset($_POST['ccText'])) $ccText = ''; else $ccText = $_POST['ccText'];
if (!isset($_POST['subjectText'])) $subjectText = ''; else $subjectText = $_POST['subjectText'];
if (!isset($_POST['msgText'])) $msgText = ''; else $msgText = $_POST['msgText'];
if (!isset($_POST['ccText'])) $ccTexth = ''; else $ccText = $_POST['ccText'];
if (!isset($_POST['bccText'])) $bccText = ''; else $bccText = $_POST['bccText'];
if (!isset($_POST['nameText'])) $nameText = ''; else $nameText = $_POST['nameText'];
if (!isset($_POST['fromText'])) $fromText = ''; else $fromText = $_POST['fromText'];

if ($send == "") {
    $title="Test Email Page";
    $announce="---";
}
else {
	if($fromText === "") die("No name!");
  $toText="youremail@yourdomain.com";
	$title="Test Email Page";
  $announce="Your Message has been Sent!";
	$header = "From: ".$fromText."\r\n";
//	$header .= "Cc: ".$ccText."\n";
	$header .= "Reply-To : ".$fromText."\r\n";
	$header .= "Return-Path : ".$fromText."\r\n";
	$header .= "X-Mailer: PHP\r\n";
	$header .= "MIME-Version: 1.0\r\n";
	$header .= "Content-Type: text/plain; charset=iso-8859-1\r\n";
//	ini_set(sendmail_from,$fromText);  
	mail($toText, $subjectText, $msgText, $header, '-f'.$fromText);
//	ini_restore(sendmail_from);
}
?>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title><?php echo($title)?></title>
<style type="text/css">
<!-- 
A:link { color: #999999; }
A:visited { color: #999999; }
A:hover {color: #0099ff;}
-->
</style>
<script type="text/javascript">
<!--
function check()
{
var at=document.getElementById("fromText").value.indexOf("@");
var eml=document.getElementById("fromText").value;
var nam=document.getElementById("nameText").value;
var alerttxt="";
var submitOK="true";

if (eml.length < 5 || at == -1)
    {
    alerttxt=alerttxt+"Please enter a valid e-mail address!\r\n";
    submitOK="false"
    //return false;
    }
if (nam.length < 3)
    {
    alerttxt=alerttxt+"Please enter your name.\r\n";
    submitOK="false"
    //return false;
    }
if (submitOK=="false")
    {
    alert(alerttxt);
    return false;
    }

}

// -->
</script>
</head>

<body bgcolor="#ddeedd">
<div align="center">
<table border="0" cellpadding="0" cellspacing="0" summary="" width="580">
<tr><td align="center">

<?php
if ($send != "") {
   	echo ("To: ".$toText."<br>\r\nSubject: ".$subjectText."<br>\r\n".$msgText."<br>\r\n".$header);
		}
?>

<p><b><font color="#000000" size="5">Test Email</font></b></p>
<font size="4" color="#000000">

<!--   <form method="POST" action="<?php echo($PHP_SELF)?>"> -->
  <form method="POST" action="Email.php" onsubmit="return check();">
    <p><font size="3"><b>Name: <input type="text" name="nameText" id="nameText" size="46"></b></font></p>
    <p><font size="3"><b>Email: <input type="text" name="fromText" id="fromText" size="46"></b></font></p>
    <input type="hidden" name="subjectText" value="Web Mail">
    <p><font face="Arial" size="3"><b>Message Text:</b></font></p>
    <p><font face="Arial" size="3"><b><textarea rows="6" name="msgText" cols="60"></textarea></b></font></p>
    <p><font size="3"><b><input type="submit" value="Send" name="send" style="font-family: Arial; font-size: 12pt; font-weight: bold"></b></font></p>
    <input type="hidden" name="state" value="1">
  </form>
  <b><font face="Arial" size="4" color="#e00000"><?php echo($announce)?></font></b><br><br>

</font>
</td></tr>
</table> 
</div>


</body>
</html>

Open in new window

0
 
LVL 31

Assisted Solution

by:Marco Gasi
Marco Gasi earned 1336 total points
ID: 35503020
Try this:

<?php

$errorMsg = '';

if ($_POST['gender'] == '-select-'){
  errorMsg .= "\n  Gender     Select a gender";
}
if ($_POST['name'] == ''){
  errorMsg .= "\n  Name       Enter a name";  
}
if ($_POST['email'] == '' || strpos($_POST['email']) === false){
  errorMsg .= "\n  E-mail     Enter a valid E-mail address";

if (errorMsg != ""){
            msg =  "Error: Not all fields are filled in.\n";
            msg .= "\n";
            msg .= "Please correct following fields:\n";
           
            errorMsg .= alert(msg + errorMsg + "\n\n");
echo "
  <script type='text/javascript'>
    alert(errorMsg);)
  </script>
";
}
?>
0
 

Author Comment

by:Stef Merlijn
ID: 35505782
marqusG:
If Java is turned off, the user won't see the message (I guess).
0
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 1336 total points
ID: 35506037
Yes. Php doesn't allow to create something like javascript alert. If you want be sure user see message even with javascript disabled, you can make several things:

1): you have to print the message in a blank page and place a link to return to the form.;
2) alternatively, you can post the form to the same page and print error message above the form or within the form itself

Cheers
0
 

Author Closing Comment

by:Stef Merlijn
ID: 35510333
Thank you all.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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 look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

830 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