How to prompt for button press confirmation? - php

I'm loading data in to a html table using following function. It also creates a delete button in front of each row. I want to prompt user to either confirm or cancel. But the code does not work and it does not prompt for confirmation. Can someone show me how to do it properly?.
I use (onclick="return confirm('Are you sure?')") to prompt.
//This function will list categories
function listCategories($sqlString)
{
$result = mysqli_query($this->connectToDb(), $sqlString);
if(mysqli_num_rows($result) > 0)
{
while ($row = mysqli_fetch_assoc($result))
{
echo "<tr>";
echo "<td>".$row['cat_id']."</td>";
echo "<td>".$row['it_category']."</td>";
echo '<td>
<form action="" method="POST">
<input type="hidden" name="catid" value="'.$row['cat_id'].'">
<input class="btn btn-outline-success" type="submit" name="Delete" value ="Delete" onclick="return confirm('Are you sure?')">
</form>
</td>';
echo "</tr>";
}
}
}

You need to escape the quotes around Are you sure. They're matching the quotes around the string argument to echo.
echo '<td>
<form action="" method="POST">
<input type="hidden" name="catid" value="'.$row['cat_id'].'">
<input class="btn btn-outline-success" type="submit" name="Delete" value ="Delete" onclick="return confirm(\'Are you sure?\')">
</form>
</td>';

Related

interface php and html, in foreach loop

I have a PHP page that shows me data from the database. I'm inserting a button that, if pressed, deletes the record. The problem is that if I push the button the action is done on all the records, because I can't uniquely identify the click on the button
some code
foreach ( $associati as $associato ) {
echo "<hr />";
echo "Nome associato : ".$associato->Nome."</br>";
echo "Codice Fiscale : ".$associato->CF."</br>";
echo "<hr />";
if(isset($_POST["numerazione"])){
echo "Hello world"; //Query for delete
}
?>
<form method="POST" action="">
<input type="submit"
name="numerazione"
value="Elimina utente"
onclick="return confirm('Are you sure?')"
/>
</form>
<?php
}
How can I do to uniquely identify the button?
You can add a hidden field to each form that contains the unique identifier of the data, that means when you click the button, it will create a POST request, and in that POST request you can get the ID of the clicked record by doing $_POST['unique-id'], also make sure to populate the value of that hidden field using PHP
<?php
foreach ( $associati as $associato ) {
echo "<hr />";
echo "Nome associato : ".$associato->Nome."</br>";
echo "Codice Fiscale : ".$associato->CF."</br>";
echo "<hr />";
if(isset($_POST["numerazione"])){
$numerazione = $_POST["unique-id"];
echo "Unique record is : ".$numerazione."</br>";
}
?>
<form method="POST" action="">
<input type="hidden" name="unique-id" value="<?php echo $associato->CF; ?>" />
<input type="submit"
name="numerazione"
value="Elimina utente"
onclick="return confirm('Are you sure?')"
/>
</form>
<?php
}
?>
Pass the unique information (e.g. $id or $associato->id or whatever the variable which can identify the record) when the form is submitted
<form method="POST" action="">
<input type=hidden name=id value=<?php echo $id; ?>>
<input type="submit"
name="numerazione"
value="Elimina utente"
onclick="return confirm('Are you sure?')"
/>
</form>
Solution for this problem:
if(isset($_POST["mod"]) and $_POST["id"]== $associato->ID ){
echo "Hello world";
}
?>
<form method="POST" action="">
<input type="hidden" name="id" value=<?php echo $associato->ID; ?>>
<input type="submit"
name="mod"
value="valore"
onclick="return confirm('Are you sure?')"
/>
</form>

Can't pass form data to PHP to update Database

Thanks to all who responded. I got it to update with a combination of ideas from you. I've amended the code to show what's working.
To implement LIKE functionality by passing the LIKE button value to php so as to update the DB.
This displays the table, complete with a LIKE button per row:
echo '<table>
<tr>
<th>Word ID</th>
<th>User ID</th>
<th>User Name</th>
<th>Word</th>
<th>Meaning</th>
<th>Example</th>
</tr>';
foreach ($data as $row)
{
echo '<tr>';
foreach ($row as $value)
{
echo '<td>';
echo $value;
echo '</td>';
}
echo '<td>
<form method="POST" action="'.$_SERVER["PHP_SELF"].'">
<input type="hidden" name="LIKE" value="'.$row['wordID'].'">
<input type="submit" class="btn btn-success" value="Submit">
</form>
</td>';
echo '</tr>';
}
echo '</table>';
The code to process the form submit is:
if($_POST['submit'])
{
$sql = "UPDATE vocab SET likes = likes+1 where wordID = '{$row['wordID']}'";
$stmt = $db->prepare($sql);
/* Execute */
$stmt->execute();
}
<form method="post" action="<?php echo $_SERVER["PHP_SELF"]?>
is a sytax you use when you are NOT already in a <?php tag. Here, you already are writing in PHP and in an echo and you just apen again a <?php, and another echo in it, this makes no sense.
echo '... <form method="post" action="'.$_SERVER["PHP_SELF"].'"> ...';
is the way you want to include your php_self value.
also
<input type="submit" value="LIKE" name="<?php echo $row["wordID"]; ?></form>
should be
<input type="submit" value="LIKE" name="'.$row['wordID'].'"></form>
but you cant the search for $_POST['wordID'], search for $_POST[$row['wordID']] in a for loop.
You're doing an echo inside an echo here :
echo '<td>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"]?>
<input type="submit" value="LIKE" name="<?php echo $row["wordID"]; ?></form>
</td>';
<form method="post" action="<?php echo $_SERVER["PHP_SELF"]?>
should be :
<form method="post" action="'.$_SERVER["PHP_SELF"].'">
i would do this:
<td>
<form method='POST'>
<input type='hidden' name='LIKE' value='".$row['wordId']."'>
<input type='submit' value='Submit'>
</form>
</td>
Action will always default to : PHP_SELF so no point adding that, additionally adding the hidden field instead of the submit button is preferable to me as it's easier to read, plus adds an easy option incase you actually want to add more to the form (which is entirely possible)

