Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/250761-php-dynamic-dropdown-get-help/
Share on other sites

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?

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/

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();
		}
		?>

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..

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)

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.