Jump to content

[SOLVED] Problem with radio button form......


Dogrox

Recommended Posts

Hello,

 

I have seen someone else have this problem but thier situation was more complex then what i have.  First of all i have coded alot of BASIC  php routines.. so i am familiar with the code but NOT an expert by all means.  :)

 

I have a MYSQL database which has a LIST of 50 boxes (lets say,  Crates so it doesnt confuse all) .  I need the PHP code to  get that data from the MYSQL database and display it as a FORM list that displays the Crate number  (which is the Primary)  along with a set of  RADIO buttons for each CRATE that has three options,  WHITE, YELLOW and RED.  At the bottom there is a SUBMIT button that submits the whole data at once and write it back to the same DATATABLE in the MYSQL DB!

 

 

I can access the database that's not a problem.  The problem I am having is with the radio button "NAME"  cause it only  allows one BUTTON to check for the WHOLE list.  I did have it previously working at one time, but thier was a problem WHEN I was trying to WRITE the data back after hitting the SUBMIT button so i trashed the whole code and started from scratch.  NOW I  am back to the problem square one.

 

 

here is the code of the way it displays the list!

 


// begin display  results

while ($row = mysql_fetch_array($queryResource, MYSQL_ASSOC)) 

{



if($row_count % 2) 
	{  
	echo '<TR bgcolor="#222222">';
	} 	else	{ 
	echo '<TR bgcolor="#333333">';
	}


// EDIT  FORM STARTS HERE

echo '	<form id="boxcolour" action="" method="post">';	   	
   	
echo '
		<td align="center"><b> . $row['tableid'] . '</b></td>

		<td>';

echo '	<INPUT type="radio" value="0" name="status[$status]" ';
		if ($row['status'] == '0') echo 'checked="checked" ';
		echo ' >White';

echo '	<INPUT type="radio" value="1" name="status[$status]" ';
		if ($row['status'] == '1') echo 'checked="checked" ';
		echo ' >Yellow';

echo '	<INPUT type="radio" value="2" name="status[$status]" ';
		if ($row['status'] == '2') echo 'checked="checked" ';
		echo ' >Red';

echo '	</td></tr>';	
   		
   
$row_count++; 



}

// EDIT  FORM ENDS HERE

// Submit code starts here



 

 

 

First the $row_count variable is only used for alternating the COLOUR for each row so it displ;ays better.  That works fine but thats not a problem as it is only one variable.

 

 

Also like I say I HAVE ripped out the "SUBMIT" code, because that was ALL messed up to begin with.  As I tried to  get the "SUBMIT" code to work and i have had NO LUCK  :(  So any help with that, as well,  would ALSO  be greatly appreciated!

 

Thank you for all your kind help!

 

 

 

 

Link to comment
Share on other sites

Hey F1Fan!

Not shure what you mean!! 

 

But I think I may clear this up by adding that the table in the mysql database as  only two entrys  per row.  One is the Crate Number, which is the "tableid".. which doesn' get updated (nor deleted) and the second one is  the "status" of the crate. The "status" of the crate is an integer, with a value of 0, 1, or 2.

 

The 'Status" coloum is the one that gets updated by the radio buttons, that one is to select either  WHITE (0), YELLOW (1), or RED (2).  When the SUBMIT button is hit,  the changes for the WHOLE list of 50 Crates 's STATUS is  changed at once.

 

So you will see

 

CRATE#            STATUS

1                      () White () YELLOW  () RED 

2                      () White () YELLOW  () RED 

3                      () White () YELLOW  () RED 

4                      () White () YELLOW  () RED 

etc etc  all the way to  50

 

then  SUMBIT!

 

Thats all the changes that need to be done to that particular DATABASE table!

 

 

Then when they need to go in and want to update a particular Crate Status, that it will KNOW what is already been checked from the database. Hence the lines:

 

if ($row['status'] == '0') echo 'checked="checked" ';

 

on each  radio button!

 

The problem I think i am having is the ARRAY for the list.  And what to call the "NAME" of each radio button!!

 

And not to mention the actual SUBMIT code when the button is hit!

 

I hope that clears up what i need?!

 

Thank you in advance!

 

 

 

 

Link to comment
Share on other sites

OK, let's take it one step at a time. First, let's get your radio buttons working.

 

You can only select ONE of any set of radio buttons that has the same name. So, if you need to have something like this:

 

CRATE#            STATUS

1                      (X) White () YELLOW  () RED 

2                      () White () YELLOW  (X) RED 

3                      () White (X) YELLOW  () RED 

4                      () White () YELLOW  (X) RED 

etc etc  all the way to  50

 

then you will need to name each line's radio buttons something unique.

 

You could do something like name="buttonname<?php echo $crate; ?>" or something similar.

Link to comment
Share on other sites

Ok thats exactly what i need to have as far as what you you displayed.  :)

 