Delete the right data using post

I have this code. i just want to delete the record that i want to delete but when i click the button delete it only delete the last data record added what should i do ? Thankyou in advance
if(isset($_POST['delete'])){
$id = $_POST['delete_rec_id'];
$query=mysqli_query($link,"Delete from hgrecord where PossibleCondition ='".$id."' ");
}
if(isset($_GET["poscon"])){
$kwery=mysqli_query($link,"select Distinct PossibleCondition from hgrecord where PatientId='".$session."' and Date='".$new_time."' order by PossibleCondition");
while($rr=mysqli_fetch_array($kwery)){
$PatientId=$rr["PatientId"];
$condition1=$rr["PossibleCondition"];
if(isset($_POST) && isset($_POST['sym1']) && in_array($condition1,$_POST['sym1']))
$strIsChecked='checked="checked"';
else
$strIsChecked=null;
echo '<br><td><input type="checkbox" '.$strIsChecked.' title ="'.$otherspec1.'" name="sym1[]" onclick="javascript: submit()" value ="'.$condition1.'"></td>';
echo '<td align="">'.$condition1.'</td>';
?>
<button type="submit" name="delete" aria-label="Left Align">
<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
</button>
<input type="hidden" name="delete_rec_id" value="<?php print $condition1;
?>"/>
<?php } } ?>
You have to add your form tag around each set of button and hidden input:
<form action="#" method="post">
<button type="submit" name="delete" aria-label="Left Align">
<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
</button>
<input type="hidden" name="delete_rec_id" value="<?php print $condition1;" />
</form>
Otherwise you'll have one form with multiple hidden inputs with name="delete_rec_id".
Not sure what you are doing in your javascript function javascript: submit()
What I can suggest it, add the functionality in your javascript function to collect(comma separated values) all the checked checkboxes and assign it to the hidden field delete_rec_id.

PHP Delete not working as I would I like

Can someone help me. My delete code below works, but it's deleting the most recent Favorited file and not the specific file chosen. Here's the code:
while($row=$query->fetch())
{
$id=$row['id'];
$vid=$row['thread_id'];
$preview=$row['preview'];
$tt=$row['thread_title'];
$fav=$row['fav'];
$List.='<form action="" method="POST" id="postForm">
<div class="LISTT">'.$preview.'<br/><label id="pwords">'.$tt.'</label><br/>
<input type="submit" name="submit" value="Remove" id="DeleteButton"/>
</div></form>';
if(isset($_POST['submit']))
{
$query=$db->prepare("DELETE FROM favorite WHERE thread_id=:thread");
$query->execute(array(':thread'=>$vid));
}
}
You need to add a hidden form field that contains the Thread ID into your form, then read that back in your form handler, something like this:
while($row=$query->fetch())
{
$id=$row['id'];
$vid=$row['thread_id'];
$preview=$row['preview'];
$tt=$row['thread_title'];
$fav=$row['fav'];
$List.='<form action="" method="POST" id="postForm">
<div class="LISTT">'.$preview.'<br/><label id="pwords">'.$tt.'</label><br/>
<input type="hidden" name="thread" value="' . $vid . '" />
<input type="submit" name="submit" value="Remove" id="DeleteButton"/>
</div></form>';
if(isset($_POST['submit']))
{
$id = $_POST["thread"];
$query=$db->prepare("DELETE FROM favorite WHERE thread_id=:thread");
$query->execute(array(':thread'=>$id));
}
}
The reason for this is because you have the If statement in your while loop. The logic in the code you have given is to delete records when $_POST['submit'] is set. So it will follow the loop to delete the records and not a specific record.
You need to pass the id you want to delete to the user, as you are using form to do this, have a hidden field with the id.
if(isset($_POST['submit']))
{
$query=$db->prepare("DELETE FROM favorite WHERE thread_id=:thread");
$query->execute(array(':thread'=>$_POST['id']));
}
while($row=$query->fetch())
{
$id=$row['id'];
$vid=$row['thread_id'];
$preview=$row['preview'];
$tt=$row['thread_title'];
$fav=$row['fav'];
$List.='<form action="" method="POST" id="postForm">
<div class="LISTT">'.$preview.'<br/><label id="pwords">'.$tt.'</label><br/>
<input type="submit" name="submit" value="Remove" id="DeleteButton"/>
<input type="hidden" name="id" id="id" value="'.$id.'" />
</div></form>';
}

HTML Forms in PHP

i am writing html form code with in php script
here it is
<?php
$problem_code="STR";
echo '<form name="submit-button" action="/codejudge/submit.php?id='.$problem_code.'">';
echo '<button type="submit" >Submit</button>';
echo "</form>";
?>
But after submitting url look like this localhost/codejudge/submit.php ?
but it should be like this localhost/codejudge/submit.php?id=STR
If a form is method="GET" (which is the default), as this one is, then submitting it will erase the existing query string in the action.
Store the data in a hidden input instead.
<?php
$problem_code="STR";
?>
<form name="submit-button" action="/codejudge/submit.php">
<input type="hidden" name="id" value="<?php echo htmlspecialchars($problem_code); ?>">
<button type="submit">Submit</button>
</form>
You should specify a method of form submit.
$problem_code="STR";
echo '<form method=post name="submit-button" action="/codejudge/submit.php?id='.$problem_code.'">';
echo '<button type="submit" >Submit</button>';
echo "</form>";

Categories