How to make my simple alert window work - php

i'm currentrly coding in php and when i click a button the message should me Would you like to delete this entry?
This is where I'm currently at
<button onClick="alert('Sure to delete entry?')">Remove Entry</button>
And sure it works. But when I press "x" to close the alert window it still deletes the entry.
How can you make a popup window that has the "Cancel" opition?
I would really appreciate the help! :)

if (confirm("Your question")) {
// do things if OK
}
Use confirm
Click here for examples

You can do this using the following JavaScript code:
if (confirm('Sure to delete entry?') == true) {
//write your code here to delete
}

The HTML
<a href="delete.php?delete=<?php echo $row['guestbook_id'];?>"
onclick="return areYouSure()">Remove Entry</a>
And the JS
function areYouSure()
{
var con = window.confirm("Are You Sure?");
if(con)
{
return true;
}
else
{
return false;
}
}

Related

Full screen mode in php

I developed online exam for education company.I want to make my exam open on full screen mode.I tried several codes but don't work.Any help?
I disabled right click and f12.
You will need to use Javascript.
You can add a button or link that calls goFS() in it's href to trigger fullscreen in browser.
<script>
function goFS(){
if (fs.requestFullscreen) {
fs.requestFullscreen();
} else if (fs.webkitRequestFullscreen) {
fs.webkitRequestFullscreen();
} else if (fs.mozRequestFullScreen) {
fs.mozRequestFullScreen();
} else if (fs.msRequestFullscreen) {
fs.msRequestFullscreen();
}
}
</script>

javascript alert before php code

page:content display.php
<td width="100"><a onclick="return confirmSubmit()" href="delete.php?id=<?php echo $row['id']; ?>&table=labour_details&return=content_display.php"><img src="../images/delete.png" border="0" alt="delete"></a></td>
page:delete.php
if(isset($_GET['id']) && isset($_GET['table']))
{
$id=$_GET['id'];
$tablename=$_GET['table'];
$return=$_GET['return'];
if($tablename=="labour_details")
{
$sql=mysql_query("SELECT * FROM `labour_details` WHERE `id`='$id'");
$row_1=mysql_fetch_array($sql);
$labour_name= $row_1['labour_name'];
if($labour_name!="")
{
$str=mysql_query("DELETE FROM `labour_details` WHERE `id`='$id'");
if($str)
{
header("location:$return?msg=Record Deleted Successfully!&id=$id");
}else{
echo "Problem in deleting :";
}
}
}
}
my problem is where to add alert so that before deleting record it show javascript massage if allow then it will delete record.
function confirmSubmit()
{
var agree=confirm("Are you sure you wish to Delete this Entry?");
if (agree)
return true ;
else
return false ;
}
This line will show confirm dialogbox before redirect.
<a onclick="return confirm('Are You sure?')">...</a>
First you need to create the confirmSubmit() function
<script>
function confirmSubmit()
{
if (confirm('Are you sure you want to delete this?'))
{
return true;
}
return false;
}
</script>
Then you attach that to your A tag, as you have done..
blah
You're probably looking for confirm.
Then, the body of the confirmSubmit function would look something like:
function confirmSubmit() {
return confirm ("Are you sure you want to do this?");
}
This works due to how event handlers work in javascript. Confirm returns true when the user clicks OK and false when they click Cancel.
By returning a true/false value from an event handler function you tell the window whether to keep processing that event. Returning false (which happens when the user clicks Cancel) tells the browser to stop processing the event and not follow the link.
<td width="100"><a onclick="confirmSubmit(id)"><img src="../images/delete.png" border="0" alt="delete"></a></td>
function confirmSubmit(delete_id)
{
var r=confirm("Are you sure you want to delete?");
if(r==true)
{
window.location.href = "delete.php?id="+delete_id"; ?>&table=labour_details&return=content_display.php"
}
}
<a onclick="return confirm('Are you sure?')" href="delete.php">delete</a>

javascript delete confirmation before deleting

