views:

53

answers:

3

i've a search form wherein the database query has been coded in php and the html file calls this php file via ajax to display the results in the search form. the problem is, i would like the result to be displayed in the same form as search.html; yet while the ajax works, it goes to search.php to display the results.

search.html:

<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script src="scripts/search_ajax.js" type="text/javascript"></script> 
</head>

<body>
<form id="submitForm" method="post">
<div class="wrapper">
<div class="field">
<input name="search" id="search" />
</div><br />
<input id="button1" type="submit" value="Submit" class="submit" onclick="run_query();" /><br />
</div>
<div id="searchContainer">
</div>
</form>
</body>
</html>

if i add action="search.php" to the form tag, it displays the result but on search.php. i'd like it to display on the same form [i.e search.html, and not search.php] if i just add the javascript function [as done above], it displays nothing on search.html

search_ajax.js:

var xmlHttp


function run_query() {
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null) {
alert ("This browser does not support HTTP Request");
return;
} // end if
var url="search.php";
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
} //end function

function stateChanged(){
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
document.getElementById("searchContainer").innerHTML=xmlHttp.responseText;
} //end if
} //end function

function GetXmlHttpObject() {
var xmlHttp=null;
try {
// For these browsers: Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}catch (e){
//For Internet Explorer
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
} //end function

search.php:

<?php
include 'config.php';

$search_result = "";

$search_result = $_POST['search'];

$result = mysql_query("SELECT cQuotes, vAuthor, cArabic, vReference FROM thquotes WHERE cQuotes LIKE '%$search_result%' ORDER BY idQuotes DESC", $conn)
  or die ('Error: '.mysql_error());
+4  A: 
onclick="run_query();"

You don't seem to block the form's default action. Add a return false; to the end of onclick:

onclick="run_query(); return false;"

Or if run_query() actually returns false, then rewrite the onclick as follows:

onclick="return run_query();"

Update: you indeed didn't pass the request parameter to PHP code. Replace

var url = "search.php";

by

var url = "search.php?search=" + searchvalue;

And use $_GET in PHP. If you actually want to use POST, then do

var url = "search.php";
xmlHttp.open("POST",url,true);
xmlHttp.send("search=" + searchvalue);

(the searchvalue is obviously the user-entered value. I assume that you can figure how to grab it from the HTML DOM using JS.

That said and unrelated to the actual problem, instead of all that opaque and boilerplate Ajax code I suggest you to use jQuery for this. It greatly eases doing ajaxical stuff. You could for example use jQuery.post() instead of this all. The link points to several examples.

BalusC
did that. doesn't work. if i add `onclick="run_query(); return false;"` it displays all the records, instead of the keyword entered [the sql query works because i've tried in mysql] and if i rewrite the onclick, then nothing happens.
fuz3d
It at least solved the problem that the page got refreshed. Your next problem is why it returns all results. Is the request parameter passed correctly? Is the request parameter gathered correctly? Debug your PHP code.
BalusC
the request parameter isn't being passed to search.php
fuz3d
i've posted the relevant snippet from search.php above.
fuz3d
thanks for this. but it doesn't work. in the address bar, it doesn't go search.php. rather it shows `search.html?search=sweet` and displays no result ..what am i doing wrong here?
fuz3d
That's already answered. You need to `return false` to block the form's default action.
BalusC
thanks. its working now.
fuz3d
A: 

I am guessing that you need to add a return false; somewhere in your Javascript?

Finbarr
A: 

So, let me see if I understood you properly:

You have 2 files: a plain text HTML where you have the aforementioned form, and a search.php file that receives the AJAX petitions, right? And you want the file not to submit the data but instead send the AJAX and construct the results in the searchContainer div, right?

Then you have 2 options:

  1. Construct the results from an XML / Plain text petition
  2. Use innerHTML, if supported by your browser.

Stranding from the answer, the previous answers are right, you need a return false; to avoid submitting the form to the server. Now, going back to the answer, you could also change the type from submit to button to use just a button instead.

Now, the options:

  1. The first option should be the ideal: you get the results in a pre-formatted string, which you can then split and construct with createElement() and appendChild(). Although this requires a little more coding, you ensure cross-browser support (unless the browser sucks so much you cannot use the 2 functions mentioned above).
  2. This one is pretty much browser-dependant (I had issues with Firefox and Internet Explorer), and you may have to implement a lot of checking and other functions to replace the current HTML written inside the searchContainer.

I'd go for number 1, it puts off a lot of weight off your shoulder.

Update:

Per request, I'm editing with an example of 1). Supposing you get the results as plain-text, saved in xhr_results and formated like this: text_1:value_1|text_2:value_2|... (I don't know much about XML, and this is easier for me).

var options = xhr_results.split('|');
var options_text = [];
var options_value = [];
for (var i = 0; i < options.length; i++){
options_text[i] = options[i].split(':')[0];
options_value[i] = options[i].split(':')[1];
}
var sel = document.createElement('select');
for (var i = 0; i < options.length; i++){
    opt = document.createElement('option');
opt.text = options_text[i]
opt.value = options_value[i];
sel.appendChild(opt);
}

This piece of code will construct you a dropdown menu with all the options passed from the AJAX.

erKURITA
thank you for your response. can you give an example of the first option?
fuz3d
Updated the answer with an example. Once constructed, all you have to do is document.getElementById('searchContainer').appendChild(sel); , for example.
erKURITA