Solved

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

Posted on 2014-11-30
13
83 Views
Last Modified: 2014-12-08
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
Comment
Question by:James Murrell
  • 6
  • 5
  • 2
13 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 40472742
You're wanting to check this at the browser or at the server?
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40472769
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
 
LVL 31

Author Comment

by:James Murrell
ID: 40473179
Hi Gary - ideally browser

Ray -  thanks for this bit lost on me a mo...
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 58

Expert Comment

by:Gary
ID: 40474069
Ok, then where exactly is 85 coming from?
0
 
LVL 31

Author Comment

by:James Murrell
ID: 40482501
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
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 100 total points
ID: 40482620
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
 
LVL 58

Expert Comment

by:Gary
ID: 40483003
What happens when someone selects

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

You only count the 20?
0
 
LVL 31

Author Comment

by:James Murrell
ID: 40484359
Gary yes the 8 is my error
0
 
LVL 58

Accepted Solution

by:
Gary earned 400 total points
ID: 40484787
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
 
LVL 31

Author Comment

by:James Murrell
ID: 40487613
cool Gary I will test and get back to you as soon as possible
0
 
LVL 31

Author Comment

by:James Murrell
ID: 40487629
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
 
LVL 58

Expert Comment

by:Gary
ID: 40487638
At the beginning presumably - I don't know what you are supposed to be doing based on the two conditions.
0
 
LVL 31

Author Closing Comment

by:James Murrell
ID: 40488292
Thanks for help. sorry i took long time replying
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
000webhost.com default error log 1 28
How can I make this form submit to itself? 10 28
PHP strings vs array errors 13 23
Checkout Page Input Field not aligned 1 23
"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

803 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