0

I've been trying for a few hours already, and I don't know what else to try. I looked at dozens of questions around here, but they're overly-complicated or I don't understand them. Doesn't help that my experience with javascript/jquery dates to a few days ago. Anyways, here's my form:

<form onsubmit="onSubmit(this)">
    <input type="text" name="input1"/><br/>
    <input type="text" name="input2"/><br/>
</form>

And my script:

function onSubmit( form ){
    var jsondate = JSON.stringify( $(form).serializeArray() );
    console.log( jsondate );
    alert(jsondate);
    $.ajax({
        type: "POST",
        url: "json.php",
        data: jsondate,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data){
            alert(jsondate);
        },
        failure: function(errMsg) {
            alert(errMsg);
        }
    });
}

And my json.php file:

<?php
if (isset($_POST['jsondate'])) {
    echo "whatever";
}

The thing is, I get the alert with the json string, but when it redirects me to json.php (using action="json.php" on the form), or I stay on the page, it doesn't show anything, so I guess it's something inside $.ajax({...})

Any explanation about how to make it work, how it works and why would be REALLY helpful!

Newwt
  • 491
  • 1
  • 5
  • 22
  • 4
    `data: {jsondate:jsondate},` try this – M A SIDDIQUI Apr 12 '17 at 13:17
  • 2
    Your form elements don't have names... – Jonnix Apr 12 '17 at 13:17
  • 1
    didn't you ask a similar question earlier? http://stackoverflow.com/q/43366068/1415724 and received answers. – Funk Forty Niner Apr 12 '17 at 13:17
  • 1
    Possible duplicate of [Send JSON file from jQuery to PHP without AJAX](http://stackoverflow.com/questions/43366068/send-json-file-from-jquery-to-php-without-ajax) – Mazz Apr 12 '17 at 13:18
  • 1
    Why are you doing this `JSON.stringify($(form).serializeArray())`? You don't need to change it to a JSON string. – Mikey Apr 12 '17 at 13:19
  • @Fred-ii- yes, I did. I was trying without ajax, though, so I used what they answered and gave up, so I'm now using ajax. – Newwt Apr 12 '17 at 13:20
  • Give your form elements some names. – Twinfriends Apr 12 '17 at 13:20
  • @Newwt you gave up; well... I think they probably gave up because they didn't know what you were trying to do or tried helping but came to a brick wall. – Funk Forty Niner Apr 12 '17 at 13:21
  • @Mikey I have absolutely no idea. This is how I saw someone doing it. It does log the json string to the console, though. Sorry, I'm really lost. – Newwt Apr 12 '17 at 13:22
  • @Newwt Just a communication breakdown; all is well :-) Now, see if PHP's error reporting throws something back http://php.net/manual/en/function.error-reporting.php and look at your developer console. Those will help you to see what is going on or not. Also look at your HTML source. – Funk Forty Niner Apr 12 '17 at 13:35
  • @Fred-ii- Not an error either from php or the console. The only time I get an error is if I try to do a `$_POST['jsondate']` without an `isset()`, where it says it's undefined. – Newwt Apr 12 '17 at 13:38
  • 1
    @Newwt Well, TBH; I'm not much of a JS guy, mostly PHP so I thought there'd be something I could help with further. For the bit of JS I know, it seems that you're not sending JSON data, probably why or one reason why it's not working. Wish I could be of more help, sorry. I sincerely wish you well, *cheers*. – Funk Forty Niner Apr 12 '17 at 13:45
  • 1
    @Fred-ii- Thank you,any help is appreciated. I will keep trying and if nobody replies and I manage to get it working I'll post the answer. – Newwt Apr 12 '17 at 13:47

2 Answers2

0

You will need a way to get the value from the elements.. In this example I'll use the ID on the HTML and get using it in the javascript

HTML:

<form onsubmit="onSubmit(this)">
    <input type="text" id="ID1"><br/>
    <input type="text" id="ID2"><br/>
</form>

JavaScript:

var v1 = $("#ID1").val(); // Get de value using the Ids
var v2 = $("#ID2").val();
$.ajax({
  method: "POST",
  url: "json.php", // add the page here
  data: { name: v1, location: v2 } // put the data here (Will get this data using POST)
})
  .done(function( msg ) {
    alert( "Data Saved: " + msg ); // if it works this alert will appear
});
Xidh
  • 582
  • 1
  • 5
  • 19
0

Two problems: you didn't give names to your form elements and you don't stop the form from submitting.

Here's a solution making use as much of jQuery.

First, for convenience, give an ID to your form.

Second, required, give names to your form fields.

<form id="form1">
    <input type="text" name="input1"><br/>
    <input type="text" name="input2"><br/>
</form>

Third, attach an onsubmit event handler using .submit(handler) to your form.

$('#form1').submit(function (e) {
    // prevent the form from submitting/reloading (by default)
    e.preventDefault();
    //
    $.ajax({
        type: "POST",
        url: "json.php",
        data: $(this).serializeArray(),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data){
            alert(jsondate);
        },
        failure: function(errMsg) {
            alert(errMsg);
        }
    });
});

Fourth, in your json.php, you would access the fields

echo $_POST['input1'];
echo $_POST['input2'];
Mikey
  • 6,728
  • 4
  • 22
  • 45
  • Doesn't work. It clears the inputs and doesn't display anything. By the way, what do you mean by "stop the form from submitting"? – Newwt Apr 12 '17 at 13:34
  • Did you include jQuery library as shown in the [documentation](https://learn.jquery.com/about-jquery/how-jquery-works/)? By default, when you submit a [form](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form#Examples), it sends the data to whatever page is in the `action` attribute. If there is no `action` attribute, it will send the data to the same page. When you send the data, it will also go to that page. We want to prevent that happening. – Mikey Apr 12 '17 at 13:42
  • Yes, I did. jQuery works when doing alerts with the json string and such. It's the ajax part where everything goes down. – Newwt Apr 12 '17 at 13:43