Delete the right data using post - php

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.

Related

How to prompt for button press confirmation?

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>';

cannot get $_POST variable value when HTML form is isset

i want to get the value of a text input 'name="quantity"' when my html form is isset using $_POST , the problem is everytime i submit the form i cannot get it's value !
HTML :
<form method="POST" name="updateform">
<!-- the input text that i want to get it's value when the form is isset -->
<input type="text" name="quantity" value="<?php echo $row['quantite'] ?>" size="1" class="form-control" />
<!-- the input text that i want to get it's value when the form is isset -->
<a type="submit" name="updateu" data-toggle="tooltip" title="Update" class="btn btn-primary" href='cart.php?id=<?php echo $getid ?>&upt=<?php echo $row['idproduit']; ?>' ><i class="fa fa-clone"></i></a>
</form>
PHP :
//update commande
if (isset($_POST['updateform'])) {
$mdf = $_POST['quantity'];
echo $mdf;
}else{
echo "form not isset()";
}
//update commande
it's showing "form not isset"
any solutions please , and thanks
You cannot use an anchor tag as a portion of the form, as you've done here:
<a type="submit" name="updateu" data-toggle="tooltip" title="Update" class="btn btn-primary" href='cart.php?id=<?php echo $getid ?>&upt=<?php echo $row['idproduit']; ?>' ><i class="fa fa-clone"></i></a>
The link never becomes part of the post array. You will need a submit input named updateu, for example:
<input type="submit" name="updateu" ...

How to stop 2 buttons from opening the same div tag in jquery

I have been working on a comment system in php and jQuery. I am using jQuery to slide a form whenever you press reply but instead 2 buttons work for only one form.
I am using php to get the data from a mysql database and display the content it displays it in div tags with a button that says reply when I click on the second button the first div come's down.
PHP CODE:
<?php if(!isset($_SESSION['fname']) && !isset($_SESSION['username']) && !isset($_SESSION['email'])) {
echo "<h1>You must be logged in to comment</h1>";
} else { ?>
<div id="comment">
<form action="createcomment.php?videoid=<?php echo $_GET['id']; ?>" method="post">
Comment: <br /><textarea name="comment" placeholder="Your comment..."></textarea><br />
<input type="submit" name="submit" value="Post Comment" class="btn btn-warning">
</form>
</div>
<?php } ?>
<div id="comments">
<?php $sql = "SELECT * FROM comments WHERE videoid = " . $_GET['id'];
$result = mysqli_query($db, $sql);
while($row = mysqli_fetch_assoc($result)) {
if(!empty($result)) {
?>
<div class="comment">
<h5><?php echo $row['name']; ?></h5>
<p><?php echo $row['comment']; ?></p>
<button class="btn btn-primary btn-small">Reply</button>
<div id="reply">
<form action="replycomment.php?videoid=<?php echo $_GET['id']; ?>&commentid=<?php echo $row['id']; ?>" method="post">
Reply Message: <br /><textarea name="comment" placeholder="Your comment..."></textarea><br />
<input type="submit" name="submit" value="Post Reply" class="btn btn-warning">
</form>
</div>
</div>
<?php }else {
echo "<h1>There are no comments.</h1>";
}
}?>
</div>
jQuery code:
<script>
$(document).ready(function(){
$("#reply").hide();
$("#comments > div > button").click(function(){
$("#reply").slideToggle();
});
});
</script>
The jQuery is supposed to hide all of the reply divs and when you click reply the div opens instead the first button works but the last button opens the first one i have no idea what is going on.
Thank you.
There can only be one of each ID in the entire DOM. You have an element with the id of "reply". Since the database is looping through all of the comments, there will be multiple elements with the id of "reply". This syntax is not correct.
Now, if you click on a button to toggle the element with the id of "reply", all of them will toggle. That's not what you want. Change the HTML to:
<button class="btn btn-primary btn-small">Reply</button>
<div class="reply">
<form action="replycomment.php?videoid=<?php echo $_GET['id']; ?>&commentid=<?php echo $row['id']; ?>" method="post">
Reply Message: <br /><textarea name="comment" placeholder="Your comment..."></textarea><br />
<input type="submit" name="submit" value="Post Reply" class="btn btn-warning">
</form>
</div>
Note that I have removed the id, and added the class. There can be multiple classes in a DOM. And so, the jQuery needs to hide all of the classes, so:
$(document).ready(function(){
$(".reply").hide();
// Hides all the elements with class="reply"
});
Now all we need is the button click action. If we just change the hashtag to the dot and make it a class, it still will toggle ALL of the elements with the class of reply. We need to just toggle the element that is right after the clicked button... right?
And so:
$(document).ready(function(){
$("#comments > div > button").click(function(){
$(this).nextAll('.reply:first').slideToggle();
// Gets all the next sibling elements under the same parent, and selects the first one with the class="reply"
});
});

PHP form only posting last value in a series of inputs

