Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery AJAX POST without form elements?

I don't have a lot of experience with AJAX calls, but my example seems straightforward (except that I'm not using a element) and it makes me wonder if I'm doing the call wrong, or if you just can't do an AJAX POST without a form.

My HTML for the button I'm using is this:

<button class="btn btn-primary btn-small" id='saveAsNewName' onclick=saveAsNewName()>Save As</button>

My JS:

function saveAsNewName(str, id) {
var values = {
        'str'       : str,
        'id'        : id,
};
$.ajax({
    url: "saveAsNewName.php",
    type: "POST",
    data: values,
    dataType: 'JSON',
    success: function(data){
        alert("success" + data);
    }
})
.done(function(data) {
    alert("success" + data);
})
.fail(function(data) {
    alert("failure" + data);
});
}

And, for now, my php is simply this for testing if this works:

echo json_encode($_POST['id']));

I'm getting the failure notice every time I click my button. Is there a specific workaround I need for not using a form, or am I mucking up the basic .ajax example?

Edit, to be clear: I'm using another set of jQuery to continually update teh arguments for saveAsNewName(). The result is a call like:

onclick="saveAsNewName("wheyjuice 555", "33541")"

as a result from this jQuery code:

$(document).on('keyup',"[class*=editSimNameField]", function() { 
    var newText = $(this).val(); var id = $(".loadWindow").val();       
    $("#saveAsNewName").attr('onclick', 'saveAsNewName(\"' + newText + '\", \"' + id + '\")'); 
}); 
like image 758
bo_knows Avatar asked Jun 02 '14 14:06

bo_knows


People also ask

What is Ajax post method in jQuery?

JQuery Ajax POST Method Sends an asynchronous http POST request to load data from the server. Its general form is: jQuery.post (url data ] [, success ] [, dataType ])

How to submit forms without page refresh using jQuery?

So the user ends up seeing the following after they submit the form: By now, I think you will have to agree that it's incredibly easy to submit forms without page refresh using jQuery's powerful ajax () function. Just get the values in your JavaScript file, process them with the ajax () function, and return false.

How to create jQuery Ajax form submission?

To create jQuery ajax form submission and pass the required form data to a PHP file, you have to create an HTML file, JS File, and PHP File with the codes given below. First of all, create an HTML file with the name ‘form.html’ and add your form as given below. The form contains the input type text field, input type email field, and textarea field.

How do I send a POST request in Ajax?

JQuery Ajax POST Method. Sends an asynchronous http POST request to load data from the server. Its general form is: url : is the only mandatory parameter. This string contains the adress to which to send the request. The returned data will be ignored if no other parameter is specified.


1 Answers

You're not passing anything along to your saveAsNewName() function in the onclick, you're just running the function. Therefore, str and id are null values.

How about something like this, where you get the values from the elements by ID:

function saveAsNewName() {
    var values = {
            'str': document.getElementById('elem1').value,
            'id': document.getElementById('elem2').value
    };
    $.ajax({
        url: "saveAsNewName.php",
        type: "POST",
        data: values,
    });

    ...

}

Obviously, there are innumerable other ways to get the values.

Also, you can POST to a URL regardless of HTML markup, a form being present, etc.

Edit: Seeing your comment about updating the onclick attribute dynamically, I recommend you do 2 things that will allow you to solve this problem on your own. These notes are for Chrome, but will work similarly in Firefox via Firebug.

1) Use the console.log() function on values to verify that you're sending the data to your function that you think you're sending. E.g. console.log(values), then check the console in developer tools.

2) Open the developer tools and use the network tab to inspect the AJAX call. You can actually see the call happen in realtime and can inspect it to see what data was sent and what data was received.

like image 99
Charlie Schliesser Avatar answered Oct 23 '22 04:10

Charlie Schliesser