11 Replies - 721 Views - Last Post: 20 September 2013 - 06:01 AM Rate Topic: -----

#1 Kiranja407  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 16-September 13

html checkbox send wrong value

Posted 16 September 2013 - 07:31 AM

Hi! am a new member, I got a problem when I was dealing with checkbox.. they send word 'array' in database table instead of the value I want...

example: Column Connectivity has 1. Bluetooth
2. Infared
3. GPS
4. Wlan / Wifi
5. USB
here is it's html


<div class="element-checkbox" ><label class="title">Connectivity :</label>		
<div class="column" style="width:33%">
<input type="checkbox" name="conn[]" value="Bluetooth"/ ><span>Bluetooth</span><br/>
<input type="checkbox" name="conn[]" value="GPS"/ ><span>GPS</span><br/></div>
                <span class="clearfix"></span>
		<div class="column" style="width:33%">
<input type="checkbox" name="conn[]" value="Wlan / Wi-fi"/ ><span>Wlan / Wi-fi</span><br/>
<input type="checkbox" name="conn[]" value="USB "/ ><span>USB </span><br/></div>
                <span class="clearfix"></span>
		<div class="column" style="width:33%">
<input type="checkbox" name="conn[]" value="Infared"/ ><span>Infared</span><br/></div>
<span class="clearfix"></span>
</div>
 




here is the php code :

note:

database name = 'sims'
table name = 'entry'
column name( for check values) = 'connection'

conn ('$_POST[conn]') = php valiable for post value to mysql table:


mysql_select_db("sims", $con);

$sql="INSERT INTO entry (phone_name,manufacture,availability,phone_colour,sim_card,operating_system,connectivity,cpu,ram,messaging,battery_type,standby_time,talk_time,features,inbuilt_memory,data,browser_present,serial_number,camera,packed_with,media,phone_gurantee,price_us,price_ts,password)
VALUES
('$_POST[pname]','$_POST[manu]','$_POST[ava]','$_POST[pcolo]','$_POST[simc]','$_POST[osy]','$_POST[conn]','$_POST[cpu]','$_POST[ram]','$_POST[mes]','$_POST[bty]','$_POST[stb]','$_POST[tk]','$_POST[fet]','$_POST[inb]','$_POST[dat]','$_POST[brp]','$_POST[sen]','$_POST[cam]','$_POST[paw]','$_POST[med]','$_POST[phg]','$_POST[prus]','$_POST[prts]','$_POST[pass]')";




Ihave also include some photo and full codes; I have attached all files....

Attached image(s)

  • Attached Image
  • Attached Image

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: html checkbox send wrong value

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3253
  • View blog
  • Posts: 10,908
  • Joined: 12-December 12

Re: html checkbox send wrong value

Posted 16 September 2013 - 07:42 AM

Yes, $_POST['conn'] is an array of the posted values, that's what name="conn[]" achieves with the square brackets.

If you want to loop through the checked items:

$conns = $_POST['conn'];

foreach ($conns as $value) {
    // do something with $value
}

Then what will you do with the values? Or perhaps there should only be one item checked? If so then $conns[0] would contain the value you want to store in the database (having checked that there is only one value of course). [You would also need to use some Javascript so that they could only check one item - but this process would not be foolproof, still requiring you to count the number of posted values.]

Note that any unchecked items will not be posted.

Note also that the mysql extension is deprecated: see my signature, and you should not post unsanitized POST data directly into a database, use prepared statements instead (see my signature again).

This post has been edited by andrewsw: 16 September 2013 - 07:49 AM

Was This Post Helpful? 1
  • +
  • -

#3 Kiranja407  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 16-September 13

Re: html checkbox send wrong value

Posted 16 September 2013 - 08:52 AM

andrewsw:

Thanks for reply, my idea is to insert in column 'conectivity' at least 1 value, if user want to insert all is ok! but the field should not be empty.. also i tried to use the code you gave me and i get this responce...
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("sims", $con);


$conns = $_POST['conn'];
	 
	foreach ($conns as $value) 
	{
	   


"INSERT INTO entry (phone_name,manufacture,availability,phone_colour,sim_card,
operating_system,connectivity,cpu,ram,messaging,
battery_type,standby_time,talk_time,features,inbuilt_memory,
data,browser_present,serial_number,camera,packed_with,media,
phone_gurantee,price_us,price_ts,password)

