Calling mysql string at selection of radio button in php

Hi,

i am calling 2 strings with the selection of radio button…one is for company and another is for category…

Here’s what I am doing…


$v1 = 'checked';
$selected_radio = $_POST['category'];
if ($selected_radio = = 'v1')
{
if(isset($_GET['getkeywordsByLetters']) && isset($_GET['letters'])){
    $letters = $_GET['letters'];
    $letters = preg_replace("/[^a-z0-9 ]/si","",$letters);
    $res = mysql_query("select category from tblcat where category like '".$letters."%'") or die(mysql_error());
   
    while($inf = mysql_fetch_array($res)){
        echo $inf[""]."###".$inf["category"]."|";
    }
}

}
    else
    {
    if(isset($_GET['getkeywordsByLetters']) && isset($_GET['letters'])){
    $letters = $_GET['letters'];
    $letters = preg_replace("/[^a-z0-9 ]/si","",$letters);
    $res = mysql_query("select biz_nam, biz_addy3 from tblbusinessinfo where biz_nam like '".$letters."%'") or die(mysql_error());
   
    while($inf = mysql_fetch_array($res)){
        echo $inf["biz_nam"]."###".$inf["biz_addy3"]."|";
    }
    }
}

The problem is its not showing any suggestion…no error though.

Please let me know how it can be called as I want…thanks in advance.

Is this because of this:


if ($selected_radio = = 'v1') 

Double equals to symbols must be together not with space


if ($selected_radio == 'v1') 

Do you know in which condition it has entered in?


$v1 = 'checked'; 
$selected_radio = $_POST['category']; 
if ($selected_radio == 'v1'){
    echo 'V1' . '<br />';
    if(isset($_GET['getkeywordsByLetters']) && isset($_GET['letters'])){ 
        $letters = $_GET['letters']; 
        $letters = preg_replace("/[^a-z0-9 ]/si","",$letters);
        $sql =  "select category from tblcat where category like '".$letters."&#37;'";
        echo $sql . '<br />';
        $res = mysql_query($sql) or die(mysql_error());
         
        while($inf = mysql_fetch_array($res)){ 
            echo $inf[""]."###".$inf["category"]."|"; 
        }
    }
} 
else{
    echo 'Not V1' . '<br />';
    if(isset($_GET['getkeywordsByLetters']) && isset($_GET['letters'])){ 
        $letters = $_GET['letters']; 
        $letters = preg_replace("/[^a-z0-9 ]/si","",$letters);
        $sql = "select biz_nam, biz_addy3 from tblbusinessinfo where biz_nam like '".$letters."%'";
        echo $sql . '<br />';
        $res = mysql_query($sql) or die(mysql_error()); 
        while($inf = mysql_fetch_array($res)){ 
            echo $inf["biz_nam"]."###".$inf["biz_addy3"]."|"; 
        }
    } 
}

Run the above code and see what it prints in the page. And try to run the query outside PHP (from phpmyadmin) and see if that returns any records there.

I have not read past this line as I have never seen conditional checks with spaces in them like this before:

if ($selected_radio = = ‘v1’)

change it to

if ($selected_radio === ‘v1’)

and let us know if it makes a difference.

If you are debugging conditional code your first rule should be to debug the variable you are testing against, before and maybe even after your condition.

var_dump( $selected_radio ) ;

This is especially true when testing nulls, empty, zero, empty strings etc.

It is full of traps for the unwary. http://www.php.net/manual/en/language.operators.comparison.php

Thanks to both of you…I am trying your suggestions and let you know what happened.

Hi guys,

I tried your suggestions…but unfotunately the problem is still there.

Its being called by the function but there were no results to show…

this is what I am doing…

$v1 = ‘checked’;
$selected_radio = $_POST[‘category’];
if ($selected_radio === ‘v1’){
echo ‘V1’ . ‘<br />’;
if(isset($_GET[‘getkeywordsByLetters’]) && isset($_GET[‘letters’])){
$letters = $_GET[‘letters’];
$letters = preg_replace(“/[^a-z0-9 ]/si”,“”,$letters);
$sql = “select category from tblcat where category like '”.$letters.“%'”;
echo $sql . ‘<br />’;
$res = mysql_query($sql) or die(mysql_error());

    while($inf = mysql_fetch_array($res)){ 
        echo $inf[""]."###".$inf["category"]."|"; 
    }
}

}
else{
var_dump($selected_radio) ;
echo ‘Not V1’ . ‘<br />’;
if(isset($_GET[‘getkeywordsByLetters’]) && isset($_GET[‘letters’])){
$letters = $_GET[‘letters’];
$letters = preg_replace(“/[^a-z0-9 ]/si”,“”,$letters);
$sql = “select biz_nam, biz_addy3 from tblbusinessinfo where biz_nam like '”.$letters.“%'”;
echo $sql . ‘<br />’;
$res = mysql_query($sql) or die(mysql_error());
while($inf = mysql_fetch_array($res)){
echo $inf[“biz_nam”].“###”.$inf[“biz_addy3”].“|”;
}
}
}

