467,134 Members | 1,049 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,134 developers. It's quick & easy.

Send Dynamic form Values to PHP

ak1dnar
Expert 1GB
Hi, I have created a web application that enables to Delete Items in a product table using check boxes.
I Need to Display each and every product in <TR> and with the product there is a corresponding check box to delete it, use can select the check box and then he will press the Update Button in the Form to submit for processing.

[PHP]
//Here I will get the Values from MySQL Table
//This Area is Inside a HTML form with Update button and On Submit it will pass //to PHP page for precessing.

while($row = mysql_fetch_assoc($result))
{
echo '<tr>
<td>'.$row['p_name'].'</td><td>
<input type="checkbox" name="pid" value="'.$row['p_id'].'" /><td>
</tr>';
}
[/PHP]

But the problem is here in my coding as the checkbox name I am giving the name as pid.
Since i have put it inside while loop for 5 products there will be 5 check boxes.
Then name will create duplicates.

So then I cant get this Value from PHP side. Only one of the check box value will come to PHP page.
So How do i send all the selected check box values when user Submit the Form.
Mar 7 '07 #1
  • viewed: 3466
Share:
18 Replies
gauravgmbhr
100+
Hi, I have created a web application that enables to Delete Items in a product table using check boxes.
I Need to Display each and every product in <TR> and with the product there is a corresponding check box to delete it, use can select the check box and then he will press the Update Button in the Form to submit for processing.

[PHP]
//Here I will get the Values from MySQL Table
//This Area is Inside a HTML form with Update button and On Submit it will pass //to PHP page for precessing.

while($row = mysql_fetch_assoc($result))
{
echo '<tr>
<td>'.$row['p_name'].'</td><td>
<input type="checkbox" name="pid" value="'.$row['p_id'].'" /><td>
</tr>';
}
[/PHP]

But the problem is here in my coding as the checkbox name I am giving the name as pid.
Since i have put it inside while loop for 5 products there will be 5 check boxes.
Then name will create duplicates.

So then I cant get this Value from PHP side. Only one of the check box value will come to PHP page.
So How do i send all the selected check box values when user Submit the Form.

i had the very same problem

the solution is very simple
dont use the name="pid"

but use
Expand|Select|Wrap|Line Numbers
  1.  name="pid[]"

now when u recieve the value in PHP
then
var_dump($_GET[pid]) will b an array that will contain all ur selected values
Mar 7 '07 #2
gauravgmbhr
100+
i had the very same problem

the solution is very simple
dont use the name="pid"

but use
Expand|Select|Wrap|Line Numbers
  1.  name="pid[]"

now when u recieve the value in PHP
then
var_dump($_GET[pid]) will b an array that will contain all ur selected values



the other solition is u can have a $i varible
and name="pid{$i}";
$i++;


so each check box will have names as
pid1
pid2..........


but that will make ur destination script complicated
so try using the first idea
which is more reliable
and employed and tested by me
Mar 7 '07 #3
ak1dnar
Expert 1GB
Thanks I made it but now there is a small bug.
to submit the Form I am using a image and onclick of the image i will call for the JS function . then JS will Submit the form.


[HTML]
<!--This is in side the form, it will make multiple check boxes according to the result -->

<input type="checkbox" name="frm_chk_delete[]" value="'.$id.'" />
<!--More Comes here -->

<!-- My Submit image -->
<a href="javascript:update()"><img src="images_in/b_update.jpg" alt="Update" width="64" height="27" border="0" /></a>[/HTML]

JS function
Expand|Select|Wrap|Line Numbers
  1. function update(){
  2.     document.inq_form.action="update.php";
  3.     document.inq_form.submit();
  4.     }
  5.  
Problem is if i am submitting form to update.php with out selecting a checkbox from the list Error will come.

Expand|Select|Wrap|Line Numbers
  1. Warning: implode() [function.implode]: Bad arguments. in D:\....\update.php on line 4
update.php
[PHP]
require 'dbcon.php';//MySQL connectivity
$frm_chk_delete = $_REQUEST['frm_chk_delete'];
$list_of_ids = implode(",", $frm_chk_delete);
mysql_query("DELETE FROM cart WHERE p_id IN ($list_of_ids)");
header('Location:previouspage.php');
[/PHP]