Which is correct, you can only select EITHER  white, yellow, or red.

 

the radio buttons are  setting the STATUS column for each crate.

 

Oh also.. that the whole page is in PHP  so that everything  html is  ECHOed.. i wouldnt need to add that  <php> tags in there on the html lines. I just wnated to add that cause that is confusing me to know if your using a Array Variable there?!  hehe Sorry about that! Like i say i am not an expert!

 

 

But your way of $crate.  how would i assign that to an array siince I am using the WHILE command for the loop?!

 

Link to comment
Share on other sites

For starters, try this:

 

<?php
// begin display  results
while ($row = mysql_fetch_array($queryResource, MYSQL_ASSOC)) 

{
if($row_count % 2) 
	{  
	echo '<TR bgcolor="#222222">';
	} 	else	{ 
	echo '<TR bgcolor="#333333">';
	}

// EDIT  FORM STARTS HERE
echo "<form id=\"boxcolour\" action=\"\" method=\"post\">";	   	
echo "<td align=\"center\"><b>{$row['tableid']}</b></td>";
echo "<td><INPUT type=\"radio\" value=\"0\" name=\"status{$row['tableid']}[$status]\" ";
if ($row['status'] == '0') echo "checked=\"checked\" ";
echo " >White";
echo "<INPUT type=\"radio\" value=\"1\" name=\"status{$row['tableid']}[$status]\" ";
if ($row['status'] == '1') echo "checked=\"checked\" ";
echo " >Yellow";
echo "	<INPUT type=\"radio\" value=\"2\" name=\"status{$row['tableid']}[$status]\" ";
if ($row['status'] == '2') echo "checked=\"checked\" ";
echo " >Red";
echo "</td></tr>";	
$row_count++; 
}
// EDIT  FORM ENDS HERE
// Submit code starts here
?>

 

Never forget the importance of single vs. double quotes.

Link to comment
Share on other sites

That shouldn't have caused a problem, but here's another way to do it:

 

<?php
// begin display  results
while ($row = mysql_fetch_array($queryResource, MYSQL_ASSOC)) 
{
if($row_count % 2) 
	{  
	echo '<TR bgcolor="#222222">';
	} 	else	{ 
	echo '<TR bgcolor="#333333">';
	}

// EDIT  FORM STARTS HERE
echo "<form id=\"boxcolour\" action=\"\" method=\"post\">";	   	
echo "<td align=\"center\"><b>".$row['tableid']."</b></td>";
echo "<td><INPUT type=\"radio\" value=\"0\" name=\"status".$row['tableid']."[$status]\" ";
if ($row['status'] == '0') echo "checked=\"checked\" ";
echo " >White";
echo "<INPUT type=\"radio\" value=\"1\" name=\"status".$row['tableid']."[$status]\" ";
if ($row['status'] == '1') echo "checked=\"checked\" ";
echo " >Yellow";
echo "	<INPUT type=\"radio\" value=\"2\" name=\"status".$row['tableid']."[$status]\" ";
if ($row['status'] == '2') echo "checked=\"checked\" ";
echo " >Red";
echo "</td></tr>";	
$row_count++; 
}
// EDIT  FORM ENDS HERE
// Submit code starts here
?>

Link to comment
Share on other sites

Ok that worked better!  I dont knwo why the  why them curly brackets didnt work! 

 

The array is now working!

 

