JS and PHP code to display the maximum value after the user enters a number

Source: Internet
Author: User
Tags explode html header string to number
1. Use JS's prompt to enter the value that the user wants to enter.

2. Use the text label of the HTML form to pass the input value to the PHP processing file

3.PHP to determine the maximum value and position.

This article mainly and everybody introduced the js+php realizes the user to enter the number to display the biggest value and is the first several related position, the article gives the detailed sample code for everybody to refer to the study, needs the friend below to look together, hoped can help everybody.

Digest

1. Gets the maximum value of the array in the JS implementation.

var a=[10,20,40,30]; var max=0; var max=a[0]; for (Var i=0;i<a.length;i++) {if (Max<a[i]) {  max=a[i];}} alert ("Maximum number is" +max);

It is worth noting here: You can not directly set a variable max=0, because you are not sure whether the number entered later is less than 0 or greater than 0 [the case is greater than 0], so you should select a number inside the array, on the array of the first good max=a[0].

Because the JS language is based on the object-oriented process, JS inside all things can be objects, so its array has attributes, length is a JS array to get the array length of the property, with this property can iterate over the array, and then one by one comparison.

2. In the JS implementation get the minimum value of the array and their position (that is, in the array of the first few)

[Note: This case position +1 is for convenient viewing, JS array Start is also 0]

var a=[10,20,40,30]; var max=a[0];//cannot specify a number as the maximum value [var max=0] (unless the case is determined), should use the value inside the array, a[0] that is, the first value of the array as the maximum value of the comparison. var maxaddress=0; var min=a[0]; var minaddress=0; for (Var i=0;i<a.length;i++) {  if (Max<a[i]) {   max=a[i];   Maxaddress=i  }  if (Min>a[i]) {   min=a[i];   Minaddress=i  }} alert ("Maximum number for" +max+ "position is" + (maxaddress+1) + "x"); Alert ("Minimum number for" +min+ "position is" + (minaddress+1) + "x");

3. Now change to use prompt for user input to get the value to find the maximum value and so on.

Difficulties:

1. How to enter, with prompt

2. How do I convert a string to an array and format it?

Prerequisite Knowledge:

1. Divide the string into the spilt method of the JS string object (Note that JS is all objects, so called methods, not functions)

2. Convert an "ABC" string to number type.

A string to a numeric type number★ a string of numeric type, converted to the resulting number. var n1= "123"; var n2=number (N1);//123★★ non-numeric string, converted to be Nan. var n1= "123ABC"; var n2=number (N1);//nan★ a string of decimal type, after which the original number is obtained. var n1= "123.23"; var n2=number (N1),//123.23 parseint★ integer numeric type string, converted to get an integer number. var n1= "123"; var n2=parseint (N1); The string at the beginning of the//123★ number, which is the number in front of the conversion. var n1= "123ABC"; var n2=parseint (N1);//123★ A string that starts with a non-digit, which is Nan after the conversion. var n1= "abc123"; var n2=parseint (N1);//nan★★ a string of fractional type, rounded after conversion (the decimal point is omitted directly). var n1= "123.53"; var n2=parseint (n1);//123 var n1= "-5.93"; var n2=parseint (N1),//-5 parsefloat★ integer numeric type string, converted to get an integer number. var n1= "123"; var n2=parsefloat (N1); The string at the beginning of the//123★ number, which is the number in front of the conversion. var n1= "123ABC"; var n2=parsefloat (N1);//123★ A string that starts with a non-digit, and the conversion gets Nan. var n1= "abc123"; var n2=parsefloat (N1);//nan★★ a string of decimal type, after which the original number is obtained. var n1= "123.23"; var n2=parsefloat (N1);//123.23 to Boolean type ★ number and string after the turn is true. ★undefined, NULL, 0 turn after the end of the False.var n1=123; var N2=boolean (N1);//truevar n1= "123"; var N2=boolean (N1);//truevar n1= "0"; var N2=boolean (N1); alert (n2);//truevar N1; var N2=boolean (N1);//faLsevar N1=null; var N2=boolean (N1);//falsevar n1=0; var N2=boolean (N1);//false

The above analysis is complete, start writing code

<script>  //var a=new Array ( -12,34,56,88,32, "AA", " -34.33", " -34.34", 12.23);  /*var a=new Array (Prompt ("Please enter Number"));  document.write (a); */  var b=prompt ("Please enter the number to compare, use \", \ "number to separate");  var a=new Array ();  A=b.split (",");  for (Var j=0;j<a.length;j++) {  //   document.write (A[j]);  }  alert (typeof (a) + "JS Array essence is Object Ah!!"); /essence is an object PHP array is an array, no attributes (such as length)  document.write (A + "<br>");  Console.log (A + "<br>");  var max=parsefloat (a[0]);  document.write (max);  var maxaddress=0;  var min=parsefloat (a[0]);  var minaddress=0;    for (Var i=0;i<=a.length;i++) {   var shu=parsefloat (A[i]);   if (Shu>max) {    Max=shu;    maxaddress=i;   }   if (shu<min) {    min=a[i];    minaddress=i;   }  }  document.write ("Maximum number for" +max+ "position is" + (maxaddress+1) + "+" <br> ");  document.write ("Minimum number for" +min+ "position is" + (minaddress+1) + "x"); </script>

4. The above is JS do processing, my goal is to let js+php+html linkage up.

PHP is not able to directly interact with the front end, that is, he can not directly get the user input values, but through the form of HTML forms

JS data to PHP, you can use Ajax, but I will say later, today to see what methods.

1. Make an HTML form:

<! DOCTYPE html>

2.JS Pass the value to HTML:

<script type= "Text/javascript" >  var a=prompt ("Please enter Numbers");  document.getElementById ("Shu"). Value=a; </script>

Here is a hole, if the JS code in the HTML header, will error, said uncaught Typeerror:cannot set Property ' value ' of NULL

Because the browser parsing sequence, in the browser parsing JS (when the input number is completed), the browser began to parse the HTML, although JS assigned the value of text, but by the subsequent parsing of the HTML text value is NULL, this is a contradiction.

So as far as possible to let the JS code written in the back, you first parse HTML, and then I give you value. (Specific analysis, not necessarily JS code in the head)

5. Well, the HTML is worth it, now look at PHP

<?phpheader ("Content-type:text/html;charset=utf-8"), $a =$_post[' Shuzi '];//var_dump ($a); $b =explode (', ', $a); for ($i =0; $i <count ($b); $i + +) {echo $b [$i];} Var_dump ($b); $max = $b [0]; $maxaddress =0;for ($j =0; $j <count ($b); $j + +) {if ($b [$j]> $max) {  $max = $b [$j];  $maxaddress = $j; }}echo "Maximum value is". $max. " <br> "; echo" position is "first". ($maxaddress + 1). " A ";? >

The main point here is to see explode (splitting the string into an array) count (get the number of arrays)

Also pay attention to the type conversion problem, add later or write it yourself.

At last:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.