0

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 ??

3
  • 1
    Are you posting any data with your Form? Commented May 17, 2014 at 2:57
  • Oh yeh it should have said GET. Commented May 17, 2014 at 3:00
  • What exactly are you trying to do? If your script just gets some data from another web-site and stores that in a database, you are probably looking for a cron job. No need to do that via a web-page and ajax. Commented May 17, 2014 at 3:04

2 Answers 2

1

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.........
        })
  });
});
Sign up to request clarification or add additional context in comments.

3 Comments

If you are not posting data then you should not use the POST type. a Get request is galled for here
The PHP script is not executing at all now. The success response is success but the PHP does not execute.
I did actually need a data section. Stupid silly mistake on my part. I needed to provide the URL parameters that the PHP file needs to function.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.