confirmation dialog when addressing a function - php

I have a function specified in html. This function is called through onclick on a table cell. Normally I would also use onclick to create a confirmation dialog to ask if the person is sure to delete the row of data in this case.
I'm wondering if there is any way to go about this and still have the confirmation box pop up to link to the function. I'm pretty much out of ideas.
little piece of code to show what I mean with addressing my function.
echo "<td class='option' onclick=\"deleteEvent($eventID)\">Delete</td>";

You can accomplish this via the window.confirm API.
Example:
if (window.confirm("Do you really want to delete?")) {
//Do the Delete Operation
}

Related

How to put function to this block of PHP code?

i'm just beginner and i want to call function which will be create the "sub buttons" when button is clicked, how i can do it from this block in echo? help pls
<?php function btncreate($filename) {
echo "<button class='m-1 btn btn-outline-success'>$filename</button>";
}
?>
P.S. i need to do it at the same page with the general buttons
i was trynna to do it by using onclick, putting the JS code inside the echo
First thing first, why do you need to create UI using backend language like that's mostly a Front end language job, Secondly even if you want to do it like this then I say you should use AJAX but that still requires you the combination of Front END with Backend, and if you are looking for a simple button creation upon an event and then save/retrieve something for that button in the backend then I think we can start with JavaScript to create the button on an event (i.e. CLICK) and upon its creation just run an ajax request to save/retrieve whatever you want from that.

PHP: Confirm Box before deleting

I want to add a button on my website, where a User can delete his Account. Unfortunately I don't know how to realize it...
my Code so far:
Javascript:
<script language = "JavaScript" >
function delete(id) {
if (confirm("Do your really want to delete your account?"))
{
header("refresh:1;url=intern.php?act=account");
}
else
{
}
}
</script>
my .html file:(there are no tags like html title head... it begins with ?php..)
<td></form><input type='submit' name='deleteuser' value='Delete Account' onClick='return delete()'/></form></td>
Also i have an if clause in the .html file:
if(isset($_POST['deleteuser'])) {
if(delete() == true){
delete_user;
}
else{
header("refresh:1;url=intern.php?act=account");
}
}
The Button is there and when I click on it, it asks me if I'm sure to delete my account, but afterwards I got an error: "Fatal Error:Call to undefined function delete() "
I have a stored procedure named: sp_deleteAccount. In my config.php I declared it as:
$SQL_delete_user = "CALL sp_deleteAccount('";
Now I don't know how to bind that stored procedure in the code so that the Account will be deleted after pressing "Yes I want to delete my Account".
Hope I didn't miss anything and someone can help me
JOP
In this portion you're calling a php redirect(i think?) in javascript without php open tag so that's not going to work. Instead you can use a javascript reditrect if the 'if' statement returns true(yes) then redirect to a url with a get variable of delete or something, see below.
edit -- you'll probably want the id as well so i made adjustments. PLus in the onclick in the form you'll need to pass the id, unless it's stored in a session variable or something, in which case you don't need to pass it into the url. your sql should end with "WHERE id=" then just tack the id onto the query. This is just a simple example to get you started, always be cautious of sql injection, but i'll leave preventing it up to you.
<script language = "JavaScript" >
function deleteUser(id) {
if (confirm("Do your really want to delete your account?"))
{
window.location.href= 'intern.php?delete=true&id='+id;
}
else
{
window.location.href = 'intern.php?act=account';
}
}
</script>
next in intern.php check for the get variable
if(isset($_GET['delete'])) {
$mysqli = new mysqli(connection variables here);
$mysqli->query($SQL_delete_user.(int)$_GET['id']);
}
give that a try, rearrange the code as you like but that should get it done.
as for the error, you can't use the keyword delete for a function name. One last thing, for this to work make the input type "button"
I am not sure there might be a way to achieve it using the approach you are trying , but I am not aware of it. For this I would typically use an ajax call to an url, on the click event of the OK button in the jquery-ui Dialog. And then process the logic and on success create another dialog for confirmation.
I searched and struggled with this issue until I did a little lateral thinking.
I used JavaScript to show a hidden div containing the Yes/No options.
Then an onClick around the Yes option which loaded the php script into a hidden iFrame.
The onClick around the No option simply hid the div and did nothing else.
A bonus is being able to style the div any way I wanted, show and hide it with an effect and place it exactly where it looked best.

Trigger javascript function through PHP

