Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 94
  • Last Modified:

How do i add 4 form fields to check if over 85

Hiya, i am learning php and enjoying it but i am having trouble with doing a calculation of 4 fields to make sure the user hasn;t gone over 85...

all help will be great -


code -----
<form id="form1" name="form1" method="post" action="signup.php">

<label for="Email"><br><br>Email </label><input type="text" name="Email" id="Email" required  />
<br class="clear" /> 
<label for="FullName"><br>Fullname </label><input type="text" name="FullName" id="FullName" required />
<label for="TeamName"><br><br>Teamname </label><input type="text" name="TeamName" id="TeamName" required /> 
<label for="Password"><br><br>Password </label><input type="password" name="Password" id="Password" required />
	<br>
<br class="clear" /> 
<label for="Driver1">Driver1 </label><select name="Driver1" id="Driver1" required>
<option value="12 Sebastian VETTEL">Sebastian VETTEL</option>
<option value="12 Daniel RICCIARDO">Daniel RICCIARDO</option>
<option value="15 Lewis HAMILTON">Lewis HAMILTON</option>
<option value="15 Nico ROSBERG">Nico ROSBERG</option>
<option value="13 Fernando ALONSO">Fernando ALONSO</option>
<option value="12 Kimi RAIKKONEN">Kimi RAIKKONEN</option>
<option value="12 Romain GROSJEAN">Romain GROSJEAN</option>
<option value="10 Pastor  MALDONADO">Pastor  MALDONADO</option>
<option value="12 Jenson BUTTON">Jenson BUTTON</option>
<option value="11 Kevin MAGNUSSEN">Kevin MAGNUSSEN</option>
<option value="10 Nico HULKENBERG">Nico HULKENBERG</option>
<option value="9  Sergio PEREZ MENDOZA">Sergio PEREZ MENDOZA</option>
<option value="8  Adrian SUTIL">Adrian SUTIL</option>
<option value="7  Esteban GUTIERREZ">Esteban GUTIERREZ</option>
<option value="6  Jean-Eric VERGNE">Jean-Eric VERGNE</option>
<option value="5  Daniil KVYAT">Daniil KVYAT</option>
<option value="11  Felipe MASSA">Felipe MASSA</option>
<option value="14  Valtteri BOTTAS">Valtteri BOTTAS</option>
<option value="4  Jules BIANCHI">Jules BIANCHI</option>
<option value="3  Max CHILTON">Max CHILTON</option>
<option value="2  KAMUI KOBAYASHI">KAMUI KOBAYASHI</option>
<option value="2  MARCUS ERICSSON">MARCUS ERICSSON</option>
</select>
<br>
<br class="clear" /> 
<label for="Driver2">Driver2 </label><select name="Driver2" id="Driver2" required>
<option value="12 Sebastian VETTEL">Sebastian VETTEL</option>
<option value="12 Daniel RICCIARDO">Daniel RICCIARDO</option>
<option value="15 Lewis HAMILTON">Lewis HAMILTON</option>
<option value="15 Nico ROSBERG">Nico ROSBERG</option>
<option value="13 Fernando ALONSO">Fernando ALONSO</option>
<option value="12 Kimi RAIKKONEN">Kimi RAIKKONEN</option>
<option value="12 Romain GROSJEAN">Romain GROSJEAN</option>
<option value="10 Pastor  MALDONADO">Pastor  MALDONADO</option>
<option value="12 Jenson BUTTON">Jenson BUTTON</option>
<option value="11 Kevin MAGNUSSEN">Kevin MAGNUSSEN</option>
<option value="10 Nico HULKENBERG">Nico HULKENBERG</option>
<option value="9  Sergio PEREZ MENDOZA">Sergio PEREZ MENDOZA</option>
<option value="8  Adrian SUTIL">Adrian SUTIL</option>
<option value="7  Esteban GUTIERREZ">Esteban GUTIERREZ</option>
<option value="6  Jean-Eric VERGNE">Jean-Eric VERGNE</option>
<option value="5  Daniil KVYAT">Daniil KVYAT</option>
<option value="11  Felipe MASSA">Felipe MASSA</option>
<option value="14  Valtteri BOTTAS">Valtteri BOTTAS</option>
<option value="4  Jules BIANCHI">Jules BIANCHI</option>
<option value="3  Max CHILTON">Max CHILTON</option>
<option value="2  KAMUI KOBAYASHI">KAMUI KOBAYASHI</option>
<option value="2  MARCUS ERICSSON">MARCUS ERICSSON</option>
</select>
<br>
<br class="clear" /> 
<label for="Team1">Team1 </label><select name="Team1" id="Team1" required>
<option value="9   Red Bull Racing">Red Bull Racing</option>
<option value="131 Mercedes">Mercedes</option>
<option value="6   Ferrari">Ferrari</option>
<option value="208 Lotus">Lotus</option>
<option value="1   McLaren">McLaren</option>
<option value="10  Force India">Force India</option>
<option value="15  Sauber">Sauber</option>
<option value="5   Toro Rosso">Toro Rosso</option>
<option value="3   Williams">Williams</option>
<option value="5   Marussia">Marussia</option>
<option value="207 Caterham">Caterham</option>
</select>
<br><label for="Team2"><br>Team2 </label><select name="Team2" id="Team2" required>
<option value="9   Red Bull Racing">Red Bull Racing</option>
<option value="131 Mercedes">Mercedes</option>
<option value="6   Ferrari">Ferrari</option>
<option value="208 Lotus">Lotus</option>
<option value="1   McLaren">McLaren</option>
<option value="10  Force India">Force India</option>
<option value="15  Sauber">Sauber</option>
<option value="5   Toro Rosso">Toro Rosso</option>
<option value="3   Williams">Williams</option>
<option value="5   Marussia">Marussia</option>
<option value="207 Caterham">Caterham</option></select>&nbsp;
<br>
<br class="clear" /> 
	
