kamal213 Posted November 9, 2011 Share Posted November 9, 2011 Hi guys, I've got this php script which display the users of my database in a dynamic dropdown: <?php include "leadscript/connect_to_mysql.php"; $canvass_name=""; $sql = mysql_query("SELECT * FROM csj_canvasser"); $appointmentCount6 = mysql_num_rows($sql); // count the output amount if ($appointmentCount6 > 0) { while($row = mysql_fetch_array($sql)){ $c_employee = $row["c_employee"]; $canvass_name .='<option value="' . $c_employee . '">' . $c_employee . ' </option>'; } } ?> <form> <select name="c_employee"> <option value="">Select a person:</option> <?php echo $canvass_name; ?> </select> </form> I was wondering if there's a way I can write a code to GET value I select from the dynamic dropdown and use it to write a select query. Thank Quote Link to comment https://forums.phpfreaks.com/topic/250761-php-dynamic-dropdown-get-help/ Share on other sites More sharing options...
AyKay47 Posted November 9, 2011 Share Posted November 9, 2011 yes, you already have the select element inside of a form, all you will need to do is set up the form correctly and grab the value using the GET or POST method, then use this value to write a query, do you want this functionality to be asynchronous or upon refresh using PHP? Quote Link to comment https://forums.phpfreaks.com/topic/250761-php-dynamic-dropdown-get-help/#findComment-1286542 Share on other sites More sharing options...
kamal213 Posted November 9, 2011 Author Share Posted November 9, 2011 Thanks for getting back to me Yes how do I get it to be asynchronous? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/250761-php-dynamic-dropdown-get-help/#findComment-1286547 Share on other sites More sharing options...
kamal213 Posted November 9, 2011 Author Share Posted November 9, 2011 Also would the form need to have an action when I set it up? even though i'm not posting into the db Quote Link to comment https://forums.phpfreaks.com/topic/250761-php-dynamic-dropdown-get-help/#findComment-1286548 Share on other sites More sharing options...
AyKay47 Posted November 9, 2011 Share Posted November 9, 2011 the forms action is used to distinguish where the form is to redirect its information to. If you are planning on going the AJAX route, you do not need to include a form at all, as it is not needed to grab the select elements value with AJAX, however I always recommend having a PHP backup script available just in case a user does not have JavaScript enabled. I would advise using jquery's AJAX API for this.. here is a little example of how it works. http://api.jquery.com/selected-selector/ Quote Link to comment https://forums.phpfreaks.com/topic/250761-php-dynamic-dropdown-get-help/#findComment-1286553 Share on other sites More sharing options...
kamal213 Posted November 9, 2011 Author Share Posted November 9, 2011 The truth is I don't need it to post to my db at all, only to used the value to select. This is what i've tried to do which didn't work. Can you spot my problem? <?php include "leadscript/connect_to_mysql.php"; $canvass_name=""; $sql = mysql_query("SELECT * FROM csj_canvasser"); $appointmentCount6 = mysql_num_rows($sql); // count the output amount if ($appointmentCount6 > 0) { while($row = mysql_fetch_array($sql)){ $c_employee = $row["c_employee"]; $canvass_name .='<option value="' . $c_employee . '">' . $c_employee . ' </option>'; } } ?> <form action="addnew_leads.php" enctype="multipart/form-data" name="addform" id="addform" method="post"> <select name="c_employee" id="c_employee"> <option value="">Select a person:</option> <?php echo $canvass_name; ?> </select> </form> <?php if (isset($_GET['c_employee'])) { $c_employee = $_GET['c_employee']; $sql = mysql_query("SELECT * FROM customer WHERE c_employee ='$c_employee' LIMIT 1"); $customerMatch = mysql_num_rows($sql); // count the output amount if ($customerMatch > 0) { while($row = mysql_fetch_array($sql)){ $c_employee = $row["c_employee"]; echo '' . $c_employee . ''; } } exit(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/250761-php-dynamic-dropdown-get-help/#findComment-1286560 Share on other sites More sharing options...
AyKay47 Posted November 9, 2011 Share Posted November 9, 2011 2 things stand out to me, but really they shouldn't effect the code greatly and it looks to me like it should work. 1. If you set a limit of 1 on your query, you do not need while loop since you are only expecting 1 returned row. 2. distinguishing exit() at the end of your script will not really do anything except cut off your final closing bracket which may be causing your issue.. Quote Link to comment https://forums.phpfreaks.com/topic/250761-php-dynamic-dropdown-get-help/#findComment-1286564 Share on other sites More sharing options...
kamal213 Posted November 9, 2011 Author Share Posted November 9, 2011 I have made those changes, Cheers for that! It doesnt give any errors but it doesnt echo the result which is what I want it to do? just similar to the previous link you showed me..(am trying to avoid ajax coz i have no clue how to use it/how it works lol) Quote Link to comment https://forums.phpfreaks.com/topic/250761-php-dynamic-dropdown-get-help/#findComment-1286571 Share on other sites More sharing options...
AyKay47 Posted November 9, 2011 Share Posted November 9, 2011 try to debug your code to make sure that it is grabbing the select value correctly upon the form submitting, also make sure that you error_reporting is set to 1 and display_errors is ON etc.. Quote Link to comment https://forums.phpfreaks.com/topic/250761-php-dynamic-dropdown-get-help/#findComment-1286574 Share on other sites More sharing options...
kamal213 Posted November 9, 2011 Author Share Posted November 9, 2011 Yeah it works now! I had to include a submit button for it to work. I guess I should use ajax if I dont want to use a submit button? Quote Link to comment https://forums.phpfreaks.com/topic/250761-php-dynamic-dropdown-get-help/#findComment-1286581 Share on other sites More sharing options...
AyKay47 Posted November 9, 2011 Share Posted November 9, 2011 Yeah it works now! I had to include a submit button for it to work. I guess I should use ajax if I dont want to use a submit button? using AJAX will enable you to get a response from the server without refreshing the page, if this is what you want, then yse you will want to use AJAX. Quote Link to comment https://forums.phpfreaks.com/topic/250761-php-dynamic-dropdown-get-help/#findComment-1286591 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.