What I want to do is to use a confirm box where if the user clicks OK, to delete a row from my SQL database. I have a js function I use to send the value "delete" to a php function on the same file like this:
<script>
function myFunction() {
if (confirm("Are you sure you want to delete?") == true) {
document.getElementById("delete").name = "delete";
}else{
return false;
}
}
</script>
The button where the user will click to delete an image is this:
<button onclick="myFunction()">Delete Image</button>
I send the value to the PHP function like this:
<input type="hidden" name="" value="delete" id="delete">
This is my PHP function:
if(isset($_POST['delete'])){
$img_path=$_POST['ipath'];
$imgid=$_POST['imgid'];
$link = mysqli_connect($host, $username, $password, $db);
$delete = "DELETE FROM images_info
WHERE Image_Id = $imgid";
$result3 = mysqli_query($link, $delete);
echo "Image Deleted : $imgid";
mysqli_close($link);
}
What am I doing wrong? I believe it's gotta do with the javascript
What I want to do is to use a confirm box where if the user clicks OK, to delete a row from my SQL database. I have a js function I use to send the value "delete" to a php function on the same file like this:
<script>
function myFunction() {
if (confirm("Are you sure you want to delete?") == true) {
document.getElementById("delete").name = "delete";
}else{
return false;
}
}
</script>
The button where the user will click to delete an image is this:
<button onclick="myFunction()">Delete Image</button>
I send the value to the PHP function like this:
<input type="hidden" name="" value="delete" id="delete">
This is my PHP function:
if(isset($_POST['delete'])){
$img_path=$_POST['ipath'];
$imgid=$_POST['imgid'];
$link = mysqli_connect($host, $username, $password, $db);
$delete = "DELETE FROM images_info
WHERE Image_Id = $imgid";
$result3 = mysqli_query($link, $delete);
echo "Image Deleted : $imgid";
mysqli_close($link);
}
What am I doing wrong? I believe it's gotta do with the javascript
Share Improve this question edited Mar 30, 2016 at 5:39 rrk 15.9k4 gold badges30 silver badges47 bronze badges asked Mar 30, 2016 at 5:36 Yohan BlakeYohan Blake 1,3184 gold badges21 silver badges43 bronze badges 8- to use $_POST in php you need form with method post – Divyesh Savaliya Commented Mar 30, 2016 at 5:41
-
You need to use
ajax
for this. – Ohgodwhy Commented Mar 30, 2016 at 5:45 -
The html is inside
<form action="newsearch.php" method="post">
. However I opened and closed several<div>
s inside this form. Will this make a difference? @DivyeshSavaliya – Yohan Blake Commented Mar 30, 2016 at 5:47 - @Ohgodwhy, is there a simple way like this using ajax? I'm not familiar with ajax – Yohan Blake Commented Mar 30, 2016 at 5:48
- no...its fine to have div inside form – Divyesh Savaliya Commented Mar 30, 2016 at 5:49
6 Answers
Reset to default 6in your html, input name attribute is empty,
<input type="hidden" name="" value="delete" id="delete">
it should be like
<input type="hidden" name="delete" value="delete" id="delete">
$_POST['delete']
will not be set until the name attribute is provided with the value 'delete'
Use it like this. here is PhpFiddle
PHP CODE
<?php
if(isset($_POST['delete']))
{
// PERFORM YOUR DELETE QUERY HERE
print_r($_POST);
}
?>
JAVA SCRIPT
<script>
function myFunction() {
if (confirm("Are you sure you want to delete?") == true) {
document.getElementById("delete").name = "delete";
}else{
return false;
}
}
</script>
HTML
<form method="post" action="">
<button onclick="myFunction()">Delete Image</button>
<input type="hidden" name="" value="delete" id="delete">
</form>
The variable $_POST
is an associative array obtained from a form's <input>
tags. The keys into $_POST
are defined by the name
attributes of an input tag, and the corresponding values in $_POST
are defined by the value
attribute in the same tag. Data in $_POST
does not e from JavaScript unless you use an AJAX library like jQuery. If you want to control this data with pure JavaScript, you must directly set the attributes of your input tags.
You can keep the same JavaScript code you're using, just set the input tag's value
attribute instead of its name
, which should always be delete
. Then in your PHP code, you can test the value of $_POST['delete']
to find out if the user really wanted to delete.
Doesn't seem like anyone wants to answer your question. If you'd like to do this with pure js, here ya go:
var url = 'YOUR URL POST DESTINATION HERE';
function myFunction(elem){
var id = elem.target.id;
var xmlHttp = new XMLHttpRequest();
var params = 'delete=true&imgid=' + id;
xmlHttp.open('POST', url, true);
xmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xmlHttp.send(params);
}
You will need to set imgid
in your js programmatically. Execute myFunction() from your page like
<button onclick="myFunction(this)">Delete Image</button>
As a side note, it's a REALLY bad idea to let users post to a form and modify your DB. ANYONE can make a post request and delete your DB. But I decided to give you the benefit of the doubt and answer your question.
Sorry for all this mess guys, the actual problem was that I had another form inside the form. I didn't know that forms cannot contain forms inside them. Removed the other form, and it works
Do not use onclick="myFunction()" in the <input>
tag. Instead, ensure:
<form method ="post" onsubmit="return confirm("Are you sure you want to delete?")">
<button name = "delete">Delete Image</button>
</form>
<?php
if(isset($_POST['delete']))
{
$img_path=$_POST['ipath'];
$imgid=$_POST['imgid'];
$link = mysqli_connect($host, $username, $password, $db);
$delete = "DELETE FROM images_info WHERE Image_Id = $imgid";
$result3 = mysqli_query($link, $delete);
echo "Image Deleted : $imgid";
mysqli_close($link);
}
?>