How to Avoid This error...for that I have to check whether user has selected a check box or Not.
Mar 8 '07 #4
gauravgmbhr
100+
Thanks I made it but now there is a small bug.
to submit the Form I am using a image and onclick of the image i will call for the JS function . then JS will Submit the form.


[HTML]
<!--This is in side the form, it will make multiple check boxes according to the result -->

<input type="checkbox" name="frm_chk_delete[]" value="'.$id.'" />
<!--More Comes here -->

<!-- My Submit image -->
<a href="javascript:update()"><img src="images_in/b_update.jpg" alt="Update" width="64" height="27" border="0" /></a>[/HTML]

JS function
Expand|Select|Wrap|Line Numbers
  1. function update(){
  2.     document.inq_form.action="update.php";
  3.     document.inq_form.submit();
  4.     }
  5.  
Problem is if i am submitting form to update.php with out selecting a checkbox from the list Error will come.

Expand|Select|Wrap|Line Numbers
  1. Warning: implode() [function.implode]: Bad arguments. in D:\....\update.php on line 4
update.php
[PHP]
require 'dbcon.php';//MySQL connectivity
$frm_chk_delete = $_REQUEST['frm_chk_delete'];
$list_of_ids = implode(",", $frm_chk_delete);
mysql_query("DELETE FROM cart WHERE p_id IN ($list_of_ids)");
header('Location:previouspage.php');
[/PHP]

How to Avoid This error...for that I have to check whether user has selected a check box or Not.


Try Using Var_dump on ur $_REQUEST['frm_chk_delete'] In order to check whether its an array or Not


i Guess it will be an array with all values = '.$id.' yes '.$id.' not the values u want to send

so when u implode it then $id will be treated as a php variable wich is blank
thatz y the warning is comin

so dont use
<input type="checkbox" name="frm_chk_delete[]" value="'.$id.'" />


But use

Expand|Select|Wrap|Line Numbers
  1. <input type="checkbox" name="frm_chk_delete[]" value="<?php echo $id ?>" />

the reason for using echo $id is that outside the PHP island $id is a string ='$id' not the value it contain


PHP send plain HTML code to the browser
so PHP is used to write the HTML code
USE the <?php echo $id?> instead od '$id' directly.
M sure ur script will work
Mar 8 '07 #5
gauravgmbhr
100+
And One more thing
Try handling the errors so that the warning does not appear when user do not select any of the checkboxes

TRY

if(sizeof($frm_chk_delete)==0) {
echo "atleast one of the check boxes";
header('location: previospage.php');
}
else
{
implode(bla bla bla);
}


errors should be handle because implode will produce warnind not an error
so the script wont stop

and the SQL statement will be executed
and produce a database error

u dont wanna tell anyone bout ue database schema


if u wanna study how to handle SQL statement in PHP
read this
Mar 8 '07 #6
ak1dnar
Expert 1GB
Try Using Var_dump on ur $_REQUEST['frm_chk_delete'] In order to check whether its an array or Not


i Guess it will be an array with all values = '.$id.' yes '.$id.' not the values u want to send

so when u implode it then $id will be treated as a php variable wich is blank
thatz y the warning is comin

so dont use
<input type="checkbox" name="frm_chk_delete[]" value="'.$id.'" />


But use

Expand|Select|Wrap|Line Numbers
  1. <input type="checkbox" name="frm_chk_delete[]" value="<?php echo $id ?>" />

the reason for using echo $id is that outside the PHP island $id is a string ='$id' not the value it contain


PHP send plain HTML code to the browser
so PHP is used to write the HTML code
USE the <?php echo $id?> instead od '$id' directly.
M sure ur script will work
My HTML coding will Print dynamically from PHP. You have misunderstood my Problem. this $pid will always Give the value to the check box (ex: 1001,1002) if there is a checkbox and that i can pass to the PHP page with values successfully.It will Never print as $pid in value attribute.

[PHP]echo '<input type="checkbox" name="frm_chk_delete[]" value="'.$id.'" />';[/PHP]

The problem is here with out selecting a check box if i submit this form that implode function will get this as a Bad Argument. because Here at the array NO values.
Because i did not select a check box.
Mar 8 '07 #7
gauravgmbhr
100+
My HTML coding will Print dynamically from PHP. You have misunderstood my Problem. this $pid will always Give the value to the check box (ex: 1001,1002) if there is a checkbox and that i can pass to the PHP page with values successfully.It will Never print as $pid in value attribute.