<br class="clear" /> 
<INPUT TYPE = "Submit" Name = "Submit" VALUE = "Login">

</form>        

Open in new window

0
James Murrell
Asked:
James Murrell
  • 6
  • 5
  • 2
2 Solutions
 
GaryCommented:
You're wanting to check this at the browser or at the server?
0
 
Ray PaseurCommented:
On the client side you can validate with HTML5 notation using the pattern attribute.  You may want to use JavaScript, too.  It just depends on exactly what you want to validate.  On the server side, you use strlen() or mb_strlen() to determine the length of a data string.  PHP has support for filter_var() and regular expressions, too.

For simplicity in this test, I used a length of 8 instead of 85.
http://iconoun.com/demo/temp_murrell.php
<?php // demo/temp_murrell.php
error_reporting(E_ALL);

// SEE http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28571762.html

$q = !empty($_POST['q']) ? $_POST['q'] : NULL;
if ($q)
{
    echo PHP_EOL . "The length of $q is: " . strlen($q);
}

// CREATE THE FORM WITH HEREDOC NOTATION
$form = <<<ENDFORM
<form method="post">
<input type="text"   name="q" pattern="[A-Za-z ]{1,8}" />
<input type="submit" />
</form>
ENDFORM;

echo $form;

Open in new window

0
 
James MurrellProduct SpecialistAuthor Commented:
Hi Gary - ideally browser

Ray -  thanks for this bit lost on me a mo...
0
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!

 
GaryCommented:
Ok, then where exactly is 85 coming from?
0
 
James MurrellProduct SpecialistAuthor Commented:
apologies for delay I was away with work...

the 85 is coming from the form values: 1st two digits....  so all 4 forms values must not exceed 85

<option value="12
0
 
Ray PaseurCommented:
I may not be understanding the question; can't really figure out where the "85" comes from or what the meaning of "exceed" would be (Total length?  Total value?  Days until an event?  Number of elements in an array?  Etc?)  A good way to ask a question is to follow the guidance of the SSCCE.

If you want to check form input string length(s) this code snippet shows a basic design that will permit that.  You can test it out and see how it works.
http://iconoun.com/demo/temp_murrell.php

If you're new to PHP and want to find some dependable learning resources, this article can help you get started learning PHP the right way.

<?php // demo/temp_murrell.php
error_reporting(E_ALL);

// SEE http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28571762.html

// SET A MAXIMUM LENGTH INPUT VALUE PER DATA FIELD
$maxLength = 8;

// SET A MAXIMUM LENGTH INPUT VALUE PER HTML FORM
$totLength = $maxLength * 4;

// POST REQUEST VARIABLE $s WILL BE SET IF FORM IS SUBMITTED
$s = !empty($_POST['s']) ? $_POST['s'] : NULL;
if ($s)
{
    // ADD UP THE LENGTHS OF THE INPUT FIELDS
    $inpLength
    = strlen($_POST['q1'])
    + strlen($_POST['q2'])
    + strlen($_POST['q3'])
    + strlen($_POST['q4'])
    ;
    echo PHP_EOL . "The total length of input fields is: $inpLength.";

    // TEST TO SEE IF THE LENGTH IS OK
    if ($inpLength <= $totLength)
    {
        echo PHP_EOL . "The total length of input fields is acceptable because it does not exceed $totLength.";
    }
    else
    {
        echo PHP_EOL . "The total length of input fileds is too large because it exceeds $totLength.";
    }
}

