php javascript confirm box for delete - php

The below code is in PHP
echo "<input type='submit' name='delete' value='$t2[0]' class='delete_button' onclick='return confirm('Please Confirm Delete');'>"
I am trying to create a delete button, when a user clicks on delete button , it should ask confirmation. but in this case, its not working.
is there any best way to delete with confirmation in php with/ or javascript
and no ajax

Your quotes are breaking themselves here;
onclick='return confirm('Please Confirm Delete');'>
Instead use;
onclick="return confirm('Please Confirm Delete');">

Well, in javascript you can do it as:
<input type='submit' name='delete' value='$t2[0]' class='delete_button' onclick='return askme();'>
//javascript function
function askme() {
if(confirm("Are you sure you want to delete this..?")) {
//delete someting
}
}

The quotes are going wrong, use this instead:
echo "<input type='submit' name='delete' value='$t2[0]' class='delete_button' onclick='return confirm(\"Please Confirm Delete\");'>"
You are going out of your attribute by opening the single quote again inside your confirm.

You cannot "call php code into jquery". The only thing you can do is to set up a request (AJAX) to a server side PHP script which will take over the respective parameters you transferred to the script and produce an output (using echo or print) which will automatically be available in the request's callback.
With jQuery it's as easy as that
$.post('url/to/script.php', {parameter1: 'whatever', param2: 'something'}, function(response) {
// the output of the PHP script is available in the variable "response"
alert(response);
});
The PHP script can take over the parameters, take any action with it and create output
$param1 = $_POST["parameter1"];
$param2 = $_POST["param2"];
// do whatever you want with $param1 and $param2
// create some output using echo/print
echo "This will be transferred back to the calling Javascript";

You can try this, quite easy and it works.
<td> <a onClick="return confirm('DELETE: Are You sure ??');" href="member_delete?id=<?php echo $row['memID'];?>" >delete <i class="fa fa-trash-o" aria-hidden="true"></i></a></td> [DEMO][1]
I assume, its in a table list of members.

Related

How add confirmation box in php before deleting?

I create one simple list in PHP where user can add Name, age, emails, etc. I added a delete option too, but I want to add a confirmation message when the user clicks on delete option.
I tried searching Google but I found only jQuery and JavaScript solutions. Is there any way to do this with PHP only?
List.php
<?php
include('config.php');
$query1=mysql_query("select id, name, age from addd");
echo "<table><tr><td>Name</td><td>Age</td><td></td><td></td>";
while($query2=mysql_fetch_array($query1))
{
echo "<tr><td>".$query2['name']."</td>";
echo "<td>".$query2['age']."</td>";
echo "<td><a href='edit.php?id=".$query2['id']."'>Edit</a></td>";
echo "<td><a href='delete.php?id=".$query2['id']."'>x</a></td><tr>";
}
</table>
?>
Delete.php
<?php
include('config.php');
if(isset($_GET['id']))
{
$id=$_GET['id'];
$query1=mysql_query("delete from addd where id='$id'");
if($query1)
{
header('location:list.php');
}
}
?>
If you want to do this only in PHP, you will need to add "steps" in your script, like:
step1 (show form) -> step2 (ask validation) -> step3 (validate)
To do so, you can use sessions to keep form content, and GET parameter to track the step.
Otherwise the simplest solution is to use javascript:
echo "<td><a onClick=\"javascript: return confirm('Please confirm deletion');\" href='delete.php?id=".$query2['id']."'>x</a></td><tr>"; //use double quotes for js inside php!
This is u need
while($query2=mysql_fetch_array($query1))
{
echo "<tr><td>".$query2['name']."</td>";
echo "<td>".$query2['age']."</td>";
echo "<td><a href='edit.php?id=".$query2['id']."'>Edit</a></td>";
echo "<td><a onclick='javascript:confirmationDelete($(this));return false;' href='delete.php?id=".$query2['id']."'>x</a></td><tr>";
}
and create javascript function
function confirmationDelete(anchor)
{
var conf = confirm('Are you sure want to delete this record?');
if(conf)
window.location=anchor.attr("href");
}
believe me it's work :)
Here is a variation of above that gives you the Confirmation box and passes a variable from PHP to Javascript and back to PHP.
I used this to select a radio button to delete a file from a list of files.
See OnClick runs function with php $fileName past to Javascript, confirmation asked with file name, and if yes, transfers to href with variables for $_GET
PHP/HTML Code:
<?php
echo "
<tr>
<td>
<input type='radio' name='input_sched_button'
onclick='confirmation(".'"'.$fileName.'"'.")' />
</td>
<td class='fixed-td-450'><a href='./$namehref'>$fileName</a></td>
<td class='fixed-td-40'>$extn</td>
<td class='col3'>$size</td>
<td class='fixed-td-80'>$modtime</td>
</tr>
";
?>
Javascript
<script>
function confirmation(delName){
var del=confirm("Are you sure you want to delete this record?\n"+delName);
if (del==true){
window.location.href="Some_Program.php?delete=y&file="+delName;
}
return del;
}
</script>
onclick="return confirm('Are You Sure ?')"
delete
Add an onClick event to trigger the dialog box and javascript:return confirm('are you sure you want to delete this?');
echo "<td>x</td><tr>";
<script>
function deleletconfig(){
var del=confirm("Are you sure you want to delete this record?");
if (del==true){
alert ("record deleted")
}
return del;
}
</script>
//add onclick event
onclick="return deleletconfig()"
work for me but, change this:
onclick='javascript:confirmationDelete($(this));return false;'
with:
onclick='confirmationDelete(this);return false;'
I don't know, if this makes any sence, but I have come up with a solution on my own. It does not work, but I would like to share the idea in here, as it basically is supposed to do the same thing. My solution was to just have php echo the javascript, and then having the code, that is supposed to be executed echoed in javascript, so it would be execuded as normal php on site.
this is the site where I got the idea of running the php inside the JS
echo "
<script>
if (window.confirm('möchten Sie die Datei wirklich unwiderruflich löschen?')){
window.alert('<?php
unlink($allFiles);
rmdir($fileDir));
?>');
}
else{
window.alert('Vorgang abgebrochen');
}
</script>
";
How I mentioned, it does not work, so I solved it just by not having confirmation.
Would be curious why this solution does not work.