VALUES
('$_POST[pname]','$_POST[manu]','$_POST[ava]','$_POST[pcolo]',
'$_POST[simc]','$_POST[osy]',".$conns.",
'$_POST[cpu]','$_POST[ram]','$_POST[mes]',
'$_POST[bty]','$_POST[stb]','$_POST[tk]',
'$_POST[fet]','$_POST[inb]','$_POST[dat]',
'$_POST[brp]','$_POST[sen]','$_POST[cam]',
'$_POST[paw]','$_POST[med]','$_POST[phg]',
'$_POST[prus]','$_POST[prts]','$_POST[pass]')";

	}
 

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
	echo "<table border='0' width='550px' align='center' cellpadding='4' cellspacing='4'>";
				echo "<tr align='center' bgcolor='#66ccff' >
				 
				 <td allign='center' height='30px' width='150px' ><B><H4>ONE ENTRY ADDED</H4></B></td> </tr>";

mysql_close($con);
?> 



Also thank for remind me about using prepared statements, But where I can find your signature which you have told me?

Sorry for disturbance and my English if you can please reply.

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3494
  • View blog
  • Posts: 10,072
  • Joined: 08-June 10

Re: html checkbox send wrong value

Posted 16 September 2013 - 08:53 AM

View Postandrewsw, on 16 September 2013 - 04:42 PM, said:

Or perhaps there should only be one item checked? If so then $conns[0] would contain the value you want to store in the database (having checked that there is only one value of course). [You would also need to use some Javascript so that they could only check one item - but this process would not be foolproof, still requiring you to count the number of posted values.]

in that case use radio buttons with an identical name. no Javascript required.
Was This Post Helpful? 1
  • +
  • -

#5 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5191
  • View blog
  • Posts: 26,903
  • Joined: 10-May 07

Re: html checkbox send wrong value

Posted 16 September 2013 - 08:58 AM

View PostKiranja407, on 16 September 2013 - 10:31 AM, said:

example: Column Connectivity has 1. Bluetooth
2. Infared
3. GPS
4. Wlan / Wifi
5. USB
<input type="checkbox" name="conn[]" value="Wlan / Wi-fi"/ ><span>Wlan / Wi-fi</span><br/>


I would avoid using a slash in the value. Honestly, I would avoid using character strings & would use numbers. This would allow you to make an admin panel that can dynamically change the selections, & base them off id's.

Also to note, what if someone checks more than one box? What do you expect to have happen?
Was This Post Helpful? 1
  • +
  • -

#6 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3253
  • View blog
  • Posts: 10,908
  • Joined: 12-December 12

Re: html checkbox send wrong value

Posted 16 September 2013 - 08:58 AM

View PostDormilich, on 16 September 2013 - 03:53 PM, said:

in that case use radio buttons with an identical name. no Javascript required.

Thanks, yes, I should have mentioned this preference: it's what they are for ;)
Was This Post Helpful? 0
  • +
  • -

#7 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3494
  • View blog
  • Posts: 10,072
  • Joined: 08-June 10

Re: html checkbox send wrong value

Posted 16 September 2013 - 08:59 AM

View PostKiranja407, on 16 September 2013 - 05:52 PM, said:

But where I can find your signature which you have told me?

go to "My Account" > "Edit My Profile", select the "Forums" tab, check "View members' signatures when reading topics?" from "Board Display Settings"

alternately: go to the member’s profile page, click the "About Me" tab

This post has been edited by Dormilich: 16 September 2013 - 09:02 AM

Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3253
  • View blog
  • Posts: 10,908
  • Joined: 12-December 12

Re: html checkbox send wrong value

Posted 16 September 2013 - 09:09 AM

Use print_r($_POST); and view the source for the resultant html to check what is in this array. Please post the results.

You should also be inserting $value into the table, not the original $conns array.

BTW tr's no longer have align or bgcolor attributes, and you've mis-spelt align (allign) and Infrared (Infared in your OP).
Was This Post Helpful? 0
  • +
  • -

#9 Kiranja407  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 16-September 13

Re: html checkbox send wrong value

Posted 16 September 2013 - 09:13 AM

andrewsw , no2pencil , Dormilich

Thanks for reply guys,

My idea is to insert in column 'conectivity' at least 2 value, if user want to insert all is ok! but the field should not be empty.. also i tried to use the code you gave me and i get this responce -look at the photo below - ...