// CREATE THE FORM WITH HEREDOC NOTATION
$form = <<<ENDFORM
<form method="post">
<input type="text"   name="q1" pattern="[A-Za-z ]{1,$maxLength}" />
<input type="text"   name="q2" pattern="[A-Za-z ]{1,$maxLength}" />
<input type="text"   name="q3" pattern="[A-Za-z ]{1,$maxLength}" />
<input type="text"   name="q4" pattern="[A-Za-z ]{1,$maxLength}" />
<input type="submit" name="s"  value="Check Input Lengths" />
</form>
ENDFORM;

echo $form;

Open in new window

0
 
GaryCommented:
What happens when someone selects

<option value="208 Lotus">Lotus</option>

You only count the 20?
0
 
James MurrellProduct SpecialistAuthor Commented:
Gary yes the 8 is my error
0
 
GaryCommented:
In signup.php

<?php
if (!empty($_POST)){

	$total = substr($_POST['Driver1'],2) + substr($_POST['Driver2'],2) + substr($_POST['Team1'],2) + substr($_POST['Team2'],2); 

	if($total>85){
		// Over 85
	}
	else{
		// 85 or under
	}
}

Open in new window

0
 
James MurrellProduct SpecialistAuthor Commented:
cool Gary I will test and get back to you as soon as possible
0
 
James MurrellProduct SpecialistAuthor Commented:
sorry Gary unsure where abouts to put in signup.php



here is copy of file signup.php ,

   <link rel="stylesheet" href="css/style.css" media="screen" type="text/css" />





<?php

// Require the file that connect to the database.
// It is good practice to put the database connection
// information in a separate file.
require('db.php');

// Since we used the post method in our form, we can
// securely call our data using the $_POST predefined
// variable, with parameters specified by the name
// attribute in our form.
$submissionID =mysql_real_escape_string($_POST['submissionID']);  
$Email =mysql_real_escape_string($_POST['Email']);  
$FullName =mysql_real_escape_string($_POST['FullName']);  
$Driver1 =mysql_real_escape_string($_POST['Driver1']);  
$Driver2 =mysql_real_escape_string($_POST['Driver2']);  
$Team1 =mysql_real_escape_string($_POST['Team1']);  
$LewisEndofyearPosition =mysql_real_escape_string($_POST['LewisEndofyearPosition']);  
$Team2 =mysql_real_escape_string($_POST['Team2']);  
$Total =mysql_real_escape_string($_POST['Total']);  
$TeamName =mysql_real_escape_string($_POST['TeamName']);  
$Password =mysql_real_escape_string($_POST['Password']); 
 $pw = md5($Password);
$MyProducts =mysql_real_escape_string($_POST['MyProducts']);  



//Check to see if the username is already taken
$query = "SELECT TeamName FROM f1users WHERE TeamName = '".mysql_real_escape_string($TeamName)."'";
$result = mysql_query($query) or die(mysql_error()); // Get an array with the clients
while($row = mysql_fetch_array($result))
{ // For each instance, check the username
	if($row['TeamName'] == $TeamName)
	{
		$usernameTaken = true;
	}else{$usernameTaken = false;
}
}


if($usernameTaken)
{
	echo "That TeamName has been taken.";
}


// If our email or PayPal addresses are invalid
else if(!eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$", $Email))
{
     echo "The email address you entered is invalid."; // Tell the user
}
else{

// Finally, we use a MYSQL_QUERY to insert our information
// into the database. INSERT INTO defines what fields we want
// to insert information, and VALUES defines the values that
// we are entering.
$result= MYSQL_QUERY(
		 "INSERT INTO f1users (submissionID,Email,FullName,Driver1,Driver2,Team1,LewisEndofyearPosition,Team2,Total,TeamName,Password,MyProducts)".
		 "VALUES ('','$Email', '$FullName', '$Driver1', '$Driver2', '$Team1', '$LewisEndofyearPosition', '$Team2', '$Total', '$TeamName', '$pw', '$MyProducts')"
		 );



echo "Thank you for signing up.";
	// Then, redirect them to the profile page
echo "<p><a href='http://cs97jjm3.no-ip.org/F1/members_area/index.html'>Login</a></p>"; 
}
?>

Open in new window

0
 
GaryCommented:
At the beginning presumably - I don't know what you are supposed to be doing based on the two conditions.
0
 
James MurrellProduct SpecialistAuthor Commented:
Thanks for help. sorry i took long time replying
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 6
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now