What did you see on the page when you run the script? There must be something printed in your screen/page which has to give you a right direction too.

In the above coding when I dumped the $selected_radio, the function starts calling else condition and not checking if condition.

This is how I am checking radio’s

<input type=“radio” name=“comp_cat” class=“radiobtn” id=“company” />

            &lt;input type="radio" name="comp_cat" class="radiobtn" id="category" checked="true" tabindex="1" /&gt;

Do you think $_POST[‘category’]; will be fine as I am redirecting pages via js…

Start at the beginning.

<?
//top of the page
var_dump( $_POST );
exit() ;

// existing stuff


?>

What is being passed to your script when you submit the form?

Paste it here.

This is how I am calling the function.

onKeyUp=“ajax_showOptions(this,‘getkeywordsByLetters’,event)”

And this is how the function is working…

function ajax_showOptions(inputObj,paramToExternalFile,e)
{
if(e.keyCode==13 || e.keyCode==9)return;
if(ajax_list_currentLetters[inputObj.name]==inputObj.value)return;
if(!ajax_list_cachedLists[paramToExternalFile])ajax_list_cachedLists[paramToExternalFile] = new Array();
ajax_list_currentLetters[inputObj.name] = inputObj.value;
if(!ajax_optionDiv){
ajax_optionDiv = document.createElement(‘DIV’);
ajax_optionDiv.id = ‘ajax_listOfOptions’;
document.body.appendChild(ajax_optionDiv);

		if(ajax_list_MSIE){
			ajax_optionDiv_iframe = document.createElement('IFRAME');
			ajax_optionDiv_iframe.border='0';
			ajax_optionDiv_iframe.style.width = ajax_optionDiv.clientWidth + 'px';
			ajax_optionDiv_iframe.style.height = ajax_optionDiv.clientHeight + 'px';
			ajax_optionDiv_iframe.id = 'ajax_listOfOptions_iframe';

			document.body.appendChild(ajax_optionDiv_iframe);
		}

		var allInputs = document.getElementsByTagName('INPUT');
		for(var no=0;no&lt;allInputs.length;no++){
			if(!allInputs[no].onkeyup)allInputs[no].onfocus = ajax_options_hide;
		}
		var allSelects = document.getElementsByTagName('SELECT');
		for(var no=0;no&lt;allSelects.length;no++){
			allSelects[no].onfocus = ajax_options_hide;
		}

		var oldonkeydown=document.body.onkeydown;
		if(typeof oldonkeydown!='function'){
			document.body.onkeydown=ajax_option_keyNavigation;
		}else{
			document.body.onkeydown=function(){
				oldonkeydown();
			ajax_option_keyNavigation() ;}
		}
		var oldonresize=document.body.onresize;
		if(typeof oldonresize!='function'){
			document.body.onresize=function() {ajax_option_resize(inputObj); };
		}else{
			document.body.onresize=function(){oldonresize();
			ajax_option_resize(inputObj) ;}
		}

	}

	if(inputObj.value.length&lt;minimumLettersBeforeLookup){
		ajax_options_hide();
		return;
	}


	ajax_optionDiv.style.top = (ajax_getTopPos(inputObj) + inputObj.offsetHeight + ajaxBox_offsetY) + 'px';
	ajax_optionDiv.style.left = (ajax_getLeftPos(inputObj) + ajaxBox_offsetX) + 'px';
	if(ajax_optionDiv_iframe){
		ajax_optionDiv_iframe.style.left = ajax_optionDiv.style.left;
		ajax_optionDiv_iframe.style.top = ajax_optionDiv.style.top;
	}

	ajax_list_activeInput = inputObj;
	ajax_optionDiv.onselectstart =  ajax_list_cancelEvent;
	currentListIndex++;
	if(ajax_list_cachedLists[paramToExternalFile][inputObj.value.toLowerCase()]){
		ajax_option_list_buildList(inputObj.value,paramToExternalFile,currentListIndex);
	}else{
		var tmpIndex=currentListIndex/1;
		ajax_optionDiv.innerHTML = '';
		var ajaxIndex = ajax_list_objects.length;
		ajax_list_objects[ajaxIndex] = new sack();
		var url = ajax_list_externalFile + '?' + paramToExternalFile + '=1&letters=' + inputObj.value.replace(" ","+");
		ajax_list_objects[ajaxIndex].requestFile = url;	// Specifying which file to get
		ajax_list_objects[ajaxIndex].onCompletion = function(){ ajax_option_list_showContent(ajaxIndex,inputObj,paramToExternalFile,tmpIndex); };	// Specify function that will be executed after file has been found
		ajax_list_objects[ajaxIndex].runAJAX();		// Execute AJAX function
	}


}

Now I want to put a perameter here to check the radio buttons…

These are 2 radio buttons…

<input type=“radio” name=“comp_cat” class=“radiobtn” id=“company” />

<input type=“radio” name=“comp_cat” class=“radiobtn” id=“category” checked=“true” tabindex=“1” />

Thanks for your kind consideration.