Problem with ajax call


#1

Guys plz need a help.

I have index page

<form id="form1" name="form1" method="get" action="show.php">
<select name="cg" onchange="SearchC();" id="catG"  > 
		     <option value="1" >&#1042;&#1057;&#1045; &#1050;&#1040;&#1058;&#1045;&#1043;&#1054;&#1056;&#1048;&#1048;</option> 	     
		    <option value="2"<? if($pcat=="2")echo "selected='selected'"; ?>>subject</option> 	
		    <option value="3" <? if($pcat=="3")echo "selected='selected'"; ?>>faculty</option> 
		    <option value="4" <? if($pcat=="3")echo "selected='selected'"; ?>>lecture</option> 		        	
</select>	
</form>	     	

then

function SearchC() {
	var category = $("#catG").val();	
	var query = "searchbox.php?ct="+category;		
	$.ajax({
		url: query,
		success: function(data, textStatus, XMLHttpRequest) {			
			$("#searchcontents").html(data);
			SearchCCallBack(false);			
		}
	});
}



and searchbox.php

<?
if (($_GET['ct'] == '1'))
{
?>				

	
	<select name="subj" id="subj" class="textbox">
	<option value="1"> math </option>
        <option value="2"<? if WHAT TO WRITE HERE echo "selected='selected'";?>>phys</option>
	</select>	
<?php
} ?>		

how i can pass certain options "SELECTED" to show.php from searchbox.php. how to display everything in show.php?

values from form1 m passing like -> if (isset($GET['cg'])){$pcat=$GET['cg'];}


#2

Hi Juriy and welcome to the SitePoint forums, the first thing i would like to point out is not to use $_GET queries with Ajax unless you escape the incoming value as they are more prone to attack.

From what i can gather from the code you have above is when you select an option from your cg select box that gets sent to your searchbox.php script which then generates another sub select box with more selections.

You question regarding this would be what code would you need to write for the selected="selected" attribute, is that correct?

If so then my simple answer would be at this point nothing, without having a value to match against the IF statement you just need the <option> element for now. When it comes time to selecting that value you can do this very easily using jQuery as it appears your using that framework.

On a side note i can see improvements which will increase the performance of your onchange event, please see the updated code below...

<?php

$pcat = (int) $pcat;

?>
<form id="form1" name="form1" method="get" action="show.php">
    <select name="cg" id="catG">
        <option value="1" >??? ?????????</option>
        <option value="2"<?php if ($pcat == 2) echo ' selected="selected"'; ?>>subject</option>
        <option value="3"<?php if ($pcat == 3) echo ' selected="selected"'; ?>>faculty</option>
        <option value="4"<?php if ($pcat == 4) echo ' selected="selected"'; ?>>lecture</option>
    </select>
</form>

$('#catG').change(function() {
    $.ajax({
        success : function(data, textStatus, XMLHttpRequest) {
            $('#searchcontents').html(data);
            SearchCCallback(false);
        },
        url : 'searchbox.php?ct=' + $(this).val()
    });
});

#3

thank you for your reply. smile
then Im moving to show.php I would like to see everything that I selected in index.php. How to make option from searchbox.php be "selected" because all selections should already be done in index.php
i need to show appropriate cg and subj in show.php


#4

You could use inline PHP declarations to store the values in your JS code, see the below example.

<?php $someInteger = 2; ?>

var myVar = <?php echo $someInteger; ?>;

$('#catG').change(function() {
    $.ajax({
        success : function(data, textStatus, XMLHttpRequest) {
            $('#searchcontents')
            .html(data)
            .find('select')
            .val(myVar);
            
            SearchCCallback(false);
        }
    });
});

#5

SgtLegend, thanks. this line was very helpful -> var myVar = <?php echo $someInteger; ?>;

task is done ))