I am displaying a JavaScript confirmation dialog that asks if the user is sure they wish to delete the record. However, even when the user clicks "yes" the query is still executed and the record is still deleted what can be the problem here is the code:
<script>
function deleletconfig() {
var del=confirm("Are you sure you want to delete this record?");
if (del==true){
alert ("record deleted")
} else {
alert("Record Not Deleted")
}
}
</script>
So even if I click cancel the query/record gets deleted. How can I stop this from happening what am I doing wrong? Still a newbie at JS!:(
You must use the return value of the confirm dialog:
echo"<form method = \"post\" action =\"change.php?change=$productid\">";
echo// form fields here!...
echo"<input type=\"submit\" name = \"delete\" value=\"Delete\" onclick=\"return deleletconfig()\" />";
if (isset($_POST['delete'])){ //delete clicked
//get variables here
//run query delete record from xyz where id=$id
}
<script>
function deleletconfig(){
var del=confirm("Are you sure you want to delete this record?");
if (del==true){
alert ("record deleted")
}else{
alert("Record Not Deleted")
}
return del;
}
</script>
See the changes in "onclick" and "deleletconfig".
Create a function and call it:
<script type="text/javascript">
function confirmation() {
return confirm('Are you sure you want to do this?');
}
</script>
Delete
Just copy and paste
it gives an alert box to confirm whatever you want your link to do.
Alert returns true or false based on selected answer, and then will either do what you've set your link to do or will not do what your link is supposed to do.
<td>
<a class="delete_button" href="#">Delete</a>
</td>
<script type="text/javascript">
$('.delete_button').click(function(e){
var result = confirm("Are you sure you want to delete this user?");
if(!result) {
e.preventDefault();
}
});
</script>
Instead of
if (del==true){
do
if(del) {
add return = true for deletion, return = false for no deletion.

Javascript confirm & while looped echo

I have a small problem, I made a delete button with a PHP while loop which looks like this:
while($something = mysql_fetch_array($sql_something)){
$id = $something['id']
echo '<button onclick="delconfirm()">Delete</button>
}
this echo's a few delete buttons for some content. However I need user confirmation for deleting first, this is where onclick="delconfirm()" comes in.
my confirm looks like this:
function delconfirm()
{
var r=confirm("Are you sure you want to delete this content?");
if (r==true){
// ...do nothing i guess? it needs to redirect using the PHP echo'd link...
}
else{
window.location = "edit.php";
}
}
However, whether you press cancel or ok, it'll delete it anyway. How can I fix this?
Change it to this:
while($something = mysql_fetch_array($sql_something)){
$id = $something['id']
echo '<button onclick="return delconfirm();">Delete</button>
}
And then your function:
function delconfirm()
{
return confirm("Are you sure you want to delete this content?");
}
EDIT: If you want a more unobtrusive solution:
while($something = mysql_fetch_array($sql_something)){
$id = $something['id']
echo '<input type="button" value="Delete" data-id="$id" />';
}
And then some javascript to bind the event:
function bindButtons() {
var buttons = document.getElementsByTagName("input");
for (var i = 0; i < buttons.length; i++) {
if (buttons[i].type == "button") {
buttons[i].onclick = function () {
location.href='somewhere.php?id=' + this.getAttribute("data-id");
}
}
}
}
and bind it to the window.onload, as per Ian suggestion:
window.onload = bindButtons;
Note: If you were using jQuery this solution would be easier and more elegant.
Working jsFiddle
If the user presses cancel then you need to stop the event from doing what it would normally do. Try this, for example:
function delconfirm(e) {
e = e || window.event;
if (!confirm("Are you sure you want to delete this content?")) {
e.preventDefault();
// This will prevent the event from bubbling up to the <a>.
e.stopPropagation();
return false; // For the ancient/crappy browsers still out there.
}
return true;
}
You need to stop/delete the current click event. After your code is executed the event sinks to the anchor and triggers a click. With MooTools just add 'new Event().stop();'. I think jQuery has also something like this.
EDIT: Hanlet EscaƱo is right. You can return true (the browser will redirect to the URL in the href, or false to let the browser do nothing)
In order to prevent to the HTML link to work, you have to return false in your js function or event.preventDefault() where event is an argument which is passed to the click event function
I did thin when putting a click event on the a element and not on an element inside the a tag. But it might work.

call php page under Javascript function

Is it Possible to call php page under Javascript function?
I have an javascript function and I want to call php page if someone press okk
Here is my code so far
function show_confirm()
{
var r=confirm("Do You Really want to Refund money! Press ok to Continue ");
if (r==true)
{
alert("You pressed OK!");
return true;
}
else
{
alert("You pressed Cancel!");
}
}
and this Js function i m using here
<td align="center"><input name="Refund" onclick="show_confirm()" type="submit" value="Refund" /></td>
now i want if user press okk then call other php page ...
if you want to redirect to a page:yourphppage.php if the user pressed ok.
function show_confirm()
{
var r=confirm("Do You Really want to Refund money! Press ok to Continue ");
if (r==true)
{
window.location="yourphppage.php";
return true;
}
else
{
alert("You pressed Cancel!");
}
}
If you are looking for page direction:
window.location="index.php";
There are plenty other methods to redirect: http://grizzlyweb.com/webmaster/javascripts/redirection.asp
Place this where you want your redirect to happen:
location.href = "newpage.php";
Checkout
http://www.w3schools.com/xml/xml_http.asp
Then have a look at
http://mootools.net/docs/core/Request/Request
Any of the JS libraries prototype Jquery etc. have a wrapper to make requests and your life easier.
Then just have a button and set its onClick to be the request call to the server and listen for the response.
Some tips you can append data to the request URL to pass your PHP script variables.
Checkout
http://www.w3schools.com/tags/ref_urlencode.asp
If you only want to redirect the page to the "refund" page without using AJAX you can do something along the lines of the following:
(assumes $transaction_id variable is the id number you are looking to refund)
HTML Lines:
<td align="center">
<input name="Refund" onclick="show_confirm(<?php echo $transaction_id ?>)" type="submit" value="Refund" />
</td>
Javascript lines:
function show_confirm(transaction_id)
{
var r = confirm("Do You Really want to Refund money! Press ok to Continue ");
if (r == true)
{
alert("You pressed OK!");
location.href = "refund.php?transaction_id="+transaction_id;
return true;
}
else
{
alert("You pressed Cancel!");
}
}

Categories