1

I am wanting to use the AJAX function via jQuery to return some data onto my homepage. Currently, I am using an 'action=' on my form to an external PHP file just to check that the data is being sent and it is. I'm now wanting to use AJAX to return this data into a div on my homepage. I have this code but at the moment it's not returning any data:

$("#ticketSearch").click(function(e) {
  e.preventDefault();
    var eNameData = ("#postcodeSearch").val();
      alert('#ticketSearch clicked'); //this alert doesn't even work!

    var dataToSend = 'postcodeSearch=' + eNameData;

    $.ajax({                
        url: "index.php", 
        type: "POST",
        data: dataToSend,     
        cache: false,
        success: function(php_output)
            {
            $("#ticketResults").html(php_output).show();
            }
    }); 
});

The code for the form:

<form name="pcSearchForm" id="pcSearchForm" class="pcSearchForm"  method="post" action="postCodeSearch.php"> // the action method needs to be taken out

        <input type="text" name="postcodeSearch" id="postcodeSearch" class="postcodeSearch" placeholder="Postcode..." /> 

        <input type="submit" name="ticketSearch" id="ticketSearch" class="ticketSearch" value="Search" /> 

</form>

Any help is much appreciated, I think I am on the right lines but something just isn't right! Thank you.

EDIT: New code:

$(document).ready(function() {
  $("#ticketSearch").click(function(e) {
alert("click");
e.preventDefault();
var eNameData = $("#postcodeSearch").val();
 alert('eNameData');
 });

var dataToSend = 'postcodeSearch=' + eNameData;

    $.ajax({                
        url: "index.php", 
        type: "POST",
        data: dataToSend,     
        cache: false,
        success: function(php_output)
            {
            $("#ticketResults").html(php_output);
            alert("#ticketResults");
            }
    }); 
}); 

The form:

        <input type="text" name="postcodeSearch" id="postcodeSearch" class="postcodeSearch" placeholder="Postcode..." /> 

        <input type="button" name="ticketSearch" id="ticketSearch" class="ticketSearch" value="Search" />


    </form>
0

4 Answers 4

2

You are doing it wrong dude :) ..

Change

var eNameData = ("#postcodeSearch").val();

to

var eNameData = $("#postcodeSearch").val();

I wonder why you use alert('#ticketSearch clicked'); .

What's its use. If you need to alert the data postcodeSearch,

then just do

 alert(eNameData'); 

Hope this helps you :)

EDIT:

Your code seems to be wrong, Your click event is closed before the ajax call is done. Also i think you are calling the wrong file. Your form submit action page is postCodeSearch.php but in ajax you are calling index.php

Your code should be changed to :

FORM :

<form name="pcSearchForm" id="pcSearchForm" class="pcSearchForm"  method="post" action="postCodeSearch.php"> // the action method needs to be taken out

        <input type="text" name="postcodeSearch" id="postcodeSearch" class="postcodeSearch" placeholder="Postcode..." /> 

        <input type="button" name="ticketSearch" id="ticketSearch" class="ticketSearch" value="Search" /> 

</form>

JS:

$(document).ready(function() {
    $("#ticketSearch").click(function(e) {
       var eNameData = $("#postcodeSearch").val();
       var dataToSend = 'postcodeSearch=' + eNameData;

       $.ajax({                
           url: "postCodeSearch.php", 
           type: "POST",
           data: dataToSend,     
           cache: false,
           success: function(php_output) {
               $("#ticketResults").html(php_output);
           }
       }); 
    }); 
}); 

Hope this helps you. :)

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

7 Comments

Can you try debugging the ajax call in firebug
go to the net panel in firebug. Check if any errors are there in console
can you try alerting the output. I doubt if the ajax call is returning empty output. Is their any url i can check with ??
Hey can you do one thing. In your form can you change type="submit" to type="button" and try. Since type="submit" actually submits the form without listening to the javascript click function.
Yeah. That was the problem. You should change type="submit" to type="button" in your form. Then only click event can be listened. Sorry din't notice it earlier
|
1

Error: "#postcodeSearch".val is not a function

Use:

var eNameData = $("#postcodeSearch").val();

instead of:

var eNameData = ("#postcodeSearch").val();

2 Comments

@EvilP No it's not. The $ references jQuery. Please read more careful.
oh sorry, didn't read that carefully, did focus on the val ^.^ sorry dude
1

open firebug / chrome dev tools and in your success function put

console.log(php_output)

Comments

0
var eNameData = $("#postcodeSearch").val();

http://jsfiddle.net/Fgtzd/

1 Comment

@Rizwan Mumtaz: Thank you for your response, the link in particular is a cool site never stumbled upon before!

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.