Jump to content

Using html buttons to call php scripts.


wpb

Recommended Posts

This is probably more of an HTML question than a PHP one, but as I encountered it in a PHP context, I thought someone here might be able to offer advice...

 

I have a MySQL database with records that each have a unique ID number. I use PHP to create a table listing all the records in the database, and I'd like to have an "Edit" and "Delete" button alongside each entry in the table.

 

Rather than just using a link like this:

 

<a href="editentry.php?id=1">Edit</a>

<a href="editentry.php?id=2">Edit</a>

<a href="editentry.php?id=3">Edit</a>

etc.

 

I was hoping to use an HTML button, as they stand out a bit more. As in:

 

<a href="editentry.php?id=1"><button>Edit</button></a>

etc.

 

But I don't seem to be able to make a button into a link. Is this a genuine limitation?

 

I know I could get this to work if I used forms, but I'd have to use a form for every single button in the table, as the ID numbers vary for each table row.

 

I don't want to use Javascript's onclick handler, and I'd rather not create my own image that looks like a button. Any other ideas?

 

Many thanks,

Link to comment
Share on other sites

Hey wpb,

 

You'd do it like so.

 

<a href="editentry.php?id=1"><input type="button" value="Edit"></a>
<a href="editentry.php?id=2"><input type="button" value="Edit"></a>

 

And so on. I didn't test that but I think that's how you do it. :D

 

You can also the use different images as buttons too.

 

Sam

Link to comment
Share on other sites

More a self-imposed limitation than a genuine one.

 

"I don't want to use any method that works (like checkboxes, javascript, images, multiple forms) so how can I do this?"

 

The only option left that I can think of is make them all input type=submit buttons with the same name but give them all uniique values so you will know which one was clicked.

 

[pre]

[ Edit  998] [Delete  998]

[ Edit  999] [Delete  999]

[ Edit 1000] [Delete 1000]

[/pre]

 

where the numbers are the record ids or some other unique column such as username

Link to comment
Share on other sites

Thanks for the replies.

 

Unfortunately, helraizer's suggestion doesn't work (not in IE, anyway). I thought I could just wrap a <button> tag up in a <a href> link, but it just doesn't seem to work.

 

I realize it seems like I'm being contrary, Barand, but I don't want to use Javascript as my browser doesn't support it, and I was hoping to avoid putting a seperate form around every single button to cut down on the byte size of the page. Equally, it seems a shame to have to create a button png or something, when there's a perfectly good one in HTML 4 anyway with the <button> tag. Just a shame you don't seem to be able to make it into a link.

 

Cheers,

 

Will

Link to comment
Share on other sites

I'd use something like this, allowing only a single record to be selected for edit, but many for deletion

<?php
$names = array (1=>'Peter', 'Paul', 'Mary');             // array to simulate db data

if (isset($_GET['sub']))
{
    switch ($_GET['sub'])
    {
        case "Edit":
            echo "Edit record id {$_GET['edit']}";
            break;
        case 'Delete':
            $del_list = join(',', $_GET['del']) ;
            $sql = "DELETE FROM atable WHERE id IN ($del_list)";
            echo $sql;
            break;
    }
}
?>
<form>
<table>
   <tr>
      <td>
         ID
      </td>
      <td>
         Name
      </td>
      <td>
         Edit
      </td>
      <td>
         Delete
      </td>
   </tr>
<?php
        foreach ($names as $id=>$name) 
        echo"   <tr>
              <td>
                 $id
              </td>
              <td>
                 $name
              </td>
              <td>
                 <input type='radio' name='edit' value='$id'>
              </td>
              <td>
                 <input type='checkbox' name='del[]' value='$id'>
              </td>
           </tr>";
?>
</table>
<input type="submit" name="sub" value="Edit">
<input type="submit" name="sub" value="Delete"> 
</form>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.