0

I am submiting a form to a php page using ajax in order to make a query. The problem is that the $_POST is empty. What am I doing wrong here? Any ideas?

FORM

<div id="button_left"> <form> <label> <input type="text" class="search" size="30" style="margin-left:20px;" name="epitheto" id="epitheto" placeholder="Επίθετο" /> </label> <label> <input type="submit" value="Αναζήτηση" /> </label> </form> </div 

FUNCTION

 $(function () { $('#button_left').on('submit', function (e) { e.preventDefault(); $.ajax({ type: 'post', url: 'people_name_query_final.php', data: $('form').serialize(), success: function () { $("#pelates_last_name_query").slideDown("slow"); $("#pelates_last_name_query").load( "people_name_query_final.php"); } }); }); }); 

PHP

if(isset($_POST['epitheto'])){ //some code } 
3
  • sorry what your problem, is this is your problem, that you are able to submit form even your POST is empty. Let me know, means isset function is not doing what you expecting. Thanks Commented Oct 5, 2014 at 8:42
  • the problem is that the form is being submitted, the success function is working but the $_POST is empty, so all the code in php is not running. Commented Oct 5, 2014 at 8:45
  • see the answer given by me. it might help Commented Oct 5, 2014 at 8:52

3 Answers 3

1

Add one argument to your success function:

 success: function (response) { $("#pelates_last_name_query").slideDown("slow"); $("#pelates_last_name_query").html( response); } 

and you will get response from php page....

P.S. Test php page should look like this (or whatever you want for response - you should ECHO something, send some output):

<?php if(isset($_POST['epitheto'])){ print_r($_POST); } ?> 

so, vars are sent properly.... (i've tested it right now).

If your php page looks like code you atached/showed us - there is no any output, you didn't printed anything....

Sign up to request clarification or add additional context in comments.

5 Comments

you are right. just tested it also. so the problem is that i cant load the data into #pelates_last_name_query... html(response) is not working
@AlexandrosTseros, where you test this code? Locally, or on-line? If you test locally -> you need php server installed and turned-on. :), and this must work....
@AlexandrosTseros, hm... could you send us link, and complete html... Did you checked console?
i am testing this online.i have noticed in console that when form is submitted the php page shows exactly what it should, but when the page is loaded i get nothing. so strange...
if you use load() function after ajax request -> you can't see anything, because: about load() -> It is roughly equivalent to $.get(url, data, success). So, you send POST data, and GET page... (you will notice in console). Use code i provided with response argument and html(response) in that div.
1

<form> element has submit event, <div> doesn't.

Change the first line to this:

$('form').on('submit', function (e) { // <-- change '#button_left' to 'form' // (...) Code } 

2 Comments

the form is being submitted. same results with $('form') or $('#button_left')
How do you know the form is submitted?
0

I believe Amit might be correct. You are specifying the incorrect data to submit.

Change <form> to <form id="content">
Then change your JQuery code accordingly.

$('#content').on('submit', function (e) {//rest of your code//} 

Another possible issue is that the scope on your URL is incorrect.
url: 'people_name_query_final.php', will work assuming the PHP script and JQuery are in the same directory.

Also, if you do print_r($_POST); exit; in your PHP script you see nothing?

Check the network tab on your browser developer tools to make sure the data is sent properly.

2 Comments

i am sure that's not the issue. have already tested that, still not working. yes they are in the same directory.
Just added some more ideas.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.