xa.
xa.

Reputation: 345

Using ajax and jQuery to execute some PHP code

EDIT: solved. Was a simple mistake. The PHP file needed one of those "?field=value" things in the URL, and that's what needed to be in the data section. I needed to feed through the value of the input field to the PHP file, since when using Ajax, the URL does not change.


Three files: test.html test.php test.js

So when a user clicks a button in test.html, it will execute the php code in test.php

<form method="GET" action="test.php" id="addData">

That works fine. The PHP retireves some information from another website and stores them in variables and then sends them to my database. Everything works fine. What I want to do now is make this process occur in the background. I have been researching the jQuery ajax call, and there is a 'data' field where you need to enter data to 'send'. This is where I got confused. My data is all handled in the PHP file and my javascript file has no access to that data. So...what goes in the data field?

    $(document).ready(function(){
      $("#addData").submit(function(e){
        e.preventDefault();
        $.ajax
            ({
               type: 'POST',
               url: 'test.php',
               data:  ?????????????????????????????
               success.........
            })
      });
    });

So when they submit the form, I want the PHP to execute and the data sent to the database. All the 'data' is taken care of in the PHP file and the 'sending to the database' is also handled in the PHP file. So what do I put in the ajax 'data' field ??

Upvotes: 0

Views: 204

Answers (2)

cs45977
cs45977

Reputation: 400

If you are not posting data you would use an ajax method using the get type

$(document).ready(function(){
  $("#addData").submit(function(e){
    e.preventDefault();
    $.ajax
        ({
           url: 'test.php',
           success.........
        })
  });
});

Upvotes: 0

user3567510
user3567510

Reputation:

You don't have to put anything. Take that line out.

 $(document).ready(function(){
  $("#addData").submit(function(e){
    e.preventDefault();
    $.ajax
        ({
           type: 'POST',
           url: 'test.php',
           success.........
        })
  });
});

Upvotes: 1

Related Questions