[PHP]echo '<input type="checkbox" name="frm_chk_delete[]" value="'.$id.'" />';[/PHP]

The problem is here with out selecting a check box if i submit this form that implode function will get this as a Bad Argument. because Here at the array NO values.
Because i did not select a check box.

C Now ur are Putting all the <input............................... in the PHP echo
u didnt do it in first time

If u Put the statement Inside PHP echo LIke u did now
[PHP]echo '<input type="checkbox" name="frm_chk_delete[]" value="'.$id.'" />';[/PHP]

this will work for sure
Mar 8 '07 #8
gauravgmbhr
100+
still U have an error In ur HTML script


[PHP]echo '<input type="checkbox" name="frm_chk_delete[]" value="'.$id.'" />';[/PHP]



u r using value=" ' .$id. ' " but u should use

value = ' " .$id. " '

c the diff in quotes


READ THE FINAL HTML SCRIPT AFTER THE PAGE LOAD
BY USING view source option
Then U will C that U r making mistake In HTML, so the values Are Not being passed correcly


Or Use var_dump($_REQUEST['frm_chk_delete']) U will C that its not recieving ur desired values
Mar 8 '07 #9
ak1dnar
Expert 1GB
still U have an error In ur HTML script
u r using value=" ' .$id. ' " but u should use

value = ' " .$id. " '

c the diff in quotes


READ THE FINAL HTML SCRIPT AFTER THE PAGE LOAD
BY USING view source option
Then U will C that U r making mistake In HTML, so the values Are Not being passed correcly


Or Use var_dump($_REQUEST['frm_chk_delete']) U will C that its not recieving ur desired values

Read this http://www.thescripts.com/forum/thread1707.html
I made my Script to work, based on this POST.

To delete the values i can use this PHP script.
[PHP]echo '<input type="checkbox" name="frm_chk_delete[]" value="'.$id.'" />';[/PHP]

And No need to Pass Like this as you say :)
[PHP]echo '<input type="checkbox" name="frm_chk_delete[]" value = ' " .$id. " ' />';[/PHP]

and Any one can understand why peoples are using value="'.$id.'"
with their coding except newbies. If some one can Get a Value to PHP variable again he can pass it in this way :
[PHP]
$id = "The value Goes Here";
echo '<input type="checkbox" name="frm_chk_delete[]" value="'.$id.'" />';[/PHP]

Thanks :)
Mar 8 '07 #10
gauravgmbhr
100+
Read this http://www.thescripts.com/forum/thread1707.html
I made my Script to work, based on this POST.

To delete the values i can use this PHP script.
[PHP]echo '<input type="checkbox" name="frm_chk_delete[]" value="'.$id.'" />';[/PHP]

And No need to Pass Like this as you say :)
[PHP]echo '<input type="checkbox" name="frm_chk_delete[]" value = ' " .$id. " ' />';[/PHP]

and Any one can understand why peoples are using value="'.$id.'"
with their coding except newbies. If some one can Get a Value to PHP variable again he can pass it in this way :
[PHP]
$id = "The value Goes Here";
echo '<input type="checkbox" name="frm_chk_delete[]" value="'.$id.'" />';[/PHP]

Thanks :)


SORRY MY MISTAKE
I DIDNT NOTICE THA U R USING SINGLE QUOTES TO PRINT THE VALUE
AND TOLD U TO USE value=' " .$id. "' coz i thought u r using double qotes to print ur value and sigle quotes to quote the id values

ur method is correct
Mar 8 '07 #11
gauravgmbhr
100+
and i put spaces between the double and single quotes so that it will be distinguishable here On the site for diplay, not that its used that way
Mar 8 '07 #12
gauravgmbhr
100+
i personally use double quotes for echoing HTML statement con then i dont need to exit and re-enter the quotes like value=" ' .$id. ' "

if u use double quotes the u can directly use


echo "<input type=\"checkbox\" name=\"whatever\" value=\"$id\">"
and so on
Mar 8 '07 #13
ak1dnar
Expert 1GB
Thanks for that bro. I combined your Error trapping system to my Script.Now the Script is Ready to Fly.

