Send JSON file from jQuery to PHP without AJAX

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.

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

Answers:

Answer

You can send form data as text string in standard URL-encoded notation or as JSON like string with 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>

with 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

$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
Answer

PHP is treated Server-side.

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

I know how to get data from an input with 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.

Answer

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.

Answer

You could also use an XML HTTP Request.

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

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.