Add a button to a row in a HTML table with a link to a URL containing data from another column in that row

I have a HTML table with a column that contains a button at the end of each row:
echo "<td><button type='button' class='btn btn-primary'>View Order </button></td>";
How can I make this button link to viewOrder.php and also pass the order number from the first column on the same row for e.g. viewOrder.php?id=<data from column 1 here>.
Assuming you have a PHP variable $orderNumber:
echo "<td><button type='button' class='btn btn-primary' onClick='doThing({$orderNumber});'>View Order </button></td>";
Now you need to define the method doThing in javascript:
function doThing(ordernum)
{
document.location.href = "viewOrder.php?id=" + ordernum;
}
Since your button is taking you to a new page, and you are asking for a get request, I think you should use a link. Optionally you could style the link to make it look like a button.
This way you avoid the use of java-script to do the get Request by appending the orderNumber in the URL.
Since it seems you are using BootStrap, you can use the btn btn-primary classes to make the link look like a button
Try this:
echo "<td>View Order</td>";
JSFiddle
echo "<td><button type='button' class='btn btn-primary' onClick='window.location.href='viewOrder.php?id='+orderNumber'>View Order </button></td>";
Unable to provide more, as need more code.
If your order number is some div or span, etc that have id="orderNumer", use document.getElementById("orderNumber").innerHTML.

Using a hyperlink to submit a form instead of a button through an intermedite function is not working

I want to submit a form through an 'edit(param1,param2)' function which in turn is being called in either of the two ways..
echo '<input type="button" value="DELETE" onclick="edit(\''.$key.'\',\''.$b.'\')"/>';
or
echo '<a href="list_cadmin.php" onclick="edit(\''.$key.'\',\''.$b.'\')"><span class="bluetext">DEACTIVATE</span>';
the function edit() is something like this:
function edit(a,b) {
var answer = confirm("Do You Really want to Deactivate ?")
if (answer){
alert(a)
document.getElementById('cid').value= a;
document.getElementById('key').value= b;
document.getElementById('fname').method='get';
document.getElementById('fname').action='samepage.php';
document.getElementById('fname').submit();
}
}
where $key and $b are number and string values respectively.
so, according to the above both should go to 'samepage.php?cid=BLAHBLAH&key=1234' on onClick. But only the input=button is working. Hyperlink is reloading without the GET parameters. How do i get the hyperlink to work?
You need to prevent the href from executing by returning false from the onclick:
echo '<a href="list_cadmin.php" onclick="edit(\''.$key.'\',\''.$b.'\'); return false;"><span class="bluetext">DEACTIVATE</span>';
Try to use
echo '<span class="bluetext">DEACTIVATE</span>';
<?php
echo <<<EOD
<a href="javascript:void(0);" onclick="edit('{$key}','{$b}')">
<span class="bluetext">DEACTIVATE</span></a>
EOD;

call javascript function in php and pass php variables?

