gpt4 book ai didi

javascript - 使用 ajax 和 jQuery 发送 HTML 表单到 PHP

转载 作者:行者123 更新时间:2023-12-03 04:33:55 25 4
gpt4 key购买 nike

我正在尝试使用 jQuery 库和 Ajax 来迈出 JavaScript 的第一步。我想读取 HTML 表单并接收 PHP 文件中的用户输入。以下是通过 Ajax 将表单发送到网络服务器的 JavaScript 函数:

function get_post( ) {

$.ajax({
type : "post",
url : "ajax.php",
data : $("form").serialize(),
cache : false,
success: function( data ) {
console.log( data );

}
});

};

在console.log中,显示了正确的输入数据以及在ajax.php中编程的PHP调试输出。console.log(data) 给出:

Parameter: temperature
Sensorknoten: Node1
Start: 2017-04-20 14:15
End: 2017-04-06 06:30

这显然是正确的。

在浏览器中回显 PHP 变量会给出:

Parameter: FIELD EMPTY Sensorknoten: FIELD EMPTY Start: FIELD EMPTY End: FIELD EMPTY

ajax.php 调用名为 UsersInput 的类,该类应使用全局 $_POST 检索用户输入。 UserInput 中不同的 $_POST 方法与此类似

public function getNodes() {
$location = isset($_POST['location'])?$_POST['location']:"FIELD EMPTY";
return $location;
}

ajax.php:

<?php
require_once 'includes/autoload.php';


/*
* get users input from HTML-Form
* @param object: $userIn
*/

$userIn = new UserInput();
$param = $userIn->getParams();
$node = $userIn->getNodes();
$start = $userIn->getStartdate();
$end = $userIn->getEnddate();
//debug output
echo "Parameter: $param";
echo"<br />";
echo "Sensorknoten: $node";
echo"<br />";
echo "Start: $start";
echo"<br />";
echo "End: $end";
echo"<br />";
?>

这是 HTML 表单:

<form>
<input type="hidden" name="nodeformsend" value="true">
<!-- Page Heading/Breadcrumbs -->
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">Welcome!
<small>Now, you are able to visualize your Data</small>
</h1>
<ol class="breadcrumb">
<li><a href="index.html">Sensor Monitoring</a>
</li>
<li class="active">Visualize Data</li>
</ol>
</div>
</div>
<!-- table to select different Nodes -->
<div class="row">
<div class="col-md-3">
<div class="panel panel-default text-center">
<div class="panel-heading">
<h3 class="panel-title">Select Wireless Sensor Sodes</h3>
</div>
<div class="col-md">
<select class="form-control" id="location" name="location">
<?php for($i = 1; $i < 13; $i++) : ?>
<option name="location[<?php echo $i ?>]" value="Node<?php echo $i ?>">Wireless Senssor Node <?php echo $i ?></option>
<?php endfor; ?>
</select>
</div>
</div>
</div>
<!-- table to select different parameters -->
<div class="col-md-3">
<div class="panel panel-default text-center">
<div class="panel-heading">
<h3 class="panel-title">Select Parameteres</h3>
</div>
<div class="col-md">
<select class="form-control" id="parameter" name="parameter">
<option name="parameter[0]" value="temperature">Temperature</option>
<option name="parameter[1]" value="humidity">Humidity</option>
<option name="parameter[2]" value="pressure">Pressure</option>
</select>
</div>
</div>
</div>
<div class="col-md-3">
<div class="panel panel-default text-center">
<div class="panel-heading">
<h3 class="panel-title">Begin of Timestamp</h3>
</div>
<div class="input-group date" id="datetime-start" data-date-format="dd-mm-yyyy" >
<div class="input-group-addon">
<i class="glyphicon glyphicon-calendar"></i>
</div>
<input class="form-control" name="datetime-start" type="text" placeholder="YYYY-MM-DD HH:MM">
</div>
</div>
</div>
<!-- table to select Timestamps-->
<div class="col-md-3">
<div class="panel panel-default text-center">
<div class="panel-heading">
<h3 class="panel-title">End of Timestamp</h3>
</div>
<div class="input-group date" id="datetime-end" data-date-format="dd-mm-yyyy" >
<div class="input-group-addon">
<i class="glyphicon glyphicon-calendar"></i>
</div>
<input class="form-control" name="datetime-end" type="text" placeholder="YYYY-MM-DD HH:MM">
</div>
</div>
</div>
</div>
<input id="submit" type="button" value="submit" onclick="get_post();">

