Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-13-2012, 09:03 PM   PM User | #1
cloudstryphe
New Coder

 
Join Date: Mar 2012
Posts: 62
Thanks: 7
Thanked 0 Times in 0 Posts
cloudstryphe is an unknown quantity at this point
Form submission with Ajax

Hello all!

I'm having some issues submitting a form with ajax.

I built a site with youtube search functionality. I have the index page and a results page.

The results page takes a variable in the url to perform the search (results.php/?q=myyoutubesearch).

The index page has the search bar and I would like to be able to perform the search without having to reload the page and have the results display inside a div already on the page. No matter what I do though, when I click "search" the page redirects and doesn't perform the ajax request. Here is my code:

Code:
<form name="youtubesearch" method="POST" id="youtubesearch" action="">
	<!--[if IE]>
		<input type="search" name="youtubesearchinput" id="youtubesearchinputie" placeholder="Search YouTube.." required/>
		<input type="submit" class="button" value="Search">
	<![endif]-->
	
	<!--[if !IE]><!-->
	<input type="search" name="youtubesearchinput" id="youtubesearchinput" placeholder="Search YouTube.." required/>
	<input type="submit" class="button" id="youtubesubmit" value="Search">
	 <!--<![endif]-->
</form>


<script>
$("#youtubesubmit").click(function() {
var searchquery = $("#youtubesearchinput").val();

var dataString = '?q=' + searchquery;

	$.ajax({
		type: "POST",
		url:"/youtube/results.php",
		data: dataString,
		success:function(result) {
			$(".grid_13").html(result);
	}})
});
</script>
All help is appreciated
cloudstryphe is offline   Reply With Quote
Old 10-13-2012, 10:38 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
You *ARE* doing an AJAX submit. But then, because you used a SUBMIT BUTTON, the <form> GOES AHEAD AND SUBMITS ANYWAY.

Answer is simple:
Code:
<input type="button" class="button" id="youtubesubmit" value="Search">
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
cloudstryphe (10-13-2012)
Old 10-13-2012, 10:55 PM   PM User | #3
cloudstryphe
New Coder

 
Join Date: Mar 2012
Posts: 62
Thanks: 7
Thanked 0 Times in 0 Posts
cloudstryphe is an unknown quantity at this point
Thank you so much! I have one more question though...

Ok, I've changed the code which now works when I click the button, but when I press enter, it still redirects. Any ideas?
cloudstryphe is offline   Reply With Quote
Old 10-14-2012, 04:59 AM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
A little bit of a hack, but you can do this:
Code:
<form method="POST" id="youtubesearch" action="" onsubmit="return false;">
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 10-14-2012, 05:00 AM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
I'm curious why you think you need two different versions of that code: One for IE, one for non-IE??? jQuery-based AJAX code should work just fine in any major browsers. Yes, even IE.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 10-14-2012, 06:57 AM   PM User | #6
DanInMa
Senior Coder

 
DanInMa's Avatar
 
Join Date: Nov 2010
Location: Salem,Ma
Posts: 1,307
Thanks: 12
Thanked 204 Times in 204 Posts
DanInMa is on a distinguished road
you bind your ajax request directly to the submit event by using .submit(),l then regardless if they click a submit type button or press enter your ajax request will process. ( e.preventDefault() prevents the form from submitting itself in the normal fashion so the page doesnt reload)
Code:
<form name="youtubesearch" method="POST" id="youtubesearch" action="">
	<!--[if IE]>
		<input type="search" name="youtubesearchinput" id="youtubesearchinputie" placeholder="Search YouTube.." required/>
		<input type="submit" class="button" value="Search">
	<![endif]-->
	
	<!--[if !IE]><!-->
	<input type="search" name="youtubesearchinput" id="youtubesearchinput" placeholder="Search YouTube.." required/>
	<input type="submit" class="button" id="youtubesubmit" value="Search">
	 <!--<![endif]-->
</form>


<script>

$("#youtubesearch").submit(function(e) {
e.preventDefault();
var searchquery = $("#youtubesearchinput").val();

var dataString = '?q=' + searchquery;

	$.ajax({
		type: "POST",
		url:"/youtube/results.php",
		data: dataString,
		success:function(result) {
			$(".grid_13").html(result);
	}})
});
</script>
__________________
- Firebug is a web developers best friend! - Learn it, Love it, use it!
- Validate your code! - JQ/JS troubleshooting
- Using jQuery with Other Libraries - Jslint for Jquery/other JS library users
DanInMa is offline   Reply With Quote
Old 10-14-2012, 12:08 PM   PM User | #7
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,600
Thanks: 5
Thanked 865 Times in 842 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
I just wanted to chime in to say that simply changing the button type from “submit” to “button”, as OldPedant has suggested, is not the ideal solution because you basically remove the possibility to submit the form by pressing the button if JS isn’t available for whatever reason (and don’t come with “everyone has JS enabled nowadays”, this is such a simple issue that it doesn’t require you to jump through hoops to get it right). The better solution is the one DanInMa has suggested.
__________________
Don’t click this link!
VIPStephan is offline   Reply With Quote
Old 10-15-2012, 04:06 PM   PM User | #8
cloudstryphe
New Coder

 
Join Date: Mar 2012
Posts: 62
Thanks: 7
Thanked 0 Times in 0 Posts
cloudstryphe is an unknown quantity at this point
Oh wow, thank you guys very much. I wasn't familiar with the preventDefault function.

To answer Pendant, the reason I had the IE thing was pure styling issues IE was having with my page.
cloudstryphe is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:36 AM.


Advertisement
Log in to turn off these ads.