php实战手册(2)

水宝石的技术博客 / 2023-08-08 / 原文

目录
  • 变量
  • 接收表单数据

变量

  • 定义
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>php demo</title>
  </head>
  <body>
	  <?php $x=11; ?>
	  <p>
	  php hello,world
	  </p>
	   <?php 
	   //我是注释行
	   echo $x*$x; 
	   /*
	   注释行1
	   注释行2
	    */
	    ?>
  </body>
</html>

image

  • 变量内插与null
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>php demo</title>
  </head>
  <body>
	  <p>
	  php hello,world
	  </p>
	   <?php 
	   //我是注释行
	   $x=null;
	   if (is_null($x)){ 
			echo '$x is null';
			$x=22;
		}
		?>
		<br/>
		<?php
		if ($x>11)
			echo "$x >11";
		else
		    echo $x*$x;
	    ?>
  </body>
</html>

image

接收表单数据

  • 表单submit
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>php demo</title>
  </head>
  <body>
	  <p> 
	  <form name="input_x_form" action="index.php" method="POST">
	  <label for="x">x:</label>
	  <input type="text" id="x" name="x"  size="10" />
	  <button id="btn_ok" type="submit" >确定</button>
	  </p>
	   <?php 
	   $x=$_POST["x"];
	   if (is_null($x)){ 
			echo '$x is null';
			$x=22;
		}
		?>
		<br/>
		<?php
		if ($x>11)
			echo "$x >11";
		else
		    echo $x*$x;
	    ?>
  </body>
</html>

image
image

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>php demo</title>
  </head>
  <body>
	  
	  <form name="input_form" action="index.php" method="POST">
	  <p> <label>
        Name:
          <input type="text" id="name" name="name"  required minlength="2" maxlength="8" size="10" />
	   </label>
	   <br/>
	  <label for="x">x:</label>
	  <input type="text" id="x" name="x" />
	  </p>
	   <p> 
      <button id="btn_ok" type="submit" >确定</button>
	  </p>

	   <?php 
	   //我是注释行
	   $x=$_POST["x"];
	   $name=$_POST["name"];
	   echo "hello,$name<br/>";
	   if (is_null($x)){ 
			echo '$x is null';
			$x=22;
		}
		?>
		<br/>
		<?php
		if ($x>11)
			echo "$x >11";
		else
		    echo $x*$x;
	    ?>
  </body>
</html>

image

  • 使用js 提交
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>php demo</title>
  </head>
  <body>
	  
	  <form name="input_form" action="index.php" method="POST">
	  <p> <label>
        Name:
          <input type="text" id="name" name="name"  required minlength="2" maxlength="8" size="10" />
	   </label>
	   <br/>
	  <label for="x">x:</label>
	  <input type="text" id="x" name="x" />
	  </p>
	   <p> 
      <button id="btn_ok" type="button" onClick="get_data()">确定</button>
	  </p>
	  <script  type="text/javascript">
		  function get_data(){
			  document.input_form.submit();
		  }
	  </script>
	   <?php 
	   //我是注释行
	   $x=$_POST["x"];
	   $name=$_POST["name"];
	   echo "hello,$name<br/>";
	   if (is_null($x)){ 
			echo '$x is null';
			$x=22;
		}
		?>
		<br/>
		<?php
		if ($x>11)
			echo "$x >11";
		else
		    echo $x*$x;
	    ?>
  </body>
</html>

image

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>php demo</title>
  </head>
  <body>
	  
	  <form name="input_form" action="index.php" method="POST">
	  <p> <label>
        Name:
          <input type="text" id="name" name="name"  required minlength="2" maxlength="8" size="10" />
	   </label>
	   <br/>
	  <label for="x">x:</label>
	  <input type="text"  id= "x" name="x" />
	  </p>
	   <p> 
      <button id="btn_ok" type="button" onclick="get_data()">确定</button>
	  </p>
	  <script  type="text/javascript">
		  function get_data(){
		       let  x=document.getElementById("x").value;
			   if (isNaN(Number(x))) {
				   document.getElementById("x").value=0;
				   alert("请输入正确的x");
			   }
			  document.input_form.submit();
		  }
	  </script>
     <?php 
	   //我是注释行
	   $x=$_POST["x"];
	   $name=$_POST["name"];
	    echo "hello,$name<br/>";
       if ($x>11)
			echo "$x >11";
		else
		    echo "$x <=11";
	 ?>
  </body>
</html>

image
image