Say I have 3 variables, id, name, number, on a php script, $name, $number, $id
On the PHP, I create a button
print "<td><form><input type='button' name='edit' id='edit' onclick=ed($name, $number, $id) value='Edit' /></td>";
I want those 3 variables sent to my javascript.
The function call seems to work just fine if I use
onclick=ed(this.id)
and modify the function header, and this passes the string "edit" but that's not quite useful.
function header in javascript is
function ed(name, number, id) {
//stuff here
}
For whatever it's worth using this code gets me an error on the html, something about an unexpected } on line2 of the html document.
Edit:I should clarify I said that code gives me errors so someone didn't just say "Well use this!" when I already expected it not to work.
Using this:
<input type='button' id='$id' onclick=ed(this.id) value='Edit' />
Allows me to send the value in $id to the javascript function because it was saved in the id field. Something along those lines is what I'm hoping for, but I'm unable to find if there's any way to do that. edit: for 3 variables.
Edit again:
Using:
<form><input type='button' name='$name' id='$id' title='$number' onclick='ed(this.name,this.title,this.id)' value='Edit' /></form>
sent the values of all 3 php variables to the javascript function.
It's not pretty, but it works.
For custom attributes define data-attributes, e.g. as shown on Mozilla Dev:
<div id="user" data-id="1234567890" data-user="johndoe" data-date-of-birth>John Doe
</div>
var el = document.querySelector('#user');
// el.id == 'user'
// el.dataset.id === '1234567890'
// el.dataset.user === 'johndoe'
// el.dataset.dateOfBirth === ''
el.dataset.dateOfBirth = '1960-10-03'; // set the DOB.
// 'someDataAttr' in el.dataset === false
el.dataset.someDataAttr = 'mydata';
// 'someDataAttr' in el.dataset === true
In your case, it seems you only need data-number as the other two are standard properties of the DOM-element.
I think the problem is with your quotes so try this
print '<td><form><input type="button" name="edit" id="edit" onclick=ed('.$name.','.$number.','.$id.') value="Edit" /></td>'
You can only send 3 value like this:-
onclick="javascript:func_name('<?php echo $var1 . "," . $var2 . "," . $var3;?>');
In the js function take only one argument and then split 3 values using "," as a delimiter.

Parameters in Javascript using jquery

I'm trying to pass a parameter from php into my javascript function inside html. Is this at all possible? This is what I've got so far, but it seems to crash once it hits the condition:
$str="<input type='submit' value='-' onclick='Call(".$row['field1'].");'/>";
I hope that I won't have to find a work around for this.
Thanks.
EDIT:
This is the function that I'm trying to call
function Call(stuff)
{
alert(stuff);
$.get('reports.php',
{'param':'section', 'text':stuff},
function(returned_data)
{
alert(returned_data);
});
//alert('end');
}
And this is the function that I'm populating a table with.
function PopTable()
{
alert('end');
document.getElementById('table').innerHTML = 'Loading...';
$.get('reports.php',
{'param':'getstuff'},
function(returned_data)
{
document.getElementById('table').innerHTML = returned_data; // Clear the select
});
alert('end');
}
This is the php that I'm sending back population the table:
$str.= '<tr>';
$str.='<td bgcolor="#ffffff">' . $row['stuff'] .'</td>';
$str.='<td><input type='submit' value='-' onclick="Call('$row['stuff']');"/></td>';
$str.='</tr>'; //accumulate table
I can't seem to get a return value for Call(), and the alert doesn't even pop up
Try:
$str='<input type="submit" value="-" onclick="Call(\''.$row['field1'].'\');"/>';
I would bet you need quotes around the value if it is a string value
For example if $row['field1'] = 'test'; then:
Your version: <input type='submit' value='-' onclick='Call(test);'/> which would fail because test is not a valid variable
My Version <input type="submit" value="-" onclick="Call('test');"/> which would work becase 'test' is a string
What you're trying to do is possible, whereas it is not possible to pass a parameter from JavaScript into a PHP function.
When you say it crashes once it hits the condition, do you mean when you click on the input on the page? In that case, it's an error in your JavaScript syntax. I would try using Firebug with Firefox to track down the issue.
My first guess is there are no quotation marks inside the Call() method. So you're doing this:
Call(something)
and it should be like this:
Call('something')
This is possible, but I would be very careful about mixing PHP echos and javascript inline with strings because you need to escape javascript datatypes properly
In your example, $row['field1'] is probably from a database, so it's a string, so you need to surround the value with quotes in your javascript call. But that's not all, because what if there's a quote in your string, or a special character like a newline which needs to be escaped in javascript? And then what about html escaping?
A better approach is to import your javascript values in one place using json_encode(), then use your variables from there.
E.g.:
<?php
$jsonrow = json_encode($row);
?>
<script type="text/javascript">
var jsrow = <?=htmlspecialchars($jsonrow);?>;
</script>
<?php // some time later... ?>
<input type="submit" value="-" onclick="Call(jsrow.field1);" />

Categories