编辑

是的,当然!这是:

<?php
//__________This class parse the usersinput UI__________

class UserInput{
/**
* get Nodes from UsersInput
* $location = Node[i]
*/
public function getNodes() {
$location ='';
$location = isset($_POST['location'])?$_POST['location']:"FIELD EMPTY"; //load the inputs of user to a variable as an array
return $location;
}
/**
* get Nodes from UI
* $parameter = Temperature, Humidty, Pressure
*/
public function getParams() {
$parameter = isset($_POST['parameter'])?$_POST['parameter']:"FIELD EMPTY";
return $parameter;
}
/**
* get Nodes from UI
* $start = 'YYYY-mm-DD hh:ii:ss < $end
*/
public function getStartdate() {
$start = isset($_POST['datetime-start'])?$_POST['datetime-start']:"FIELD EMPTY";
return $start;
}
/**
* get Nodes from UI
* $end = 'YYYY-mm-DD hh:ii:ss
*/
public function getEnddate() {
$end = isset($_POST['datetime-end'])?$_POST['datetime-end']:"FIELD EMPTY";
return $end;
}
/**
* get Nodes from UI
*/
}

最佳答案

尝试使用 $_POST 作为参数而不是全局变量。 $_POST 的值已更改。或者,由于您有一个类,因此您可以利用面向对象编程:现在我们添加一个构造函数,并在实例化该类时将 $_POST 作为参数传递。这样我们就不用担心从 $_POST

访问

此外,如果您要将这些详细信息保存到数据库中,请正确清理输入字符串

$userIn = new UserInput($_POST);
$param = $userIn->getParams();
$node = $userIn->getNodes();
$start = $userIn->getStartdate();
$end = $userIn->getEnddate();

在你的类(class)

<?php
//__________This class parse the usersinput UI__________

class UserInput{

var $variables = array();

public function __construct($posted_variables){
try{
if(is_array($posted_variables) && isset($posted_variables)){
$this->variables = $posted_variables;
}else{
throw new Exception('Post is empty'.var_dump($variables));
}
}catch(Exception $ex){
echo $ex->getTraceAsString();
}
}

/**
* get Nodes from UsersInput
* $location = Node[i]
*/
public function getNodes() {
$location ='';
$location = isset($this->variables['location'])?$this->variables['location']:"FIELD EMPTY"; //load the inputs of user to a variable as an array
return $location;
}
/**
* get Nodes from UI
* $parameter = Temperature, Humidty, Pressure
*/
public function getParams() {
//example:
$parameter = isset($this->variables['parameter'])?$this->variables['parameter']:"FIELD EMPTY";
return $parameter;
}
/**
* get Nodes from UI
* $start = 'YYYY-mm-DD hh:ii:ss < $end
*/
public function getStartdate() {
$start = isset($this->variables['datetime-start'])?$this->variables['datetime-start']:"FIELD EMPTY";
return $start;
}
/**
* get Nodes from UI
* $end = 'YYYY-mm-DD hh:ii:ss
*/
public function getEnddate() {
$end = isset($this->variables['datetime-end'])?$this->variables['datetime-end']:"FIELD EMPTY";
return $end;
}
/**
* get Nodes from UI
*/
}

关于javascript - 使用 ajax 和 jQuery 发送 HTML 表单到 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43370084/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com