Jump to content

how to get the value from the database to the checkbox


pixeltrace

Recommended Posts

guys,

 

i need help,

i have a form and it has a check box

permanent

contract

parttime

 

what i want to happen is if the current value in my database is

permanent

i want the checkbox of permanent to be checked.

 

i have a script for it but its not working

hope you could help me with this.

 

below is my code for this problem

<input type="checkbox" name="jtype" value="<? if($_GET['j_type']== 'permanent')print 'checked'; ?>">
                          Permanent
                          <input type="checkbox" name="jtype" value="<? if ($_GET['j_type'] == 'contract')print 'checked'; ?>">
                          Contract
                          <input type="checkbox" name="jtype" value="<? if ($_GET['j_type'] == 'part time')print 'checked'; ?>">
                          Part Time 

 

 

need help on this please

 

thanks!

Link to comment
Share on other sites

Also your saying your getting this from a database and not from a form that was submitted?

 

If that is the case the Request and Get have no clue what your telling it to look for.  You need to setup a varible for each checkbox and its value for it to pass thru with changes.

 

$j_type = $row[x] ;

if ($j_type == 1) {
$j_type_ck = "checked" ;
} else {
$j_type = 0 ;
$j_type_ck = "" ;
}

 

Then for the checkbox

<input type="checkbox" name="j_type" value="<?php echo $j_type ; ?>" <?php echo $j_type_ck ; ?>>

 

Link to comment
Share on other sites

i cannot echo the value since

this page is an updateform page.

 

i just need the current value from the database so the user who will update

will know what is the current j_type that was checked.

 

this is the code for my query and the current code that i have for my checkboxes

<? 
include 'db_connect.php';
$query = mysql_query("SELECT staff_created, j_position, c_name, cperson, specialization, level, industry, c_description, j_requirements, j_responsibilities, j_type, salary, location, DAY(date_posted), MONTH(date_posted), YEAR(date_posted), DAY(date_closed), MONTH(date_closed), YEAR(date_closed), j_status, remarks FROM job_ads WHERE jobid= '$jobid'") or die(mysql_error()); 
$row = mysql_fetch_array( $query );
$staff_created = $row["staff_created"];
$j_position = $row["j_position"];
$c_name = $row["c_name"];
$cperson = $row["c_name"];
$specialization  = $row["specialization"];
$level = $row["level"];
$industry = $row["industry"];
$c_description = $row["c_description"];
$j_requirements = $row["j_requirements"];
$j_responsibilities = $row["j_responsibilities"];
$j_type = $row["j_type"];
$salary = $row["salary"];
$location = $row["location"];
$day_posted = $row["DAY(date_posted)"];
$month_posted = $row["MONTH(date_posted)"];
$year_posted = $row["YEAR(date_posted)"];
$day_closed = $row["DAY(date_closed)"];
$month_closed = $row["MONTH(date_closed)"];
$year_closed = $row["YEAR(date_closed)"];
$j_status = $row["j_status"];
$remarks = $row["remarks"];
?>

 

<input type="checkbox" name="jtype" value="<? if($_REQUEST['j_type']== 'Permanent') { print 'checked' ; }?>">
                          Permanent
                          <input type="checkbox" name="jtype" value="<? if($_REQUEST['j_type']== 'Contract') { print 'checked' ; }?>">
                          Contract
                          <input type="checkbox" name="jtype" value="<? if($_REQUEST['j_type']== 'Part Time') { print 'checked' ; }?>">
                          Part Time 

 

 

hope you could help me.

its not yet working

 

thanks!

Link to comment
Share on other sites

Ok then you need more then just one (1) j_type...

 

$j_type1 = $row['j_type1'] ;
$j_type2 = $row['j_type2'] ;
$j_type3 = $row['j_type3'] ;

if ($j_type1 == 1) {
$j_type1_ck = "checked" ;
} else {
$j_type1 = 0 ;
$j_type1_ck = "" ;
}

if ($j_type2 == 1) {
$j_type2_ck = "checked" ;
} else {
$j_type2 = 0 ;
$j_type2_ck = "" ;
}

if ($j_type3 == 1) {
$j_type3_ck = "checked" ;
} else {
$j_type3 = 0 ;
$j_type3_ck = "" ;
}

 

<input type="checkbox" name="j_type1" value="<?php echo $j_type1 ; ?>" <?php echo $j_type1_ck ; ?>>
<input type="checkbox" name="j_type2" value="<?php echo $j_type2 ; ?>" <?php echo $j_type2_ck ; ?>>
<input type="checkbox" name="j_type3" value="<?php echo $j_type3 ; ?>" <?php echo $j_type3_ck ; ?>>

 

Using that idea should fix you up.. You will need to have the other j_types in the database and 0 or 1 will do just fine. But if you need them to say Permanate etc then...

 

if ($j_type1 == "Permanent") {
$j_type1_ck = "checked" ;
} else {
$j_type1 = "" ;
$j_type1_ck = "" ;
}

if ($j_type2 == "Contract") {
$j_type2_ck = "checked" ;
} else {
$j_type2 = "" ;
$j_type2_ck = "" ;
}