Attached are pictures of my code. I am trying to pass the index.php value (either 4 or 8) of whichever button named 'num' is clicked to chooseSnacks.php and echo the variable $snacks.
The problem is that it only passes the last valued input in the form (which is 8) no matter if I click on 4 or 8. I reversed the order of the inputs just to double check, and when I did so I could only get the value 4 to pass no matter what I clicked.
Inputs on index.php
<div class="pricing-button">
<form method="POST" action="ChooseSnacks.php">
<input class="btn btn-primary btn-md btn-square" onclick="change('19.99')" type="text" value = "4" name="num[]" readonly></a>
<input class="btn btn-primary btn-md btn-square" onclick="change('25.00')" type="text" value = "8" name="num[]" readonly></a>
</div>
Submit button on index.php
<input class="btn btn-primary btn-lg btn-square" type="Submit" name="Submit" id="Submit" value='Submit'>
Session start on chooseSnacks.php
<?php
session_start();
$snacks = $_POST['num[]'];
?>
echo the variable
<div class="categories">
<h3>Your Carepackage</h3>
<p> <?php echo $snacks; ?> Snacks Every <?php echo $snacksNum; ?> </p> </div
In regards to $_POST['num[]']; you need to remove the [] from in there, because you already declared them as an array in name="num[]".
You then need to use a foreach($_POST['num'] as $var) and checking if they are not empty.
I used a ternary operator for that.
For example:
<div class="pricing-button">
<form method="POST" action="">
<input class="btn btn-primary btn-md btn-square" onclick="change('19.99')" type="text" value = "4" name="num[]" readonly></a>
<input class="btn btn-primary btn-md btn-square" onclick="change('25.00')" type="text" value = "8" name="num[]" readonly></a>
<input type="submit" name="Submit" value="Submit">
</div>
<?php
$inputs = !empty($_POST['num']) ? $_POST['num'] : array();
foreach($inputs as $value) {
echo $value . "<br>";
}
which will echo: (as tested)
4
8
I'm unsure as to what onclick="change('19.99')" etc. is supposed to react as though and the need for sessions where you started the session.
Well your input type is TEXT so it overrides the last value.
If you want the user to choose an option you should change the type to RADIO, OR set the value and name to blank and change it with javascript

Multiple Submit buttons, how do determine which one was clicked?

I have a form with multiple submit buttons.
Each submit button is an IMG SRC trash can which denotes the delete icon for messages in a web based messaging mail inbox
what is the best way to figure out which submit button icon was clicked so that I can then write the PHP/MySQL code to DELETE the message?
if(!empty($_POST)){
// How do I figure out which submit button has been clicked to get the ID of the message to delete?
}
<form method="POST">
<input src="http://www.foo.com/img.png" id="button_1">
<input src="http://www.foo.com/img.png" id="button_2">
<input src="http://www.foo.com/img.png" id="button_3">
<input src="http://www.foo.com/img.png" id="button_4">
...
<input src="http://www.foo.com/img.png" id="button_100">
</form>
Set value for each submit button and check that in php and find which one is clicked
<form method="POST">
<img src="http://www.foo.com/img.png" id="button_1" name="submit_btn" value="1">
<img src="http://www.foo.com/img.png" id="button_2" name="submit_btn" value="2">
<img src="http://www.foo.com/img.png" id="button_3" name="submit_btn" value="3">
<img src="http://www.foo.com/img.png" id="button_4" name="submit_btn" value="4">
...
<img src="http://www.foo.com/img.png" id="button_100" name="submit_btn" value="100">
</form>
echo $_POST['submit_btn']; will give you the value of which submit button is clicked
Give each button a name=""
Then you can do something like
isset($_POST['button_name']) {
// execute code here if true
}
THE solution of this problem is to use the NAME attribute of the tag input/button.
<input type="submit" name="submitSave" value="Save"/>
<input type="submit" name="submitAddComment" value="Add comment"/>
or
<button type="submit" name="submitSave">Save</button>
<button type="submit" name="submitAddComment">Add comment</button>
I think you can also use the value attribute of button tag, this is definitively not possible with input tag.
If you need to use an ID or another variable, use name="submitDelete[888]"
Then, check it with PHP:
if( isset($_POST['submitDelete']) ) {
echo key($_POST['submitDelete']);// Displays the ID to delete, e.g. 888.
}
So many years later, I like button because it allows to display a text or an image independently of the value returned.
Here is an illustration of possibilities which fits the title of this post and more cases than the OP.
<?php
if(!empty($_POST['id'])){
echo 'button '. $_POST['id'] .' clicked';
} elseif ('create' === ($_POST['action'] ?? '')) {
echo 'create clicked'; // ?action=create
} elseif (isset($_POST['action'])) {
echo 'refresh clicked'; // ?action
} elseif (isset($_POST)) {
echo 'Default clicked'; // ?
}
?>
<form method="POST">
<!-- Original Post examples -->
<button type="submit" name="id" value="1"><img src="http://www.foo.com/img.png"></button>
<button type="submit" name="id" value="2"><img src="http://www.foo.com/img.png"></button>
...
<button type="submit" name="id" value="100"><img src="http://www.foo.com/img.png"></button>
<!-- Additional possibilities -->
<!-- ?action=create -->
<button type="submit" name="action" value="create">New element</button>
<!-- ?action -->
<button type="submit" name="action">Refresh</button>
<!-- ? -->
<button type="submit">Default</button>
</form>
you can give a name and a value to each of your buttons. It will then show up under $_POST['submit']
<img src="http://www.foo.com/img.png" id="button_4" name='submit' value='4' />
You have to pass your value to the current file by declearing name and value for each.. then you can echo in your php script in order to know which one is clicked.

Categories