Jump to content
The Uniform Server Community

[Solved] Combo Box trouble


dreeves
 Share

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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/

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 best

Ric :)

Link to comment
Share on other sites

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/

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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/

Link to comment
Share on other sites

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.   Paste as plain text instead

  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.

Loading...
 Share

×
×
  • Create New...