Sending Values to a Script Manually


In the examples so far, all of the data received in the PHP script came from what the user entered in a form. There are, however, two different ways you can pass variables and values to a PHP script, both worth knowing.

The first method is to make use of HTML's hidden input type:

 <input type="hidden" name="name"   value="Brian /> 

As long as this code is anywhere between the form tags, the variable $_POST['name'] will have a value of Brian in the handling PHP script (assuming that the form uses the POST method). This technique will be demonstrated shortly.

The second method is to append a value to the PHP script's URL:

www.dmcinsights.com/page.php?name=Brian

This technique emulates the GET method of an HTML form. With this specific example, page.php receives a variable called $_GET['name'] with a value of Brian.

To demonstrate this GET method trick, a new version of the view_users.php script will be written. This one will provide links to edit or delete an existing user. The links will pass the user's ID to the handling pages, both of which will be written subsequently.

To manually send values to a PHP script

1.

Open view_users.php (Script 7.6) in your text editor or IDE.

2.

Change the SQL query to read (Script 8.2).

 $query = "SELECT last_name,   first_name, DATE_FORMAT  (registration_date, '%M %d, %Y)   AS dr, user_id FROM users ORDER BY   registration_date ASC;  

Script 8.2. The view_users.php script has been modified so that it presents Edit and Delete links, passing the user's ID number along in each URL.


I have changed this query in a couple of ways. First, I select the first and last names as separate values, instead of as one concatenated value. Second, I now also select the user_id value, which will be necessary in creating the links.

3.

Add three more columns to the main table.

 echo '<table align="center"   cellspacing="0 cellpadding="5"> <tr>   <td align="left"><b>Edit</b></td>   <td align="left"><b>Delete</b>    </td>   <td align="left"><b>Last Name</b>    </td>   <td align="left"><b>First Name</b>   </td>   <td align="left"><b>Date     Registered</b></td> </tr> '; 

In the previous version of the script, there were only two columns: one for the name and another for the date the user registered. I've separated out the name column into its two parts and created one column for the Edit link and another for the Delete link.

4.

Change the echo statement within the while loop to match the table's new structure.

 echo '<tr>   <td align="left"><a href="edit_    user.php?id=' . $row['user_id]     . '">Edit</a></td>   <td align="left"><a href="delete_    user.php?id=' . $row['user_id]     . '">Delete</a></td>   <td align="left">' . $row['last_    name] . '</td>   <td align="left">' . $row['first_    name] . '</td>   <td align="left">' . $row['dr']     . '</td> </tr> '; 

For each record returned from the database, this line will print out a row with five columns. The last three columns are obvious and easy to create: just refer to the returned column name.

For the first two columns, which provide links to edit or delete the user, the syntax is slightly more complicated. The desired end result is HTML code like <a href="edit_user.php?id=X">Edit</a>, where X is the user's ID. Having established this, all I have to do is print $row['user_id'] for X, being mindful of the quotation marks to avoid parse errors.

5.

Save the file as view_users.php, upload it to your server, and run in your Web browser (Figure 8.1).

Figure 8.1. The latest version of the view_users.php page, with new columns and links.


6.

If you want, view the HTML source of the page to see each dynamically generated link (Figure 8.2).

Figure 8.2. The source of the page (see Figure 8.1) shows how the user's ID is added to each link's URL.


Tips

  • To append multiple variables to a URL, use this syntax: page.php?name1=value1&name2=value2&name3=value3. It's simply a matter of using the ampersand, plus another name=value pair.

  • One trick to adding variables to URLs is that strings should be encoded to ensure that the value is handled properly. For example, the space in the string Elliott Smith would be problematic. The solution then is to use the urlencode() function:

     $url = 'page.php?name=' .   urlencode('Elliott Smith); 

    You only need to do this when manually adding values to a URL. When a form uses a GET method, it automatically encodes the data properly.




    PHP and MySQL for Dynamic Web Sites. Visual QuickPro Guide
    PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (2nd Edition)
    ISBN: 0321336577
    EAN: 2147483647
    Year: 2005
    Pages: 166
    Authors: Larry Ullman

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net