Solved

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

Posted on 2014-11-30
13
80 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
Comment Utility
You're wanting to check this at the browser or at the server?
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
Hi Gary - ideally browser

Ray -  thanks for this bit lost on me a mo...
0
 
LVL 58

Expert Comment

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

Author Comment

by:James Murrell
Comment Utility
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 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 100 total points
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 58

Expert Comment

by:Gary
Comment Utility
What happens when someone selects

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

You only count the 20?
0
 
LVL 31

Author Comment

by:James Murrell
Comment Utility
Gary yes the 8 is my error
0
 
LVL 58

Accepted Solution

by:
Gary earned 400 total points
Comment Utility
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
Comment Utility
cool Gary I will test and get back to you as soon as possible
0
 
LVL 31

Author Comment

by:James Murrell
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for help. sorry i took long time replying
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now