Solved

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

Posted on 2014-11-30
13
85 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 110

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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
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 110

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

749 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