That was a stupid little problem heheh I know.  Like i said i had it working before but the array was giving me a different output Because i was using a different variable in the raido button name!  SO your  variable you are using is  showing the correct array! 

 

 

Array

(

    [status1] => Array

        (

            [0] => 0

        )

 

    [status2] => Array

        (

            [0] => 0

        )

 

    [status3] => Array

        (

            [0] => 0

        )

 

    [status4] => Array

        (

            [0] => 0

        )

 

    [status5] => Array

        (

            [0] => 0

        )

 

    [status6] => Array

        (

            [0] => 0

etc etc etc

 

which seems right now...

 

 

The old way i had it was giving me the full array for all 50  but the array name was just :  [status] => Array"  on each line!!.. wihtout the number next to the word "status" in the brackets

 

 

 

 

Ok  now the problem is  how to get that array back for the UPDATEING  of the "status" field of each crate?

 

II used the FOR EACH command before.. but it didnt work so i ripped it out!

 

 

the UPDATE code will be in the same  php file..

 

 

 

This is the submit button

 

echo' <input type="Submit" name="update" value="Update"/>';

 

 

The name is "update"

 

so i was using somethign like this

 

if (!$_POST[update])

 

to start it!

 

 

Link to comment
Share on other sites

Actually, try this. I changed the radio to create a multi-dimensional array. That will make it MUCH easier to work with.

 

<?php
// begin display  results
while ($row = mysql_fetch_array($queryResource, MYSQL_ASSOC)) 
{
if($row_count % 2) 
	{  
	echo '<TR bgcolor="#222222">';
	} 	else	{ 
	echo '<TR bgcolor="#333333">';
	}

// EDIT  FORM STARTS HERE
echo "<form id=\"boxcolour\" action=\"\" method=\"post\">";	   	
echo "<td align=\"center\"><b>".$row['tableid']."</b></td>";
echo "<td><INPUT type=\"radio\" value=\"0\" name=\"status[".$row['tableid']."][$status]\" ";
if ($row['status'] == '0') echo "checked=\"checked\" ";
echo " >White";
echo "<INPUT type=\"radio\" value=\"1\" name=\"status[".$row['tableid']."][$status]\" ";
if ($row['status'] == '1') echo "checked=\"checked\" ";
echo " >Yellow";
echo "	<INPUT type=\"radio\" value=\"2\" name=\"status[".$row['tableid']."][$status]\" ";
if ($row['status'] == '2') echo "checked=\"checked\" ";
echo " >Red";
echo "</td></tr>";	
$row_count++; 
}
// EDIT  FORM ENDS HERE
?>

 

Then, add a checker at the TOP of your code to update the table. If you update at the top of the code, it'll save to the database and then reload the page saved. You can do something like this:

 

<?php
if (isset($_POST['update'])){
  foreach ($_POST['status'] as $tableid=>$status){
    //Update your DB here with the data you received
  }
}
?>

Link to comment
Share on other sites

OK i will try that.. i see the changes you made to the radio  names.

 

What is confusing me is the quotes. it always did  for a long time..  thats why i always used the echo ' ';  not the echo " ";  cause everytign in between was easier to read when sperating variables from the quoted text!  I dont know  if i could have used your array variable for the status name.. the ay i would have echoed it?!  COuld I?  maybe that was my problem?!

 

anyways i will try that new code.... and get back to you!

 

thank you!

 

Link to comment
Share on other sites

Single quotes are for literal strings.

 

For instance, if you did this:

<?php echo 'String: $string \n'; ?>

that would print this:

String: $string \n

while this:

<?php echo "String: $string \n"; ?>

would print this (assuming the value of string was "Hello World":

String: Hello World

Hope that helps.

Link to comment
Share on other sites

That is true. If you always used single quotes, there wouldn't be any confusion. However, once you program more and more, you'll appreciate how much less time this would take:

echo "Hello worls, $string is today.";

versus:

echo 'Hello world, ' . $string . ' is today.';

 

Have you tried that code yet?

Link to comment
Share on other sites

doode.. you make a good  VALID point!  :o)

thatwould make it easier to code. hehe  I have only been doing php for about 2 years now.. NOT full time just here and there the site I built for a store took me two years.. well off and on  since i cant sit in one position for a long time.  It is all done in PHP. using smarty.. .  just simple things.. i never had to work with  stupid radio buttons..  but I fully understand you idea about the echo statments!!.. Thanx for the tip! :)

 

i tried the code..and that NAME  variable change gave my array a different outpu!

 

Array

(

    [status] => Array

        (

            [1] => Array

                (

                    [0] => 1

                )

 

            [2] => Array

                (

                    [0] => 1

                )

 

            [3] => Array

                (

                    [0] => 0

                )

 

            [4] => Array

                (

                    [0] => 0

                )

 

            [5] => Array

                (

                    [0] => 1

                )

 

etc etc etc

 

ou can see jhow it changed there!

 

But the UPDATE code.. 

 

I am having a problem with the STATUS variable for the update code

 

i have tried everything. I am gettign  a headache hehe

 

if (isset($_POST['update'])){
  
foreach ($_POST['status'] as $tableid=>$status){
  
mysql_query(" UPDATE boxcolor SET status='status[]'");
  }
}

it is that staus - variable  thats killing me

 

 

 

 

Link to comment
Share on other sites

Try this:

 

<?php
echo "<td><INPUT type=\"radio\" value=\"0\" name=\"status[".$row['tableid']."]\" ";
if ($row['status'] == '0') echo "checked=\"checked\" ";
echo " >White";
?>

 

I removed the [$status] part. That was adding another layer of the array. Then you can do this:

 

<?php
if (isset($_POST['update'])){
  
foreach ($_POST['status'] as $tableid=>$status){
  
mysql_query(" UPDATE boxcolor SET status='status'");
  }
}
?>

(Notice the missing [] after $status, because the $status variable is now a regular variable, not an array.)

Link to comment
Share on other sites

FUnny.. thats how i had it before.. wiht the RADIO NAME....

 

 

just using the  simple $row['tableid'] before.. maybe that when it WAS working before... stupid me!!  :(

 

 

anyways the UPDATE code isnt working!! nothign is being updated to the  databse!

 

 

 

Link to comment
Share on other sites

when i hit the update button on the bottom ...the ARRAY printout shows the numbers being changed to 1 or 2  on what ever crate row i change.. but the  RADIO buttons themselfs STILL stay  on the  WHITE colour  (0) one!

 

hmm.. well i am tired now.. and i will try again tommorrow!..

Link to comment
Share on other sites

You first had your radio buttons named

name="status[$status]"

 

And, sorry, there's a "$" missing in your UPDATE.

 

<?php
if (isset($_POST['update'])){
  
foreach ($_POST['status'] as $tableid=>$status){
  
mysql_query("UPDATE boxcolor SET status = '$status' WHERE tableid = '$tableid'");
  }
}
?>

 

And you need a WHERE clause (I added one, but don't know if it's right for your table).

Link to comment
Share on other sites

Good Morning...

 

yeab  thats when i pasted it here..  when i first posted.. it wasnrt workign.. but i meant way before i camer here as i say it was working at one time.  but the once i couldnt get it to wsave the data..  i reddid the whole thing from scratch and it never worked right since.  Thats when i posted here!!  :o)

 

I will try at update fix now!  and get back!!

 

 

Link to comment
Share on other sites

Hi,

 

I just looked and found that that I only had $row_['tableid'] for the name variable thats all i had. IO guess thats why it gave ta DIFFERNT name for each  radio button set.  and allowed me to change them individually.  Thats why it ONLY SEEMED like it was working at the time.  But i remember why i didnt leave it that way, like i say the array wasnt doing anthing  for STATUS and i knew that the  radio Button NAME had to have  eb able to  know it was for STATUS... thats why  I removed that too!. and started that part from scratch too!..  Then I was just totally lost with the ARRAY as i never had to work with an array!

 

doode, your update code works.  The database is UPDATED.. perfectly.. NOw I just HAVE to  FULLY understand what you did so I would know what to do if i was to have to work with an ARRAY again! :o)

 

Do I mark this [solved]  or does the Moderator do that?

 

Cheerz and thanks again for your help!

 

 

 

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.