October 14, 200916 yr comment_7718 I have some simple HTML code for a combo box on a form. When I submit the form, all the fields get entered into the mysql database successfully, except for the field in which I used a combo box. It appears in the browser as a combo box with another empty input field to the right of it. Here is the code, it is part of a table: <TD><select name="Practice_Code"> <option selected> Please Select</option> <option value="1.01A">1.01A</option> <option value="1.01B">1.01B</option> <option value="1.02">1.02</option> <option value="1.03">1.03</option> <option value="1.04">1.04</option> <option>Other, please specify:</option> </select> <input type= "text" name="Practice_Code"/></TD> I've never used Java, so I was hoping to avoid it unless I have to use it in this case. Thanks. Report
October 14, 200916 yr comment_7721 Your dropdown list and single input field have identical names they should be unique. All the bestRic Report
October 15, 200916 yr Author comment_7725 I have changed that, but it is still not working properly. As it appears, there is a combo box, then an input field to the right (useful for "Other, please specify:"). Whatever I put in that box will appear in my database, but I would like the combo box selection to appear instead. Because the Internet offers so many different solutions, it was a little overwhelming. I was hoping to get a simple solution here. Report
October 15, 200916 yr Author comment_7726 Here is the code for the checkbox: <TD><INPUT TYPE=CHECKBOX NAME="Direct_Pay" value="yes"> Amount should be paid directly paid to contractor</TD> That info is sent to insert.php which inserts the info into a mysql database. Line 22 of insert.php states:(line 22) $Direct_Pay=$_POST['Direct_Pay']; When the user "checks" the box, it works fine. When the user does not check the box, the error logs states: [error] [client 127.0.0.1] PHP Notice: Undefined index: Direct_Pay in C:\\wamp\\www\\insert.php on line 22, referer: http://localhost/form.html Report
October 15, 200916 yr Author comment_7727 I've figured it out, I insert a hidden form field with the same name as the checkbox and the default value right before the place where the checkbox is located. So... <input type="hidden" name="Direct_Pay" value="No" /><INPUT TYPE=CHECKBOX NAME="Direct_Pay" value="Yes"> Amount should be paid directly paid to contractor This worked great. When unchecked, "No" is input to the database by default. Report
October 15, 200916 yr comment_7730 Hello, You should really be doing this check in PHP rather than utilizing a hidden input field. You also have a value set to yes so lets make use of that. <INPUT TYPE=CHECKBOX NAME="Direct_Pay" value="Yes"> Amount should be paid directly paid to contractor // In PHP // If Direct Pay is set (checked) and value of it is Yes... if ( isset($_POST['Direct_Pay']) && $_POST['Direct_Pay'] == "Yes" ) { // Insert yes into DB } // otherwise if not set and value is not Yes, insert no else { // Insert no into DB } You force the logic with PHP using IF statements. Hope that helps,Kalpz ATOMIC Web Hosting 2007 - 2011 Shared Hosting - Reseller Hosting - Dedicated Servers - Virtual Private Servers (Request Dedicated/VPS Servers via E-mail) E-mail: sales@atomicwebhosting.com Website: http://www.atomicwebhosting.com/ Report
October 21, 200916 yr Author comment_7747 kalpz, That makes a little more sense. I am new to php, so I'm not quite sure what to put to fill in your comments like: // Insert yes into DB //otherwise if not set and value is not Yes. insert no //Insert no into DB I know that you couldn't explain it any simpler to me, but could you clarify or elaborate? Report
October 21, 200916 yr comment_7748 Sorry to but in!Kalpz pointed you in the right direction and rightly assumed you could fill in the blanks: “When I submit the form, all the fields get entered into the mysql database successfully” I think if you supply code you are using to write to the database he would give you another pointer in the right direction. All the bestRic Report
October 23, 200916 yr comment_7753 Hi there,Yes, I didn't supply further code as I wasn't sure how your database looks. Here it is anyway, you will just need to change the queries slightly. <INPUT TYPE=CHECKBOX NAME="Direct_Pay" value="Yes"> Amount should be paid directly paid to contractor // In PHP // If Direct Pay is set (checked) and value of it is Yes... if ( isset($_POST['Direct_Pay']) && $_POST['Direct_Pay'] == "Yes" ) { // Insert yes into DB mysql_query("INSERT INTO table_name (direct_pay_field) VALUES ('Yes')"); } // otherwise if not set and value is not Yes, insert no // This comment is to show you what this if logic is doing.. else { // Insert no into DB mysql_query("INSERT INTO table_name (direct_pay_field) VALUES ('No')"); } Hope that helps. ATOMIC Web Hosting 2007 - 2011 Shared Hosting - Reseller Hosting - Dedicated Servers - Virtual Private Servers (Request Dedicated/VPS Servers via E-mail) E-mail: sales@atomicwebhosting.com Website: http://www.atomicwebhosting.com/ Report
October 23, 200916 yr Author comment_7758 Great. That helps to see it written out like that. 'Direct_Pay' is one of many form inputs. I had constructed one single query that inserts all the variables into a database record. Is there a way I could store 'Yes' or 'No' in a variable so that it gets inserted into the correct record? Ideally, the query in my php script would be: INSERT INTO table_name (Auto_Increment_ID, Name, Address, Email, Direct_Pay) VALUES ('', '$Name', '$Address', '$Email', '$Direct_Pay'); If I have a separate INSERT query for Direct_Pay, as described in your response, it will insert it as a unique record with the other 4 fields not filled in. If I understand correctly. Is there a way to store the Yes/No as a variable? Report
October 23, 200916 yr comment_7765 Yes it is possible and it is done like so: <INPUT TYPE=CHECKBOX NAME="Direct_Pay" value="Yes"> Amount should be paid directly paid to contractor // Set variable $directPay = ''; // If Direct Pay is set (checked) and value of it is Yes... if ( isset($_POST['Direct_Pay']) && $_POST['Direct_Pay'] == "Yes" ) { // Set variable to yes $directPay = 'Yes'; } else { // Set variable to No $directPay = 'No'; } // Do query after the above check mysql_query("INSERT INTO table_name (Name, Address, Email, Direct_Pay) VALUES ('$Name', '$Address', '$Email', '$directPay')"); Hope that helps. Edited October 23, 200916 yr by kalpz ATOMIC Web Hosting 2007 - 2011 Shared Hosting - Reseller Hosting - Dedicated Servers - Virtual Private Servers (Request Dedicated/VPS Servers via E-mail) E-mail: sales@atomicwebhosting.com Website: http://www.atomicwebhosting.com/ Report
October 23, 200916 yr Author comment_7768 I made the changes but "No" is not appearing. Just to be sure I'm getting this right: This line goes in the form html <INPUT TYPE=CHECKBOX NAME="Direct_Pay" value="Yes"> Amount should be paid directly paid to contractor And this code goes in the php insert script // Set variable $directPay = ''; // If Direct Pay is set (checked) and value of it is Yes... if ( isset($_POST['Direct_Pay']) && $_POST['Direct_Pay'] == "Yes" ) { // Set variable to yes $directPay = 'Yes'; } else { // Set variable to No $directPay = 'No'; } // Do query after the above check mysql_query("INSERT INTO table_name (Name, Address, Email, Direct_Pay) VALUES ('$Name', '$Address', '$Email', '$directPay')"); When the box is unchecked, "No" is not entered into the table. Did you distinct between directPay and Direct_Pay correctly? Report
October 23, 200916 yr comment_7769 Hi,Yes, that is corrent. The input code is HTML, the rest is PHP. It should work as I have tested this on my server. Try changing both $directPay lines to double quotes instead of single quotes? Example: $directPay = "Yes"; ATOMIC Web Hosting 2007 - 2011 Shared Hosting - Reseller Hosting - Dedicated Servers - Virtual Private Servers (Request Dedicated/VPS Servers via E-mail) E-mail: sales@atomicwebhosting.com Website: http://www.atomicwebhosting.com/ Report
October 27, 200916 yr Author comment_7791 When I started over and retyped it, it worked just fine. Thanks for your help! Report
October 27, 200916 yr comment_7794 Your very welcome! Kalpz ATOMIC Web Hosting 2007 - 2011 Shared Hosting - Reseller Hosting - Dedicated Servers - Virtual Private Servers (Request Dedicated/VPS Servers via E-mail) E-mail: sales@atomicwebhosting.com Website: http://www.atomicwebhosting.com/ Report
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.