954,174 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

PHP help

hi guys, I've been following the tuts and help on this incredible site for a while and must say that you guys are really amazing and very helpful.
I am learning PHP since it's very useful for site development etc.
I will appreciate any help form you guys on a little project I'm trying my hands on.
I have a flat file data base that i read email addresses from into data table(TD) with check boxes attached to each data.
Is there a way that when a check box is checked it automatically adds that data separated by coma(;) in to a text box one at a time? and if it's unchecked it removes it from the text box.
Thanks for any assistance.
my php code for retrieving data looks like this..

$userinfo = file("list.txt");
echo '<table border="1" cellspacing="2" align="center">';
foreach($userinfo as $key => $val) 
{
   $data[$key] = explode("||", $val);
}
$bg = '#33CCFF';
for($k = 0; $k < sizeof($userinfo); $k++) 
{ 
echo '"<tr bgcolor=\"#"'. $bg .'"\"><td>'.$k.'</td><td>'.$data[$k][0].'<input type="hidden" name="email" value="'.$data[$k][0].'"></td><td><input type="checkbox" name ="confirm"></td></tr>';
echo '<tr><td colspan=2> </td></tr>';
}
echo '</table>';
mcld
Newbie Poster
12 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

THis in your loop

echo "<input type=\"checkbox\" name =\"confirm[$k]\" value=\"{$data[$k][0]}\" />";


Set up js listener to check for 'confirm' check events. This will append the value of the checkbox to the textbox text. E.g. for all browsers other than IE (need attachEvent for that).

document.forms['myform'].elements['confirm'].addEventListener('click',do-your-function-name-here,false)
diafol
Rhod Gilbert Fan (ardav)
Moderator
7,765 posts since Oct 2006
Reputation Points: 1,168
Solved Threads: 1,075
 

thanks ardav,
your code did add a check box quite ok, but what I was hoping to do is have a blank text box bellow, that will append if the any one in the table TD is checked.
Pls bear with me, I hope I'm not asking for too much, since I'm still learning. about the js can you give me a pointer? thanks again.

mcld
Newbie Poster
12 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

I understood your post, I just didn't want to do all the work for you. You can add the 'value' (e-mail address) of the checked checkbox to the textbox.

document.forms['myform'].elements['mytextbox'].value = document.forms['myform'].elements['mytextbox'].value + ", " + passedVariable;


Check this, my js isn't too hot.

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,765 posts since Oct 2006
Reputation Points: 1,168
Solved Threads: 1,075
 

Pls help,
Thank you, I'm very sure you know what I want to get at. But you want me to learn, thanks.. after trying to implement your code tis is what i have so far..
What I willl want to achieve is whem a check box is clicked the value corresponding to that checkbox +":" displayed in the textbox above, and when a checkbox is unchecked that value removed from the list. pls pls any help is well appretiated. thanks..
my code
I know with ja like

<script>
    function copy_data(val){
     var a = document.getElementById(val.id).value
     document.getElementById("copy_to").value=a
    }

 </script>

I can get soomething close to what i want, but how do I put in the php code.
thanks once more..

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>

<script>
    function copyVal(){
	document.forms['myform'].elements['confirm[$k]'].addEventListener('click',copyVal,false)
	document.forms['myform'].elements['mytextbox'].value = document.forms['myform'].elements['confirm[$k]'].value + ", " + passedVariable; 
    }

</script>
</head>

<body>
<form name="myform">
<?php
$userinfo = file("sometext_file");
  echo '<table border="1" cellspacing="0" cellpadding="0" align="center" width="25%>"';
foreach($userinfo as $key => $val) 
{
   $data[$key] = explode("||", $val);
}
$bg = '#ffffff'; 
  echo '<tr><td colspan="3" align="center"><input type="text" name="mytextbox" size="50" >';
for($k = 0; $k < sizeof($userinfo); $k++) 
{ 
  echo '<tr bgcolor=\"#"'. $bg .'"\"><td>'.$k.'</td><td>'.$data[$k][0].'<input type="hidden" name="email" value='.$data[$k][0].'></td><td><input type="checkbox" name ="confirm[$k]" value="[$data[$k][0]]  onclick="copyVal(this)" ></td></tr>';
  echo '<tr><td colspan=2> </td></tr>';
  echo '</td></tr>';
}
  echo '</table>';

?>
</form>
</body>
</html>
mcld
Newbie Poster
12 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

OK, seeing as you had a good go yourself. I'm using the jQuery library here, so if you copy this exactly, it should work in all browsers.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script>
$(document).ready(function() {
	$( "input[name='check[]']" ).bind( "click", clickCheck )
	function clickCheck(){
		var str = $("#txt").val();
		var newStr = $(this).val();
		if($(this).is(':checked')){
			$("#txt").val(str + newStr + ",");
		}else{
			$("#txt").val(str.replace(newStr + ',', ''));
		}	  
	}
});
</script>
</head>
<body>
<form name="myform" id="myform">
	<input type="checkbox" value="a1@fictic.io.us" name="check[]" id="check1" /><label for="check1">a1@fictic.io.us</label>
	<input type="checkbox" value="a2@fictic.io.us" name="check[]" id="check2" /><label for="check2">a2@fictic.io.us</label>
	<input type="checkbox" value="a3@fictic.io.us" name="check[]" id="check3" /><label for="check3">a3@fictic.io.us</label>
	<input type="checkbox" value="a4@fictic.io.us" name="check[]" id="check4" /><label for="check4">a4@fictic.io.us</label>
	<input type="text" value="" name="txt" id="txt" />
</form>
</body>


It's slightly different from my previous post. I found that applying an email_id to the name attribute of the checkbox was unnecessary.

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,765 posts since Oct 2006
Reputation Points: 1,168
Solved Threads: 1,075
 

ardav You are the best,
Like you said its different.. but that work like a charm. I'm very happy with it, only I have to Find a way to make it read dynamically from my txt file. instead of predefining the input Array. I hope I can use jquery with php.
Thanks so much

mcld
Newbie Poster
12 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

php to read from the file.
jquery to deal with the add/subtract.


just change to this:

<form name="myform">
<table border="1" cellspacing="0" cellpadding="0" align="center" width="25%">
<tr><td align="center"><input type="text" name="txt" size="50" ></td></tr>
<?php
$userinfo = file("sometext_file");
foreach($userinfo as $key => $val){
   $data[$key] = explode("||", $val);
}
for($k = 0; $k < sizeof($userinfo); $k++){ 
?>
<tr bgcolor="#ffffff"><td><input type="checkbox" id="check_<?php echo $k;?>" name ="check[]" value="<?php echo $data[$k][0];?>" /><label for="check_<?php echo $k;?>"><?php echo $data[$k][0];?></label></td></tr>
<?php
}
?> 
</table>
</form>
diafol
Rhod Gilbert Fan (ardav)
Moderator
7,765 posts since Oct 2006
Reputation Points: 1,168
Solved Threads: 1,075
 

ardav Supper Hero
You are my King, Thanks bro.. was banging my head....

mcld
Newbie Poster
12 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

OK if it works, mark this thread solved.

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,765 posts since Oct 2006
Reputation Points: 1,168
Solved Threads: 1,075
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You