2017-04-12 103 views
2

所以,我是新來的JavaScript/jquery,但我已經玩足夠長的PHP。我知道如何從PHP的輸入,這是很容易獲得的數據,而是試圖做與jQuery一樣時,該怎麼辦呢只是蒼蠅在我的頭上。從jQuery發送JSON文件到PHP沒有AJAX

現在我有這樣的腳本:

<script type="text/javascript"> 
    function onSubmit(form){ 
     var data = JSON.stringify($(form).serializeArray()); 
     console.log(data); 
    } 
</script> 

這種形式:

<form onsubmit='return onSubmit(this)'> 
    <input type="date"/><br/> 
    <input type="date"/><br/> 
    <input type="submit" name=""/> 
</form> 

我看到它記錄了以.json文件就好了([{"name":"from","value":"1994-01-01"},{"name":"to","value":"1994-02-02"}])。我的猜測是它幾乎發送上傳.json到PHP文件,然後做一個$ _ POST,但我不知道如何從這裏着手或做。我不知道,如果AJAX是必要或沒有,如果沒有,該怎麼辦呢,沒有它(的一切,我發現在這裏使用AJAX)。

+1

是你能解決這個問題? – julekgwa

+1

@julekgwa是的。解決方案是在'var results = JSON.parse(data);'中,然後在循環中使用'results [i] .probeTemp'。我只是執行得很糟糕。 – Newwt

+0

@julekgwa哦,'$ postVars = $ request-> getParsedBody();'和'$ var = $ request-> getAttribute('var');' – Newwt

回答

0

PHP是處理服務器端。

如果要將數據發送到「PHP」,則需要請求服務器(如果不想更改當前頁面或使用數據調用新URL,則通過Ajax請求)。

我知道如何從一個輸入與PHP

這是一個非常錯誤的說法,因爲PHP不能從輸入獲取數據,因爲它是服務器端獲取數據。 瀏覽器將數據發送到PHP調用服務器。

+0

嗯......我正在考慮在PHP文件一旦我「提交」.json文件,所以頁面不會重定向我。沒有想到這一點。我想我得進入阿賈克斯畢竟:/ – Newwt

0

相反,定義一個路徑將您的輸入數據發佈到您的php文件,然後通過表單,您可以簡單地method ='POST'而不是使用ajax發送您的數據。

+0

我看。 '$ _POST ['data']'應該可以正常工作,那麼? – Newwt

+0

我使用Express。JS來定義發佈數據的路由,但我假設你在你的php文件中有你的路由以及連接到你的數據庫,所以我的假設是,如果你在後端有連接,數據將通過方法發佈。 – Ozan

0

您也可以使用XML HTTP請求。

這裏是一個例子。

var http = new XMLHttpRequest(); 
var url = "get_data.php"; 
var params = "lorem=ipsum&name=binny"; 
http.open("POST", url, true); 

//Send the proper header information along with the request 
http.setRequestHeader("Content-type ", "application/x-www-form-urlencoded"); 

http.onreadystatechange = function() {//Call a function when the state changes. 
    if(http.readyState == 4 && http.status == 200) { 
     alert(http.responseText); 
    } 
} 
http.send(params); 

這是來自這裏的答案。

Send POST data using XMLHttpRequest

1

您可以發送表單數據作爲標準的URL編碼符號的文本字符串或JSON像字符串jQuery.serialize()

<form id="set-date-form"> 
    <input name="from" type="date"/><br/> 
    <input name="to" type="date"/><br/> 
    <input type="submit" id="set-date"/> 
</form> 

jQuery

<script> 
    $('#set-date').on('click', function (e) { 
     e.preventDefault(); 
     var data = $('#set-date-form').serialize(); 
     $.post('somephpfile.php', data, function (response) { 
      // response is the data echoed from php 
      var result = JSON.parse(response) // assuming that php echoed ['success' => true/false]; 
      if (result.success == true) { 
       alert("the values were sent to db successfully"); 
      }else { 
       alert("the values were not sent to db successfully"); 
      } 
     }) 
    }) 
</script> 

然後在您PHP文件

<?php 

$from = $_POST['from']; 
$to = $_POST['to']; 

// here you can update the database with this values 
// after updating db or insert 
// check if the query was successful 
// if query was successful [echo json_encode(['success' => true]);] this value will be sent to javascript as a response 
// if query was not successful [echo json_encode(['success' => false]);] this value will be sent to javascript as a response