I'm trying to call a JavaScript function through PHP and have met some problems. I have got three code snippets for your understanding:
1) My javascript function:
function addPoints(radiobutton){
//code
}
The parameter is an actual button and inside the function is a lot of code reading button value and name and taking care of checked status of the button.
2) My php-code creating the button looks like this. Notice that i send 'this' to the function.
echo "<input type=\"radio\" name=\"X\" value=\"Y\" onClick=\"addPoints(this)\"/>";
3) Finally I have this code at the very end of the document for triggering the javascript function when page is loaded.
echo "<SCRIPT LANGUAGE='javascript'>addPoints();</SCRIPT>";
If addPoints only consisted of an alert, this would work. But my problem is that I need to send an actual button as parameter to the function. I need both triggering the function on page load (to load some data from a database) and the normal button onClick-event.
Is there any solution for this if I don't use another server request to catch the desired button? It's important that I get the button created above (in fact I've got a lot of buttons, but let's think of is as one) and send it as parameter.
Give the button to pass to the function on load an id="initial_radio_button" attribute, and then make your last snippet:
echo "<script type='text/javascript'>addPoints(document.getElementById('initial_radio_button')‌​);</script>";

help with javascript confirmation dialog

So here's my situation: I have a form that validates with PHP. I want to make it so that if the form fails validation, the user is forced to click through a confirmation dialog before they navigate to another page (the form is fairly large and they don't want to accidentally leave it before it's saved). I'm going about this like so:
see updated function below,
Basically use php within the function to either set the body to present the confirmation or do nothing depending on the error status of my form. Nothing happens when the form isn't submitted and I click a link, good. When the form is displaying errors and I click a link the confirmation dialog will appear but canceling it causes it to reappear. If I cancel it a second time the page request will go through even though it's not supposed to. I'm not that familiar with javascript so I'm not sure what's going on. Is there a better way I should be going about this?
Edit: I figured it out, it was a combination of things. The first was a really dumb mistake on my part: I was calling the onlick on both tags AND the tags for each link in my list, hence why the box popped up twice.
the second piece was that even though my function already returns bool, the onclick requires an explicit return declaration. I was doing:
<a onclick="forceConfirm();" href="somepage.html">Blah</a>
When it should have been:
<a onclick="return forceConfirm();" href="somepage.html">Blah</a>
Then just edit the PHP so that forceConfirm always returns true when the form hasn't been submitted, bypassing the confirmation:
function forceConfirm(){
<?php
if($form->errorStatus){
echo 'if(confirm("Are you sure you want to navigate away from this page? All unsaved changes will be lost.")){'."\n".
'return true;'."\n".'}'."\n".
'else{ return false;}';
}
else{ echo 'return true;';}
?>
}
Now I just need to figure out how to use jQuery to apply this to all links without having to put onclick events all over the place....
You can use confirm() like this:
if(confirm("Are you sure you want to proceed?")){
document.location = 'confirmed redirect url...';
}
else{
document.location = 'cancel redirect url...';
}
Then you'd wrap that in the same PHP block as in your example, displaying it if necessary and hiding it if not.

Challenging question about html form

This is the whole source code, i would like to modify it to add a new column to show the
Client Mobile
Client Office Telephone
Client E-mail
in an another popup php pages.
What i have attempted is to add a form and a submit button to create a new column , when i press that submit button, the mobile, office,email information will post to another php page to print out. however, since there is a another form exist already, when i add a form ,the function in the following form will not work. (Don't worries , i will indicate where the problems happen.)
it is a complicated question. Thanks in advance
The source code is here:
https://docs.google.com/leaf?id=0B196kQ-9lu50OTI1NDZkMjktNzAzNi00MmM0LWIzMjgtNTQxMTIyZmYyM2I1&hl=en_US
the problem is at line 99
p.s. I just found out the form method can not get my job done, since it can not create a popup window for the information at the same time.
Looks like at line 126 there is an extra </form> tag. Delete it...that might help.
EDIT:
Ok. So it needs to look something like this:
$_SESSION['echoable'] = $aVar; //put any variable you want here, such as POST data
Then, in the popup window, write:
EDIT: forgot to say, add this to the TOP of your page.
session_start();
Then do this stuff later on.
if((isset($_SESSION['echoable'])) && (!empty($_SESSION['echoable']))) {
echo $_SESSION['echoable'];
}
else {
echo "whatever"; // whatever you want here
}
The above will print whatever var you want. Hopefully this helps...if not let me know.

Categories