/    Sign up×
Community /Pin to ProfileBookmark

PHP AJAX narrowing result retrieve from database

I am new to this forum in term of posting a thread. I am going to try to be as specific as I can. I just learn how to use AJAX to retrieve data from database.
In this exercise, depending on what I select from a drop-down list box, the result is return on that page. Next, I want to narrow the result further by selecting another drop-down list box. This is where I am lost. I can not seem to get the query so that it retain the first selected criteria while returning the second selected criteria. Can someone help?

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@WebnerdOct 03.2007 — Post the code please for the select boxes + ajax calls
Copy linkTweet thisAlerts:
@ldgiangauthorOct 04.2007 — Just in case I am not being clear. I was able to get the first criteria to return the results. What I want is that taken that result, I want to refine the results so that the number of return is smaller.

Thanks for helping!!


First criteria:

Type of Format<br>

<input class=noborder type=radio name="typ_fmt" id="typ_fmt" value="both" onclick="showResult(this.value,'')">Both<br>

<input class=noborder type=radio name="typ_fmt" id="typ_fmt" value="positive" onclick="showResult(this.value,'')">Positive<br>

<input class=noborder type=radio name="typ_fmt" id="typ_fmt" value="negative" onclick ="showResult(this.value,'')">Negative

Second criteria in refining the result:

<select class=med name="proj_sts" id="proj_sts" onchange="showResult('',this.value)">

<option value="None">Select Project Status</option>

<option value="Requested">Requested</option>

<option value="Discussion">Discussion</option>

<option value="Development|Testing">Development|Testing</option>

<option value="Completed">Completed</option></select>

Javascript:

function showResult(fmt, projsts) {

xmlHttp=GetXmlHttpObject();

if (xmlHttp==null) {

alert ("Browser does not support HTTP Request");

return;}

//alert(atgnfmt);
var url="ajax_rpt.php?fmt_typ="+fmt;
url=url+"&proj_sts="+projsts;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);

}

PHP code:

session_start();

//retrieve variables passed

$typ_fmt = $_REQUEST['fmt_typ'];

$projsts = trim($_
REQUEST['proj_sts']);

$sqlresult = "select * from " . $tblProj . " where proj_id >= 1 ";

if (($typ_fmt != "") AND ($typ_fmt != "both")) {

$sqlresult .= " AND typ_fmt = '" . $typ_fmt . "'";}

if (($projsts != "") AND ($projsts != "None")) {

$sqlresult .= " AND projsts ='" . $projsts . "'";}

$rsresult = DBUtils::runQuery($sqlresult,$dbhandle);

print "<table class=onborder>n";

print "<tr bgcolor="#FF1A23"><td class=border_bcenter><font class=white>Mgmt Status/<br>Exp Mkt Dt</font></td><td class=border_bcenter><font class=white>Projectt ID</font></td><td class=border_bcenter><font class=white>Project Status</font></td><td class=border_bcenter><font class=white>Approval Status</font></td><td class=border_bcenter><font class=white>Type of Format</font></td></tr>n";

while ($rsdata_result = mysql_fetch_array($rsresult)) {

...php code

}
Copy linkTweet thisAlerts:
@WebnerdOct 04.2007 — First:

onchange="showResult('',this.value)"

Will not always work, you should use:

onchange="showResult('',this.options[this.selectedIndex].value)"

Second:

You can't use the same ID more than once: typ_fmt

You can use the "name" attribute but not ID. ID must be unique for each element

Third:

You need a wrapper function before calling showResult.

Like:
<i>
</i>
function getParams(){
var fmt='';
var projsts=''; <br/>
var r=document.forms[0]['typ_fmt];
var s=document.forms[0]['proj_sts'];

for(i=0;i&lt;r.length;i++){
if(r[i].checked) fmt=r[i].value;
}

projsts=s.options[s.selectedIndex].value;

showResult(fmt,projsts);


}



so actually, your final HTML should look like:

<i>
</i>
onchange="getParams()"

onclick="getParams()"

Copy linkTweet thisAlerts:
@ldgiangauthorOct 04.2007 — I am not sure this is correct:

var r=document.forms[0]['typ_fmt];

var s=document.forms[0]['proj_sts'];

because when I tried to see what the value for it when I selected one of the drop-down project status, it is "undefined". I used the "alert" response.


Are you sure about this code?

Before I forget ... I want to thank you for helping me out as much as possible. Really, really appreciate it.
Copy linkTweet thisAlerts:
@WebnerdOct 04.2007 — 
Are you sure about this code?
[/quote]


That is up to you to debug, I can only assume you have a valid and only one <form> tag since I don't have your whole page. As well:

document.forms[0]['typ_fmt']

will not have a value as it is an Array
×

Success!

Help @ldgiang spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...