By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,303 Members | 1,260 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,303 IT Pros & Developers. It's quick & easy.

pass selected (drop-down) value to submit button

hannable80
P: 2
Hi,

I have being doing some php code for the very first time using wordpress as a template, basicaly what i want the prog to do is get data from a drop down menu, which i have populated with data from a MSQL db. pass it to a button (on the same page) and it to run a SQL statemment and populate the data in a table.
What i am having problems with is to get the data to the botton where it will execute on the same page.
Code below:[php]<?php
$dbhost = 'it.test.com';
$dbuser = 'audit';
$dbpass = 'audit';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
$dbname = 'WinAudi2';
mysql_select_db($dbname);
$result = mysql_query( "SELECT DISTINCT Computer FROM $dbname" ) or die("SELECT Error: ".mysql_error());
$num_rows = mysql_num_rows($result);
print "There are $num_rows records.<P>";
print "<table width=100 border=1>\n";
print "<select>";
while ($get_info = mysql_fetch_row($result)){
foreach ($get_info as $field)
print "<option>$field</option>\n";
}
print "</select></table>\n";
?>
</body>
</html>
[/php]

Please read the Posting Guidelines, its link is on the top of this forum. Especially the part about enclosing code within the appropriate code tags. - moderator
Feb 28 '08 #1
Share this Question
Share on Google+
3 Replies


ronverdonk
Expert 2.5K+
P: 4,258
Welcome to TSDN!

What exactly is it you want or your problem?
You are already building a <select> drop down list, but I don't see a <form> or a submit button.
When you add that and after the selection has been submitted, what do you want to do with the passed data? And do you want to process it in the same script or in a new one. The previous is not clear to me.

If you have developed any more code to process the submitted data, show it here.

Ronald
Feb 28 '08 #2

hannable80
P: 2
Welcome to TSDN!

What exactly is it you want or your problem?
You are already building a <select> drop down list, but I don't see a <form> or a submit button.
When you add that and after the selection has been submitted, what do you want to do with the passed data? And do you want to process it in the same script or in a new one. The previous is not clear to me.

If you have developed any more code to process the submitted data, show it here.

Ronald
Cheers

sorry about the pm
Expand|Select|Wrap|Line Numbers
  1.  <FORM>
  2. <INPUT type="button" value="Submit" name="GO"/>
  3. </FORM>
When i even this small bit of code in it throws a syntax error...
I have populated the drop down. I need to select a highlighted option in it and pass it as a var to a button. Then the button "onclick" will run a sql statement with the Value inserted from the drop down and refesh the page and dump out a table with the results of the statement...WoW that sounds alot but id say it would only be about 6 lines of code.

I have been reading up on it all day but i can't find a way to pass the high lighted data to a button!

As you can tell im more of a casual programmer SORRY!
Feb 28 '08 #3

ronverdonk
Expert 2.5K+
P: 4,258
Before you go any further, the following remarks on the code shown:

1. you use a database name 'WinAudi2' and a table 'WinAudi2'. That does not look correct to me.
2. your <option>'s have no values assigned to them.
3. your <select> list is not within a form, so where do you want to send the data to?
4. the <form> statement just has a submit attribute, but the form itself goes nowhere after hitting that submit
5. you want to pass the selection option value to a button. How do you intend to catch that value, via a submit to a script or using JavaScript?

I would propose the following:

1. assign values to the drop-down list, so they can be passed.
2. have the drop-down list AND the submit button within a <form>
3. after selecting an option from the drop-down, its value will be stored in the submit button. After hitting the submit button, the value of the drop-down is passed to the same script, where that value is inserted into the database and a 'dump-out' of the table is made? DUMP-OUT???

In order to prevent endless discussions here about what everybody means or wants, I'll take a shortcut and did some code work (only this time!). See the next (sanitized) code:[php]<script type="text/javascript">
function setButton(myvalue)
{
document.getElementById("myButton").value="Click here to \nupdate '"+myvalue+"' in database";
}
</script>
<?php
// prepare and connect MySQL actions
$dbhost = 'it.test.com';
$dbuser = 'audit';
$dbpass = 'audit';
$conn = mysql_connect($dbhost, $dbuser, $dbpass)
or die('Error connecting to mysql');
$dbname = 'WinAudi2';
mysql_select_db($dbname);

// Check to see if form was posted
if (isset($_POST['selectit'])) {
// if so, do what you plan to do
echo "Clicked and passed value: {$_POST['selectit']}<br>";
print "Update table in database<br>";
print "Show table<br>";
}
// form not posted: display selection box
else {
$result = mysql_query( "SELECT DISTINCT Computer FROM $dbname" )
or die("SELECT Error: ".mysql_error());
$num_rows = mysql_num_rows($result);
print "<p>There are $num_rows records.</p>\n";
print "Select an option from the following list:\n";
print "<form method='POST' action='".$_SERVER['PHP_SELF']."'}>\n";
print "<select name='selectit' onChange='setButton(this.value);'> >\n";
while ($get_info = mysql_fetch_row($result)) {
foreach ($get_info as $field)
print "<option value='$field'>$field</option>\n";
}
print "</select>\n";
print "<p><input type='submit' value='&nbsp;&nbsp;&nbsp;' id='myButton'/></p>\n";
print "</form>";
}
?>[/php]Ronald
Feb 28 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.