if ($j_type3 == "Part Time") {
$j_type3_ck = "checked" ;
} else {
$j_type3 = "" ;
$j_type3_ck = "" ;
}

 

That will do the trick for that if it need to be the words.

Link to comment
Share on other sites

sorry,

 

i forgot to save my current updateform

 

the name of the checkbox should be

jtype[]

 

since i will use implode in my process page for the jtype[] checkbox

 

what can be the solution for this

assuming that

permanent,

contract

part time

are the current values in my database?

 

hope you could help me on this.

 

thanks in advance!

Link to comment
Share on other sites

What I gave you should work wether you change the name="j_type1" to name="jtype1" for each check box name that is fine but the code above will do what you want.

 

So are you saying your are going to use Explode and Implode to store jtype1 jtype2 and jtype3 into one row of the table?

 

Why do it like?

 

If you implode you will want to do it with a index like - so if all three jtypeX's were selected the entry into one row of the table will look like this Permanat-Contract-Part Time.  This way you explode using - as the index.  But if thats what you want then great.. But when you have the check boxes looking for if checked using the same jtype value they will all be checked.  That is why you need to have values jtype1 jtype2 jtype3.

Link to comment
Share on other sites

TRION,

 

i tried this one

<? 
$jtype = $_GET['j_type'];
				 {
if ($jtype == "Permanent") {
$j_type1_ck = "checked" ;
} else {
$j_type1_ck = "" ;
}

if ($jtype == "Contract") {
$j_type2_ck = "checked" ;
} else {
$j_type2_ck = "" ;
}

if ($jtype == "Part Time") {
$j_type3_ck = "checked" ;
} else {
$j_type3_ck = "" ;
}
}
?>						
					<input type="checkbox" name="jtype[]" value="Permanent" <? echo $j_type_ck1 ; ?>>
                          Permanent
                          <input type="checkbox" name="jtype[]" value="Contract" <? echo $j_type_ck2 ; ?>>
                          Contract
                          <input type="checkbox" name="jtype[]" value="Part Time" <? echo $j_type_ck3 ; ?>>
                          Part Time 

 

 

but its still not working

by the way this is an update page

and with that the value of the checkbox should not be taken from the database

the only problem that i want to solve is

if the value in the database for j_type is

permanent

contract

part time

the checkbox "checked" should be added to the checkbox line.

 

hope you could help me with this.

 

thanks in advance!

Link to comment
Share on other sites

its still not working

:(

 

this is the code of my query

<? 
include 'db_connect.php';
$query = mysql_query("SELECT staff_created, j_position, c_name, cperson, specialization, level, industry, c_description, j_requirements, j_responsibilities, j_type, salary, location, DAY(date_posted), MONTH(date_posted), YEAR(date_posted), DAY(date_closed), MONTH(date_closed), YEAR(date_closed), j_status, remarks FROM job_ads WHERE jobid= '$jobid'") or die(mysql_error()); 
$row = mysql_fetch_array( $query );
$staff_created = $row["staff_created"];
$j_position = $row["j_position"];
$c_name = $row["c_name"];
$cperson = $row["c_name"];
$specialization  = $row["specialization"];
$level = $row["level"];
$industry = $row["industry"];
$c_description = $row["c_description"];
$j_requirements = $row["j_requirements"];
$j_responsibilities = $row["j_responsibilities"];
$j_type = $row["j_type"];
$salary = $row["salary"];
$location = $row["location"];
$day_posted = $row["DAY(date_posted)"];
$month_posted = $row["MONTH(date_posted)"];
//$month_posted1 = $row["MONTH(date_posted, %b)"];
$year_posted = $row["YEAR(date_posted)"];
$day_closed = $row["DAY(date_closed)"];
$month_closed = $row["MONTH(date_closed)"];
//$month_closed1 = $row["MONTH(date_closed, %b)"];
$year_closed = $row["YEAR(date_closed)"];
$j_status = $row["j_status"];
$remarks = $row["remarks"];

?>

 

and this is the code for my checkbox

<?
if ($j_type == "Permanent") {
$j_type1_ck = "checked" ;
} else {
$j_type1_ck = "" ;
}

if ($j_type == "Contract") {
$j_type2_ck = "checked" ;
} else {
$j_type2_ck = "" ;
}

if ($j_type == "Part Time") {
$j_type3_ck = "checked" ;
} else {
$j_type3_ck = "" ;
}
?>						
					<input type="checkbox" name="jtype[]" value="Permanent" <? echo $j_type1_ck ; ?>>
                          Permanent
                          <input type="checkbox" name="jtype[]" value="Contract" <? echo $j_type2_ck ; ?>>
                          Contract
                          <input type="checkbox" name="jtype[]" value="Part Time" <? echo $j_type3_ck ; ?>>
                          Part Time 

 

what could be wrong here?

need help please!

 

thanks!

Link to comment
Share on other sites

Again your still looking at just j_type in your query.  You don't have any other j_type..  Your database needs j_type1 j_type2 j_type3 so that you can then assign those to the other 2 check boxes.

 

You are looking at j_type and lets say its Part Time then only Part Time will show up checked.

Link to comment
Share on other sites

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.