943,027 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 59
  • PHP RSS
Jul 24th, 2010
0

PHP help

Expand Post »
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..
PHP Syntax (Toggle Plain Text)
  1.  
  2. $userinfo = file("list.txt");
  3. echo '<table border="1" cellspacing="2" align="center">';
  4. foreach($userinfo as $key => $val)
  5. {
  6. $data[$key] = explode("||", $val);
  7. }
  8. $bg = '#33CCFF';
  9. for($k = 0; $k < sizeof($userinfo); $k++)
  10. {
  11. 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>';
  12. echo '<tr><td colspan=2> </td></tr>';
  13. }
  14. echo '</table>';
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mcld is offline Offline
12 posts
since Jul 2010
Jul 24th, 2010
0
Re: PHP help
THis in your loop

PHP Syntax (Toggle Plain Text)
  1. 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).

PHP Syntax (Toggle Plain Text)
  1. document.forms['myform'].elements['confirm'].addEventListener('click',do-your-function-name-here,false)
Last edited by ardav; Jul 24th, 2010 at 7:53 pm.
Sponsor
Featured Poster
Reputation Points: 1036
Solved Threads: 934
Sarcastic Poster
ardav is offline Offline
6,614 posts
since Oct 2006
Jul 25th, 2010
0
Re: PHP help
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mcld is offline Offline
12 posts
since Jul 2010
Jul 25th, 2010
0
Re: PHP help
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.

PHP Syntax (Toggle Plain Text)
  1. document.forms['myform'].elements['mytextbox'].value = document.forms['myform'].elements['mytextbox'].value + ", " + passedVariable;

Check this, my js isn't too hot.
Sponsor
Featured Poster
Reputation Points: 1036
Solved Threads: 934
Sarcastic Poster
ardav is offline Offline
6,614 posts
since Oct 2006
Jul 26th, 2010
0
Re: PHP help
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
PHP Syntax (Toggle Plain Text)
  1. <script>
  2. function copy_data(val){
  3. var a = document.getElementById(val.id).value
  4. document.getElementById("copy_to").value=a
  5. }
  6.  
  7. </script>
I can get soomething close to what i want, but how do I put in the php code.
thanks once more..
PHP Syntax (Toggle Plain Text)
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  2. <html>
  3. <head>
  4.  
  5. <script>
  6. function copyVal(){
  7. document.forms['myform'].elements['confirm[$k]'].addEventListener('click',copyVal,false)
  8. document.forms['myform'].elements['mytextbox'].value = document.forms['myform'].elements['confirm[$k]'].value + ", " + passedVariable;
  9. }
  10.  
  11. </script>
  12. </head>
  13.  
  14. <body>
  15. <form name="myform">
  16. <?php
  17. $userinfo = file("sometext_file");
  18. echo '<table border="1" cellspacing="0" cellpadding="0" align="center" width="25%>"';
  19. foreach($userinfo as $key => $val)
  20. {
  21. $data[$key] = explode("||", $val);
  22. }
  23. $bg = '#ffffff';
  24. echo '<tr><td colspan="3" align="center"><input type="text" name="mytextbox" size="50" >';
  25. for($k = 0; $k < sizeof($userinfo); $k++)
  26. {
  27. 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>';
  28. echo '<tr><td colspan=2> </td></tr>';
  29. echo '</td></tr>';
  30. }
  31. echo '</table>';
  32.  
  33. ?>
  34. </form>
  35. </body>
  36. </html>
Last edited by mcld; Jul 26th, 2010 at 10:35 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mcld is offline Offline
12 posts
since Jul 2010
Jul 26th, 2010
0
Re: PHP help
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.

PHP Syntax (Toggle Plain Text)
  1. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
  2. <script>
  3. $(document).ready(function() {
  4. $( "input[name='check[]']" ).bind( "click", clickCheck )
  5. function clickCheck(){
  6. var str = $("#txt").val();
  7. var newStr = $(this).val();
  8. if($(this).is(':checked')){
  9. $("#txt").val(str + newStr + ",");
  10. }else{
  11. $("#txt").val(str.replace(newStr + ',', ''));
  12. }
  13. }
  14. });
  15. </script>
  16. </head>
  17. <body>
  18. <form name="myform" id="myform">
  19. <input type="checkbox" value="a1@fictic.io.us" name="check[]" id="check1" /><label for="check1">a1@fictic.io.us</label><br />
  20. <input type="checkbox" value="a2@fictic.io.us" name="check[]" id="check2" /><label for="check2">a2@fictic.io.us</label><br />
  21. <input type="checkbox" value="a3@fictic.io.us" name="check[]" id="check3" /><label for="check3">a3@fictic.io.us</label><br />
  22. <input type="checkbox" value="a4@fictic.io.us" name="check[]" id="check4" /><label for="check4">a4@fictic.io.us</label><br />
  23. <input type="text" value="" name="txt" id="txt" /><br />
  24. </form>
  25. </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.
Sponsor
Featured Poster
Reputation Points: 1036
Solved Threads: 934
Sarcastic Poster
ardav is offline Offline
6,614 posts
since Oct 2006
Jul 26th, 2010
0
Re: PHP help
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mcld is offline Offline
12 posts
since Jul 2010
Jul 26th, 2010
0
Re: PHP help
php to read from the file.
jquery to deal with the add/subtract.


just change to this:

PHP Syntax (Toggle Plain Text)
  1. <form name="myform">
  2. <table border="1" cellspacing="0" cellpadding="0" align="center" width="25%">
  3. <tr><td align="center"><input type="text" name="txt" size="50" ></td></tr>
  4. <?php
  5. $userinfo = file("sometext_file");
  6. foreach($userinfo as $key => $val){
  7. $data[$key] = explode("||", $val);
  8. }
  9. for($k = 0; $k < sizeof($userinfo); $k++){
  10. ?>
  11. <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>
  12. <?php
  13. }
  14. ?>
  15. </table>
  16. </form>
Sponsor
Featured Poster
Reputation Points: 1036
Solved Threads: 934
Sarcastic Poster
ardav is offline Offline
6,614 posts
since Oct 2006
Jul 26th, 2010
0
Re: PHP help
ardav Supper Hero
You are my King, Thanks bro.. was banging my head....
Last edited by mcld; Jul 26th, 2010 at 6:55 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mcld is offline Offline
12 posts
since Jul 2010
Jul 26th, 2010
0
Re: PHP help
OK if it works, mark this thread solved.
Sponsor
Featured Poster
Reputation Points: 1036
Solved Threads: 934
Sarcastic Poster
ardav is offline Offline
6,614 posts
since Oct 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Store pdf content to mysql
Next Thread in PHP Forum Timeline: upload picture





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC