Jump to content

submit buttons in a while loop


SirChick

Recommended Posts

I have a delete button in a while loop which echos next to each message per row but how do i make them assigned to the message ID so depending on which submit button is pressed the correct id is deleted... this is what i have... i also must add the the check box must be unique aswell because the user has  to tick the checkbox before pressing delete but at the moment there all the same name so its not going to work in the process validation:

 

echo '<tr>
    <td width="200"><center><font style="font-size:14px" color="#FFFFFF" face="Arial">From: <font color="#32CD32">'; 
Echo $UserName; 
Echo'</font></br><font color="#EE9A4D">'; 
Echo $Staff; 
Echo'</font>';
Echo'</br><font color="#FFFFFF">Sent On: </font><font color="#32CD32">'; 
Echo $SentOn; 
Echo'</font></br></center></font></td>
    <td width="200"><center><font style="font-size:14px" color="#FFFFFF" face="Arial">Subject: </font></br><font color="#32CD32">'; 
Echo $Subject; 
Echo'</br><hr size="2" width="40" id="Line2" style="width:200px;height:2px;z-index:0">';
Echo'</font>'; 
Echo'<font color="#FFFFFF">Message: </font><font color="#32CD32"></br><b>'; 
Echo $Message; 
Echo'</font></b></center></font></td>';

Echo'
<td background="menu_gradient.gif" bgcolor="#FFFFFF" style="background-repeat: repeat-x;"><center><font style="font-size:14px" color="#FFFFFF" face="Arial">';
Echo'<a href="reportext.php?report=';Echo$MessageID;Echo'">Report</a></br></br>';
Echo'<input type="checkbox" id="Checkbox1"" name="Checkbox1" value="">
<input type="submit" id="Button1"" name="Button1" value="Delete">';
Echo'</center></td></tr>';

 

what should I do ?

Link to comment
Share on other sites

You would either need to wrap the submit button inside <form> tags and then have the ID to delete in the url the form submits to, or in a hidden field within that form.

 

Alternatively, you can use javascript onClick to forward to a specific url containing the ID to delete.

 

This isn't really a php problem, maybe someone can move it to an HTML related forum.

Link to comment
Share on other sites

it is in form tags ... im only showing u the layout because i dont see why u'd need to see the variable assignments, but are u referring to $_GET ?

 

i was hoping to put $messageID as the submit name

but then realised the form process would have to have tons of checks per id which isnt logical.

 

i can show u the whole thing if it is needed ?

Link to comment
Share on other sites

My bad, sorry.

 

 

You want it so that the user has check the box, then delete the record?

 

Can they check multiple boxes and delete them all with one button?

 

If they can only check one box, and hit the delete button, they should probably be radio buttons instead of check boxes so only one can be selected at once.

 

Otherwise, the only real way to submit a specific value to the server with a submit button is by its name. You could have your radio buttons/check boxes contain the ID of the record as the value, then each button's name would be something like delete-XX where XX is the ID that of the record as well.

 

Then on the back end, you would find the ID of the selected radio button or check box, and see if a POST value with a key like 'delete-XX' is present.

 

 

There really isn't an easy way around the issue the way you are doing it.

 

 

Link to comment
Share on other sites

well the user can delete more than one yes... so many check boxes can be clicked and there is global "delete select" button which is outside the while loop but for individual deleting i wanted the delete button on each row..and that button would represent which one to delete.

 

but if i have a while loop the submit buttons is always going to have the same name.. so how do i unique them... is there an easier way to do this ?

Link to comment
Share on other sites

That's the weird thing about it.

 

Check the $_POST (using var_dump() or print_r())array after submitting the form. It'll show you all the values it contains and how the data is structured.

 

You will most likely have to loop through each value in the $_POST array, check if the key begins with 'delete-', and extract the ID number from the string. Once you have the ID, you can then check to see if the ID is present in the array of checked boxes.

Link to comment
Share on other sites

It's a bit confusing because its kind of an unconventional way of doing things.

 

Anyways, here's something to get you started. Paste the code in to a regular php file and run it. Then you can play around with it and see where you get. The var_dump() in the script shows you exactly what gets sent to the server every time you hit a button.

 

 

<form action="<?php echo $PHP_SELF; ?>" method="post">
<?php for($i = 1; $i <= 10; $i++): ?>
<div>
	Record #<?php echo $i; ?>
	<input type="checkbox" id="Checkbox1" name="delete_confirm[]" value="<?php echo $i; ?>">
	<input type="submit" id="delete" name="delete-<?php echo $i; ?>" value="Delete">
</div>
<?php endfor; ?>
</form>
<br /><br />


<?php

if ($_POST)
{
var_dump($_POST);

foreach ($_POST as $key => $value)
{
	if (preg_match('/^delete-([0-9]+)$/', $key, $matches))
	{
		$delete_id = $matches[1];
		break;
	}
}

// check if the checkbox for $delete_id was checked
if (is_array($_POST['delete_confirm']))
{
	if (in_array($delete_id, $_POST['delete_confirm']))
	{
		echo "Deleting record ID {$delete_id}!";
	}
	else
	{
		echo "Check box for record ID {$delete_id} was not checked!";
	}
}
else
{
	echo "No check boxes checked!";
}
}

?>

 

 

 

I used a regex match to do 2 things at once (check if the key begins with 'delete-' and extract the ID if it does). How you check if it begins with 'delete-' and extract the ID can be done differently using regular string manipulation functions. Those may be simpler, but a preg_match with an appropriate regex pattern will do it all at once. :)

Link to comment
Share on other sites

I guess, in theory that could work. I can't say for sure as I don't know what the rest of your code looks like.

 

It would actually be this though:

 

$MessageID = $_POST['delete-'.$_SESSION['MessageID']; 

 

 

You have to make sure you keep the 'delete-' part of the key there.

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.