2

Hello I'm trying to give each of these button names a uniquely numbered name. There can be up to 50 of these button pairs and each pair will refer to one order. (This is for the admin page of a practice estore im building) Anyway, where their name is currently "dele_ord" and "ship_ord" I would like it to be (somehow) Dele_ord($i) So that each button is numbered ie: dele_ord34

Once i have this setup I will use an if(isset($_POST(dele_ordXX){...} to run database queries that will delete or modify the orders associated with the buttons.

Im very new to php and mysql and want to learn it better.

        $i=1;
           while($i<=50)
        {
        echo '<h5>'.$row['o_id'].' || '.$row['first_name'].' || '.$row['last_name'].' || '.$row['prod_id'].' || '.$row['prod_title'].' || '.$row['prov'].' || '.$row['city'].' || '.$row['street'].' || '.$row['s_instruct'].' || '.$row['order_time'].' || '.$row['ship_status'].'</h5>';

        echo'<form action="admin.php" method="post" class="dele_o"><input type="button" name="delete_ord" value="Delete Order"></form>';
        echo'<form action="admin.php" method="post" class="ship_o"><input type="button" name="ship_ord" value="Ship Order"></form> <br />';         

        $row = mysqli_fetch_array($result);
         $i++;
        }
        mysqli_close($link); 
        ?>
3
  • Hi Luke. Not a bad question, I'll help out =] Commented Feb 12, 2012 at 0:17
  • You should not use <form>'s nor buttons. See my post below. Commented Feb 12, 2012 at 0:46
  • Another way to do it is to use checkboxes. It's easier to use (since you can delete/ship multiple orders at the same time) and it gives you less mark-up code. Posted an answer below, check it out Commented Feb 12, 2012 at 0:56

4 Answers 4

1

You don't need a separate <form> tag for each button, you can have multiple submit buttons with different names in the same form.

Anyway, to answer your question, if you use double quotes "..." instead of single '...' for your echo commands, then php allows you to embed variables within your string, for example:

$i = 5;
echo "<input type=\"button\" name=\"delete_ord$i\" value=\"Delete Order\">";

That will output

<input type="button" name="delete_ord5" value="Delete Order">

Note how I've used \ to escape the double quotes in the string, although you could also just use single quotes for your HTML attributes instead if you prefer.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! I always have more trouble with the syntax of code then with the solution!
1

Well, interestingly you're individually wrapping each button with it's own form context, perhaps you can accomplish what you're looking for based on which button is submitted.

In either case, you should specify a hidden value to indicate the id of your order:

<form action="admin.php" method="post" class="dele_o">
    <input type="hidden" name="order_id" id="$i" />
    <input type="submit" name="delete_ord" value="Delete Order" />
</form>

<form action="admin.php" method="post" class="ship_o">
    <input type="hidden" name="order_id" id="$i" />
    <input type="submit" name="ship_ord" value="Ship Order" />
</form>

And then on the accepting POST side you can do:

$order_id = $_POST["order_id"];
if(isset($_POST["delete_ord"])) {
    // Delete order by $order_id
}
if(isset($_POST["ship_ord"])) {
    // Ship order by $order_id
}

2 Comments

there's a missing " in the first submit, at the type parameter :)
Why would you say "You should not". Assuming both were equally safe (which they're not) then you'd simply be going with preference. And in that case, form posts == cleanlier urls. The whole point of our posts is to split up the action and the order into separate variables anyway.
0

Nick is correct in his answer, however I think there is a slightly better way to do this.

Rather than echoing out that long line, close the PHP tag and put the dynamic element of the number inside a PHP tag.

while(loop){
?>
<input type="button" name="delete_ord" value="<?php echo $i ?>">
<?php
}

On the post page, you'd access the $_POST["delete_ord"] for the correct ID

Comments

-1

Don't use either form's or button's for this. Just use anchors.

for($i = 1; $i <= 50; $i++){
  echo '<h5>'.$row['o_id'].' || '.$row['first_name'].' || '.$row['last_name'].' || '.$row['prod_id'].' || '.$row['prod_title'].' || '.$row['prov'].' || '.$row['city'].' || '.$row['street'].' || '.$row['s_instruct'].' || '.$row['order_time'].' || '.$row['ship_status'].'</h5>';

  echo'<a href="admin.php?action=delete&id=' . $row['o_id'] .'">Delete Order</a>';
  echo'<a href="admin.php?action=ship&id=' . $row['o_id'] .'">Ship Order</a>';

  //$row = mysqli_fetch_array($result); // what is this for?
  $i++;
}

Then after clicking;

<?php

if(isset($_GET['action']) AND isset($_GET['id'])){

  switch($_GET['action']){

    case "delete":
      // do action for deletion $_GET['id']
    break;
    case "ship":
      // do action for shipment $_GET['id']
    break;
    default:
      die('unknown action');
    break;

  }

}

As a response to the form vs. hyperlink. A form is used to submit data that has been inputted by the visitor. A hyperlink is used for page requests. There is no reason at all in this case to use a form and it does not add any value or security, it just makes it more complicated. Regardless of using a form or a hyperlink, you need to secure your pages by checking if the active user that requests the URL is logged in and has the privileges to do what is requested. Using a hyperlink is only insecure if your code is not secured, with a form, this is exactly the same. Tom's reply is complete rubbish and based on no facts whatsoever, otherwise please tell me the arguments for them.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.