【问题标题】:Web HTML Form handling the data处理数据的 Web HTML 表单
【发布时间】:2016-07-04 18:49:34
【问题描述】:

我使用 html 创建了一个表单,该表单主要获取人的姓名、电子邮件、手机号码以及从一系列单选按钮选项中选择的选项。我看过几个教程,但我仍然对如何收集所有用户输入数据感到困惑。我会做php和mysql吗?如果是这样,我该如何开始?非常感谢大家!

【问题讨论】:

标签: php html mysql web


【解决方案1】:

基本上是这样的:

function checkData(){
	var firstname=document.getElementById("firstname").value; // access firstname id field
	var lastname=document.getElementById("lastname").value;  // access name id field
	if(firstname==""){ // if first name field is empty
		alert("Please fill the firstname field");
		return false; // you don't send the data
	}else if(lastname==""){ // if the lastname field is empty
		alert("Please fill the lastname field");
		return false; // you don't send the data
	} else{ // else you can send the data to action_page.php
		return true;
	}
}
<form action="action_page.php" method="post" onsubmit="return checkData();">
  First name:<br>
  <input type="text" name="firstname" id="firstname"><br>
  Last name:<br>
  <input type="text" name="lastname" id="lastname"><br><br>
  <input type="submit" value="Submit">
</form>

在您的表单中,我告诉过我会将数据发送到方法中的action_page.php 文件(必须是.php 文件,因为如果有php,即使是单行,您的文件也必须是.php) post

有两种发送数据的方式:

  • get:数据将通过 URL 发送(在我的示例中,您将有类似:action_page.php?lastname=Potter;firstname=James
  • post:数据将被发送,用户看不到它们

在您的action_page.php 文件中,如果数据是使用 GET 或 $_POST['nameOfFIeld'] 发送的,您可以使用 $_GET['nameOfFIeld'] 访问字段。再一次,在我的空里你会有一些东西:

$firstname=$_POST["firstname"];
$lastname=$_POST["lastname"];

然后你可以做你想做的,你得到你发送的2个字段

这只是一个简单的示例,向您展示它是如何工作的。如果您不知道如何获取数据,您应该了解更多关于 PHP 语言的知识:

简而言之:在您的 .html 文件中的表单中,您可以在表单字段中说明将数据发送到何处以及如何发送(不在其中的输入将不会发送)。一个 JavaScript 函数,用于检查您的数据是否格式正确(检查长度或它们是否为空)。如果不应该发送数据,javascript 函数将返回 false 或 true。然后在您的 .php 文件中,您可以使用以下方法访问名称:$_method['namefile'];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-29
    • 2016-06-14
    • 2011-02-16
    • 2016-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-30
    相关资源
    最近更新 更多