[PHP]require 'dbcon.php';
$frm_chk_delete = $_REQUEST['frm_chk_delete'];
if(sizeof($frm_chk_delete) == 0){
echo 'Please Select check boxes';
/*
I'll Do something Better Here to send the Error Back to
the_page_I_came_from.php
*/
}else{
$list_of_ids = implode(",", $frm_chk_delete);
mysql_query("DELETE FROM MyTable WHERE p_id IN ($list_of_ids)");
header('Location: the_page_I_came_from.php');
}[/PHP]

Wish to c u again :)
Mar 8 '07 #14
gauravgmbhr
100+
Any time Brother
not a problem
Mar 8 '07 #15
ak1dnar
Expert 1GB
Hi I have to Open this thread again.
see here in my dynamic form again i have to put a <textarea> to pass the Inquiries to database.Along with a Hidden value field(ID).
The ID will always send to php page since it is hidden and always there is value.
But if user doesn't type a Value to textarea(S) array of comma will pass.
I need find out a way to give a default value to text area if user does not type a Value.
Again I need to update the MySQL table with new Inquiries values. Thats why i am getting this ID to the hidden fields.

[PHP]
/*This is in side while loop and will repeat*/
echo '<input type="hidden" name="frm_id[]" value="'.$id.'"/>
<textarea name="frm_inq[]" cols="65" rows="3" ></textarea>';
[/PHP]

[PHP]<?php
$inq = $_REQUEST['frm_inq'];
$ids = $_REQUEST['frm_id'];

$list_of_ids = implode(",", $ids);
$list_of_inq = implode(",", $inq);

echo $list_of_ids;
echo '<BR>';
echo $list_of_inq;
// I need to Update mySQL table Here For Now I'll will Print it
?>[/PHP]

How to update mytable from these values
sample Output

Expand|Select|Wrap|Line Numbers
  1. 1001,1002
  2. I need to pass this is for 1001 Record ,this is for 1002
Any Idea About this?
Mar 8 '07 #16
gauravgmbhr
100+
Hi I have to Open this thread again.
see here in my dynamic form again i have to put a <textarea> to pass the Inquiries to database.Along with a Hidden value field(ID).
The ID will always send to php page since it is hidden and always there is value.
But if user doesn't type a Value to textarea(S) array of comma will pass.
I need find out a way to give a default value to text area if user does not type a Value.
Again I need to update the MySQL table with new Inquiries values. Thats why i am getting this ID to the hidden fields.

[PHP]
/*This is in side while loop and will repeat*/
echo '<input type="hidden" name="frm_id[]" value="'.$id.'"/>
<textarea name="frm_inq[]" cols="65" rows="3" ></textarea>';
[/PHP]

[PHP]<?php
$inq = $_REQUEST['frm_inq'];
$ids = $_REQUEST['frm_id'];

$list_of_ids = implode(",", $ids);
$list_of_inq = implode(",", $inq);

echo $list_of_ids;
echo '<BR>';
echo $list_of_inq;
// I need to Update mySQL table Here For Now I'll will Print it
?>[/PHP]

How to update mytable from these values
sample Output

Expand|Select|Wrap|Line Numbers
  1. 1001,1002
  2. I need to pass this is for 1001 Record ,this is for 1002
Any Idea About this?

its simple


use
value attribute in text area like

Expand|Select|Wrap|Line Numbers
  1. <textarea name="something" value="default text">
Mar 8 '07 #17
ak1dnar
Expert 1GB
its simple


use
value attribute in text area like

Expand|Select|Wrap|Line Numbers
  1. <textarea name="something" value="default text">
I think <textarea> is not using this value attribute like other HTML form elements.
Please refer to this http://www.thescripts.com/forum/thread602808.html .
[HTML]<textarea>PHP will get Contents from Here</textarea>[/HTML]

I think for the PHP side i have to do some thing for this.
How ever after getting that Values how to pass this to MySQL.
Because its a Gettng as two arrays.
Mar 8 '07 #18
gauravgmbhr
100+
WELL RIGHT
THE VALUE ATTRIBUTE IS APPLICABLE IN <input type="text">
NOT IN TEXT AREA
Mar 8 '07 #19

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Nathan Bloomfield | last post: by
2 posts views Thread by Fatih BOY | last post: by
2 posts views Thread by deejayquai@gmail.com | last post: by
4 posts views Thread by =?Utf-8?B?RHlsYW5TbWl0aA==?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.