开发者

<select> dropdown output my MYSQL data properly using JS function

开发者 https://www.devze.com 2023-04-06 18:48 出处:网络
I have a JS/PHP piece of code that selects from a database, does some things with the ID\'s of the tables and outputs the data in a dropdown.

I have a JS/PHP piece of code that selects from a database, does some things with the ID's of the tables and outputs the data in a dropdown.

My JS currently appends myfile.php?cat=X&projectID=Y correctly and i'm grabbing the variables correctly, but when I select the second dropdown it doesn't output the name, it only appends the ID to the title and variable..

     function reload(form)
    {
    var val=form.cat.options[form.cat.options.selectedIndex].value;
    var val2=form.cat2.options[form.cat2.options.selectedIndex].value;
    self.location='add-recharge.php?cat=' + val + '&projectID=' + val2;
    }

The PHP

<?

            @$cat=$_GET['cat'];
            if(strlen($cat) > 0 and !is_numeric($cat)){  
            echo "Data Error";
            exit;
            }

            //MYSQL stuff

开发者_如何转开发
            echo "<select name='cat' onchange=\"reload(this.form)\"><option value=''>Select a client</option>";

                while($noticia2 = mysql_fetch_array($quer2)) { 
                 if($noticia2['clientID']==@$cat){echo "<option selected value='$noticia2[clientID]'>$noticia2[clientName]</option>"."<BR>";}
                    else{echo  "<option value='$noticia2[clientID]'>$noticia2[clientName]</option>";}
                }
                echo "</select>";
                echo "&nbsp;<span class='req'><img src='/images/essentialInput.png' alt='*' title='*' border='0' /></span>";
                echo "&nbsp; &nbsp;<span class='label'>Project</span>" . "&nbsp;";
                echo "<select name='cat2' onchange=\"reload(this.form)\"><option value=''>Select a project</option>";

                while($noticia = mysql_fetch_array($quer)) { 
                    echo  "<option value='$noticia[projectID]'>$noticia[projectName]</option>";
                }
                 echo "</select>";
                 echo $intClientID;
                 //echo "<br /><br /><br /><br />";   
       ?>

How do I get it to update it with projectName and not reload with 'Select project' ?


Try using:

var val=form.cat.value;

var val2=form.cat2.value;

EDIT

Apologies, I misunderstood the question.

You need some form of if/else logic in your PHP similar to that used on the Client select, but dependent on the projectID. Something like:

if( $_GET['projectID'] == $noticia[projectID] ){
    echo "<option selected value='$noticia[projectID]'>$noticia[projectName]</option>";
} else {
    echo "<option value='$noticia[projectID]'>$noticia[projectName]</option>";
}
0

精彩评论

暂无评论...
验证码 换一张
取 消