I did not try to use Radio button since user might need to check more than 3 box Dormilich .





<?php
	$con = mysql_connect("localhost","root","");
	if (!$con)
	  {
	  die('Could not connect: ' . mysql_error());
	  }
	 
	mysql_select_db("sims", $con);
	 
	$conns = $_POST['conn'];
	      
	    foreach ($conns as $value)
	    {
	        
	 
	 
	"INSERT INTO entry (phone_name,manufacture,availability,phone_colour,sim_card,
	operating_system,connectivity,cpu,ram,messaging,
	battery_type,standby_time,talk_time,features,inbuilt_memory,
	data,browser_present,serial_number,camera,packed_with,media,
	phone_gurantee,price_us,price_ts,password)
	 
	VALUES
	('$_POST[pname]','$_POST[manu]','$_POST[ava]','$_POST[pcolo]',
	'$_POST[simc]','$_POST[osy]',".$conns.",
	'$_POST[cpu]','$_POST[ram]','$_POST[mes]',
	'$_POST[bty]','$_POST[stb]','$_POST[tk]',
	'$_POST[fet]','$_POST[inb]','$_POST[dat]',
	'$_POST[brp]','$_POST[sen]','$_POST[cam]',
	'$_POST[paw]','$_POST[med]','$_POST[phg]',
	'$_POST[prus]','$_POST[prts]','$_POST[pass]')";
	 
	    }
	  
	 
	if (!mysql_query($sql,$con))
	  {
	  die('Error: ' . mysql_error());
	  }
	    echo "<table border='0' width='550px' align='center' cellpadding='4' cellspacing='4'>";
	                echo "<tr align='center' bgcolor='#66ccff' >
	                  
	                 <td allign='center' height='30px' width='150px' ><B><H4>ONE ENTRY ADDED</H4></B></td> </tr>";
	 
	mysql_close($con);
	?> 




Also thank for remind me about using prepared statements, But where I can find your signature which you have told me?

Also I will change the value wlan / wi-fi as you adviced no2pencil

Sorry for disturbance and my English if you can please reply.

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#10 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3494
  • View blog
  • Posts: 10,072
  • Joined: 08-June 10

Re: html checkbox send wrong value

Posted 16 September 2013 - 09:22 AM

View PostKiranja407, on 16 September 2013 - 06:13 PM, said:

But where I can find your signature which you have told me?

cf. post #7.

error #1: there is no such param "conn" in your POST data.

error #2: see error #1.

error #3: $sql is nowhere defined. should have been set at line #17.
Was This Post Helpful? 0
  • +
  • -

#11 Kiranja407  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 16-September 13

Re: html checkbox send wrong value

Posted 20 September 2013 - 05:46 AM

Sorry guys for being silent I was sick, however I tried using code which andrewsw told me


conns = $_POST['conn'];
       foreach ($conns as $value)  {
		   
		   $insv= implode(',',$value);
		   
$sql="INSERT INTO entry (phone_name,manufacture,availability,phone_colour,sim_card,
operating_system,connectivity,cpu,ram,messaging,
battery_type,standby_time,talk_time,features,inbuilt_memory,
data,browser_present,serial_number,camera,packed_with,media,
phone_gurantee,price_us,price_ts,password)

VALUES
('$_POST[pname]','$_POST[manu]','$_POST[ava]','$_POST[pcolo]',
'$_POST[simc]','$_POST[osy]','$value',
'$_POST[cpu]','$_POST[ram]','$_POST[mes]',
'$_POST[bty]','$_POST[stb]','$_POST[tk]',
'$_POST[fet]','$_POST[inb]','$_POST[dat]',
'$_POST[brp]','$_POST[sen]','$_POST[cam]',
'$_POST[paw]','$_POST[med]','$_POST[phg]',
'$_POST[prus]','$_POST[prts]','$_POST[pass]')";




and it work although If you check two iteams only the last checked will be added, but is better half than notrhing... Thanks...

And I took Dormilich advice and I changed them to radio button :

Thanks...all below is the picture before and after applying the code foreach

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#12 Kiranja407  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 16-September 13

Re: html checkbox send wrong value

Posted 20 September 2013 - 06:01 AM

andrewsw

Code you gave me worked.. although only one check item is added or the last checked....

All in all code worked Thanks..
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1