不使用AJAX将JSON文件从jQuery发送到PHP

时间:2021-05-05 09:51:34

So, I'm new to javascript/jquery, but I have played around long enough with PHP. I know how to get data from an input with PHP, which is really easy, but when trying to do the same with jQuery, how to do it just flies over my head.

所以,我是javascript/jquery的新手,但我已经用PHP玩够了。我知道如何使用PHP从输入中获取数据,这非常简单,但是当我尝试使用jQuery时,如何从我的脑海中跳出来。

Right now I have this script:

现在我有了这个剧本:

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

And this form:

和这种形式:

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

I see it logs the .json file just fine ([{"name":"from","value":"1994-01-01"},{"name":"to","value":"1994-02-02"}]) . My guess is it's pretty much sending the .json to the .php file, and then doing a $_POST, but I don't know how to proceed from here or do it. I don't know if ajax IS necessary or not, and if not, how to do it without it (everything I found around here is using ajax).

我看到这日志. json文件很好([{“名称”:“从”,“价值”:“1994-01-01”},{“名称”:“来”,“价值”:“1994-02-02”}])。我的猜测是,它将.json发送到.php文件,然后执行$_POST,但我不知道如何从这里继续,也不知道如何执行。我不知道是否需要ajax,如果不需要,也不知道如何使用它(我在这里找到的所有东西都是使用ajax)。

4 个解决方案

#1


1  

You can send form data as text string in standard URL-encoded notation or as JSON like string with jQuery.serialize().

可以使用标准的url编码表示法将表单数据作为文本字符串发送,也可以使用jQuery.serialize()将表单数据作为JSON格式发送。

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

with jQuery

与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>

Then in your PHP file

然后在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

#2


0  

PHP is treated Server-side.

PHP服务器端处理。

If you want to send data to "PHP" you need to request the server (either via Ajax if you don't want to change your current page or calling a new URL with the data).

如果要向“PHP”发送数据,需要请求服务器(如果不希望更改当前页面或使用数据调用新URL,可以通过Ajax)。

I know how to get data from an input with PHP

我知道如何用PHP从输入中获取数据

That's a pretty wrong statement as PHP can't get data from input since it's server side. The browser send data to PHP calling the server.

这是一个非常错误的语句,因为PHP不能从输入中获取数据,因为它是服务器端。浏览器向调用服务器的PHP发送数据。

#3


0  

Instead , define a route to post your input data in your php file and then through the form you can simply method='POST' rather than using ajax to send your data.

相反,定义一条路径将输入数据发布到php文件中,然后通过表单您可以简单地方法=' post ',而不是使用ajax发送数据。

#4


0  

You could also use an XML HTTP Request.

您还可以使用XML HTTP请求。

Here is an example.

这是一个例子。

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);

This is taken from an answer here.

这是从一个答案中得出的。

Send POST data using XMLHttpRequest

使用XMLHttpRequest发送POST数据

#1


1  

You can send form data as text string in standard URL-encoded notation or as JSON like string with jQuery.serialize().

可以使用标准的url编码表示法将表单数据作为文本字符串发送,也可以使用jQuery.serialize()将表单数据作为JSON格式发送。

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

with jQuery

与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>

Then in your PHP file

然后在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

#2


0  

PHP is treated Server-side.

PHP服务器端处理。

If you want to send data to "PHP" you need to request the server (either via Ajax if you don't want to change your current page or calling a new URL with the data).

如果要向“PHP”发送数据,需要请求服务器(如果不希望更改当前页面或使用数据调用新URL,可以通过Ajax)。

I know how to get data from an input with PHP

我知道如何用PHP从输入中获取数据

That's a pretty wrong statement as PHP can't get data from input since it's server side. The browser send data to PHP calling the server.

这是一个非常错误的语句,因为PHP不能从输入中获取数据,因为它是服务器端。浏览器向调用服务器的PHP发送数据。

#3


0  

Instead , define a route to post your input data in your php file and then through the form you can simply method='POST' rather than using ajax to send your data.

相反,定义一条路径将输入数据发布到php文件中,然后通过表单您可以简单地方法=' post ',而不是使用ajax发送数据。

#4


0  

You could also use an XML HTTP Request.

您还可以使用XML HTTP请求。

Here is an example.

这是一个例子。

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);

This is taken from an answer here.

这是从一个答案中得出的。

Send POST data using XMLHttpRequest

使用XMLHttpRequest发送POST数据