Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Apr 2007
    Posts
    85
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Confusion when passing variables through urls...

    I just created a script that allows me to update values in my database with the help of a friend. I understand everything perfectly except this first part of the update script, and a variable it utilizes, called ['id]. Here is the first part of the script...


    Code:
    if ((isset($_GET['id'])) && (is_numeric($_GET['id']))) { 
    	$id = $_GET['id']; 
    }
    Now I understand what it does, I just do not understand where this 'id' variable is coming from since I have not defined any id variable throughout my scripts on this project.

    My guess is it is coming through this part of my code:

    Code:
    <td><a href="edit_user.php?id=' .$row['user_id'] .'">Edit</a></td>
    So am I setting a variable here when I use the ?id= in the above code?


    So if I renamed this to:

    Code:
    <td><a href="edit_user.php?alternativevariable=' .$row['user_id'] .'">Edit</a></td>

    If I renamed it then my new variable would be ['alternativevariable'] ?


    Any guidance or help is greatly appreciated because I just do not understand where this ['id] variable is coming from.



    Thanks.


    Then my
    I am on my way. :)

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,509
    Thanks
    8
    Thanked 1,090 Times in 1,081 Posts
    Say your page has this:
    <td><a href="edit_user.php?alternativevariable=test123&action=edit">Edit</a></td>
    <td><a href="edit_user.php?alternativevariable=test123&action=delete">Delete</a></td>

    Your script has this:
    <?php
    $myid = $_GET['alternativevariable'];
    $action = $_GET['action'];

    if you echo the two:
    $myid would be: "test123"
    and $action would be: "delete" or "edit", depending on which link you clicked.

    You'll notice how the variable called "alternativevariable" is loaded
    into another variable called $myid once it gets into the script.
    You don't have to do that .. example $action is the same name.

    The other part, isset($_GET ..... , is testing to see if the variable has
    been set (if the variable is null ... no character, it's not set).

    I hope I don't confuse you ... you can look this stuff up on Google too.

  • #3
    New Coder
    Join Date
    Apr 2007
    Posts
    85
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mlseim View Post
    Say your page has this:
    <td><a href="edit_user.php?alternativevariable=test123&action=edit">Edit</a></td>
    <td><a href="edit_user.php?alternativevariable=test123&action=delete">Delete</a></td>

    Your script has this:
    <?php
    $myid = $_GET['alternativevariable'];
    $action = $_GET['action'];

    if you echo the two:
    $myid would be: "test123"
    and $action would be: "delete" or "edit", depending on which link you clicked.

    You'll notice how the variable called "alternativevariable" is loaded
    into another variable called $myid once it gets into the script.
    You don't have to do that .. example $action is the same name.

    The other part, isset($_GET ..... , is testing to see if the variable has
    been set (if the variable is null ... no character, it's not set).

    I hope I don't confuse you ... you can look this stuff up on Google too.


    Thank you for taking the time to right that. Now I am 99% sure I understand what you are saying.

    You are saying that you can se the variable name after whatever you want as long as you set the href link to use an '=' sign. So if I wanted to add another variable I would set it like this...

    Code:
    <td><a href="edit_user.php?alternativevariable=test123&action=edit&thirdvariable=echothis">Edit</a></td>
    $third = $_GET['thirdvariable]


    And if I echoed $third to my screen, the output would be "echothis"?


    Thanks again.
    I am on my way. :)

  • #4
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    Yes

    You can append unlimited variables to the URL

    http://www.mysite.com?var=a&var2=b&var3=c&var4=etc

    HTH
    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #5
    New Coder
    Join Date
    Apr 2007
    Posts
    85
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by whizard View Post
    Yes

    You can append unlimited variables to the URL

    http://www.mysite.com?var=a&var2=b&var3=c&var4=etc

    HTH
    Dan

    Thanks, Dan.
    I am on my way. :)


  •  

    Posting Permissions

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