SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Member
    Join Date
    Nov 2007
    Location
    Manchester, UK
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Input Anchors Already Defined (while statment)

    Hey

    I'm brand new to these forums, hopefully you guys can give me a hand.

    Situation:
    I'm selecting a few 'rows' from a table called 'users' in my mySQL database. On this page there is a PHP while statement which selects key variables like 'username', 'user email' etc and prints them to a row on a table. The variables are printed into text boxes so they can be edited on each row. Then at the end of each row I have an 'apply' and 'delete' button, to apply changes or delete the row (user).

    This all works fine, apart from one major problem. The text boxes are all using the same ID

    Code HTML4Strict:
    <input type="text" id="username" value="$username" />

    So that when I edit any of the rows and hit apply, only the first row with the ID "username" is updated.

    I'm using JavaScript to pass the values of the text boxes etc to the URL when the apply button is clicked, where then PHP gets the info and updates the row on the database.

    Hope you can help me come up with a solution!

    Cheers

  2. #2
    SitePoint Guru Ize's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Simple; put the user's id after the id, so you get something like this:
    Code HTML4Strict:
    <input type="text" id="username1">
    <input type="text" id="username2">
    <input type="text" id="username3">
    <etc.>

  3. #3
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Why are you using javascript to do that? Why not just;
    Code html4strict:
    <input type="text" name="username[]" value="username1">
    <input type="text" name="username[]" value="username2">
    Then easily process $_POST['username'] as an array without making the site unusable in case of JS turned off (even if it's just the admin panel) and avoiding the work you do to acheive this in JS.

    Unless I'm missing something about what you're doing, of course.

  4. #4
    SitePoint Member
    Join Date
    Nov 2007
    Location
    Manchester, UK
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ize View Post
    Simple; put the user's id after the id, so you get something like this:
    Code HTML4Strict:
    <input type="text" id="username1">
    <input type="text" id="username2">
    <input type="text" id="username3">
    <etc.>
    Thanks for the quick reply.

    I've tried what you have said adding the variable $userid to the ID, but now my JavaScript code doesn't find any elements with the ID 'username'.

    Code JavaScript:
                    function apply() {
    		userid = document.getElementById('userid').value;
    		username = document.getElementById('username').value;
    		useremailaddress = document.getElementById('useremailaddress').value;
    		useradminlevel = document.getElementById('useradminlevel').value;
                    // pass the vars to PHP with window.location
    		}

    because there is now a number after each one..

  5. #5
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Use names instead of id's and document.yourformname.elements['username[]'] will give you an array of the elements.

  6. #6
    SitePoint Member
    Join Date
    Nov 2007
    Location
    Manchester, UK
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by php_daemon View Post
    Use names instead of id's and document.yourformname.elements['username[]'] will give you an array of the elements.
    Thanks. I understand the logic now. But I have a query, the data from the while statement is printed in rows on a table. To do the document.yourformname.elements['username[]'] I need to put the rows (<tr>'s) in forms, is this possible to do with Valid XHTML 1.0?

    Edit: do I just include the whole table in one form? I'll give it a try.

  7. #7
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    One form. Having input fields without a form is invalid XHTML, I believe.

  8. #8
    SitePoint Member
    Join Date
    Nov 2007
    Location
    Manchester, UK
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok so now I have my table wrapped in one form called users.

    I've updated the JavaScript from:
    Code JavaScript:
    	function apply() {
    		userid = document.getElementById('userid').value;
    		username = document.getElementById('username').value;
    		useremailaddress = document.getElementById('useremailaddress').value;
    		useradminlevel = document.getElementById('useradminlevel').value;
     
    		// pass the vars to PHP with window.location
    	}

    to

    Code JavaScript:
    	function apply() {
    		userid = document.users.elements['userid[]'].value;
    		username = document.users.elements['username[]'].value;
    		useremailaddress = document.users.elements['useremailaddress[]'].value;
    		useradminlevel = document.users.elements['useradminlevel[]'].value;	
     
    		// pass the vars to PHP with window.location
    	}

    and the text boxes etc look like this:
    Code PHP:
    			$query = mysql_query bla bla
    			while($data=mysql_fetch_array($query)) {
     
    				$userid = clean_str($data['userid']);
    				$username = clean_str($data['username']);
    				$useradminlevel = clean_str($data['useradminlevel']);
    				$useremailaddress = clean_str($data['useremailaddress']);
     
     
    				echo '
    					<tr>
    					<td><input type="text" name="userid'.$userid.'" value="'.$userid.'" readonly="true" style="width: 30px; border-width: 0px;" /></td>
    					<td>
    					<input type="text" name="username'.$userid.'" value="'.$username.'" style="width: 100px;" />
    					</td>
    					<td>
    					<input type="text" name="useremailaddress'.$userid.'" value="'.$useremailaddress.'" style="width: 170px;" />
    					</td>
    					<td align="center"><img src="../images/accept.png" border="0" alt="Update User" width="16" height="16" onclick="apply" /></td>
    					<td align="center"><img src="../images/delete.png" border="0" alt="Edit User" width="16" height="16" onclick="delete()" /></td>
    				  </tr>
    				';
    			}
     
    			echo '
    			</table>
    			</form>
    			<br /><br />
    			';

    Now when I hit the apply button.. FireBug says
    document.users.elements['userid[]'] has no properties

    Why's that?

  9. #9
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Oh, looks like I haven't mentioned that you should name all of them as username[] (as I posted in my first post in this thread).

  10. #10
    SitePoint Member
    Join Date
    Nov 2007
    Location
    Manchester, UK
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by php_daemon View Post
    Oh, looks like I haven't mentioned that you should name all of them as username[] (as I posted in my first post in this thread).
    Now it doesn't seem to pass the values of the text boxes etc to PHP in the URL the values are 'undefined'.

  11. #11
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    And what if you instead of building the url in JS simply submitted the form?

  12. #12
    SitePoint Member
    Join Date
    Nov 2007
    Location
    Manchester, UK
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is it possible to do 2 submit buttons one for Applying and one for Deleting the row?

  13. #13
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Yes. The value of the clicked button will be passed, which you can use to determine which action to take.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •