簡體   English   中英

發送帶有ajax和jQuery的HTML表單到PHP

[英]sending HTML-form with ajax and jQuery to PHP

我正在嘗試使用jQuery-library和Ajax使用JavaScript邁出第一步。 我想閱讀HTML表單,並在PHP文件中接收用戶輸入。 以下是一個JavaScript函數,用於通過Ajax將表單發送到Web服務器:

function get_post( ) {

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

        }
    });

};

在console.log中,顯示了正確的輸入數據,並顯示了PHP-debug-output,該輸出在ajax.php中進行了編程。 console.log(data)提供:

參數:溫度
傳感器結:Node1
開始:2017-04-20 14:15
結束於:2017-04-06 06:30

這顯然是正確的。

在瀏覽器中顯PHP變量將得到:

參數:FIELD EMPTY傳感器結:FIELD EMPTY開始:FIELD EMPTY結束: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();

在你的課上

<?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
     */
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM