0

I've seen several questions here with the similar subject but I can't find anything which is relevant to my situation. I am trying to build jQuery code that is able to build a list of items to save it in an inventory database and I am using .post() those to a additems.php that will add them to that database (after sensitization), as well as the current path name so the .php can send the user back to the same page.

The behavior I am getting is nothing whatsoever with no console error (except the 'this works' alert when I leave that in.) The behavior I am looking for is, the page should redirect to additems.php as an html form action would, execute the code there and redirect back to this page.

Here is my piece of code:

$(document).ready(function(){
        $("#button").click(function(){
            alert("this works");
            var itemsarray = ['itemname'];
            var itemattributesarray = ['itemattribute'];
            var quantitiesarray = ['1'];

            $.post('additems.php', {
                items:{items: itemsarray}, 
                itemattributes:{itemattributes: itemattributesarray},
                quantities:{quantities: quantitiesarray}, 
                returnpath: window.pathname
            });
        });
    });

Thank you for your time and any suggestions. I've never used this site so please let me know how I can improve my question as well, if you have the time.

Dinesh Patil
  • 615
  • 2
  • 15
  • 37
  • An Ajax post happens in the background, you're not going to see it occur. Add a 3rd argument: `() => console.log("and this works too")` – Tibrogargan Nov 03 '16 at 04:57
  • 1
    Sounds like you want a standard form submission. Why are you using AJAX? – Phil Nov 03 '16 at 04:57
  • You said:- `"The behavior I am looking for is for the page to redirect to additems.php as an html form action would, execute the code there, and redirect back to this page. " ` So no use of ajax. Just normal form submission with `action=second page` and on second page after all code `header('location: previous page')` – Alive to die - Anant Nov 03 '16 at 05:04
  • Thank you Anant and Phil, you are both correct. I'm still concerned that the AJAX was not interacting with the .php at all, but that is a bridge I can cross at a later time as your responses have helped me to realize I can simply create an invisible form and submit that normally. – Kirby Perry Nov 03 '16 at 16:59

1 Answers1

0

An alternative way is,

$.ajax({
   'url':'additems.php',
   'method' : 'POST',
   'data':{
             'items':itemsarray,
             'itemattributes':itemattributesarray,
             'quantities' : quantitiesarray
          },
   success: function(data){
     //here you will get ajax response
     console.log(data);
   }
});
Balraj Allam
  • 611
  • 6
  • 24
  • 1
    OP said:- `"The behavior I am looking for is for the page to redirect to additems.php as an html form action would, execute the code there, and redirect back to this page. "` – Alive to die - Anant Nov 03 '16 at 05:04