homepage Welcome to WebmasterWorld Guest from 54.226.10.234
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Dialog box
Mister_L

5+ Year Member



 
Msg#: 3965994 posted 10:03 am on Aug 5, 2009 (gmt 0)

Hi,

I have the following button that displays a dialog box when you click it:

<input type="button" name="delete_product" value="Delete" onClick='confirm("Are you sure you want to delete this product?")' />

I understand that if I click OK some variable is set to TRUE,and if I click Cancel the variable is set to FALSE.
But what variable exactly?

Can I get the result in a php variable?

Thanks.

 

Arno_Adams

10+ Year Member



 
Msg#: 3965994 posted 2:11 pm on Aug 5, 2009 (gmt 0)


var result = confirm('Click button');

result now contains the true or false;

HTH, Tom

Mister_L

5+ Year Member



 
Msg#: 3965994 posted 2:29 pm on Aug 5, 2009 (gmt 0)

Thanks,but how do I get the result in php?

rocknbil

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3965994 posted 4:11 pm on Aug 5, 2009 (gmt 0)

Your Javascript is what's called "progressive enhancement." If JS is disabled, your php should manage it by "graceful degradation."

So what's that mean?

You don't need the JS confirm value passed to PHP. You already have it in your form.

<form action="yourscript.php" onSubmit="return confirm_delete(this);">
<input type="submit" name="delete_product">
<!-- use submit, not button, buttons have no inherent action and will not work if JS is disabled -->
</form>
<script type="text/javascript">
function confirm_delete(form) {
var result = confirm('Are you sure?');
if (result == true) { form.submit(); }
return false;
}
</script>

Then in your PHP,

if (isset($_POST['delete_product'])) {
// delete it
}

What is happening:

- By passing the form object to the function and returning false, it tells the browser to NOT perform it's natural action, submit the form. Return false allows JS to manage the submit of the form.

- If JS is disabled, the above will be ignored and the form will still work.

- A checkbox value will not be present in $_POST/$_GET if it is not checked, and a button or submit value will not be present if it is not clicked. So use this value to operate on your form. This gives you the redundancy you need for graceful degradation - works with or without JS.

However, you may want multiple products on a page, and probably have the product ID in a hidden field. I like to do something like this:

// As you generate the product list, get an id

$id = 1234;
<input type="submit" name="delete_1234">

foreach ($_POST as $key=>$value) {
if (preg_match('/delete\_\d+/',$key) {
($tag,$id) = explode('_',$key);
$select = "delete from table where record_id=$id";
}
}

EXAMPLE ONLY, be sure to cleanse input and never use $_POST or $_GET directly in your programs

So you get the best of both worlds and your page and PHP is no longer Javascript dependent.

[edited by: rocknbil at 4:45 pm (utc) on Aug. 5, 2009]

Mister_L

5+ Year Member



 
Msg#: 3965994 posted 4:25 pm on Aug 5, 2009 (gmt 0)

OK,I'll see how it works...

Mister_L

5+ Year Member



 
Msg#: 3965994 posted 4:51 pm on Aug 5, 2009 (gmt 0)

by the way,what does "this" stand for in onsubmit?

Arno_Adams

10+ Year Member



 
Msg#: 3965994 posted 8:11 am on Aug 6, 2009 (gmt 0)

'this' is the form element.

Tom

rocknbil

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3965994 posted 3:21 pm on Aug 6, 2009 (gmt 0)

by the way,what does "this" stand for in onsubmit?

In this case it's the form, yes, but the "this" keyword refers to any element to which it's attached. So, for example,

<form action="" id="myForm">
<input type="button" id="myButton" onClick="return someFunction(this, this.form);" value="Test Me">
</form>

<script type="text/javascript">
function someFunction(buttonObject, form) {
alert(buttonObject.id);
// the above should alert 'myButton' ....;
alert(form.id);
// ... then should be followed by an alert 'myForm';
form.submit();
return false;
}
</script>

In the above case, "this" would refer to the button, but the button is contained in a form. To properly refer to the form from an element within it, you have to use this.form.

Mister_L

5+ Year Member



 
Msg#: 3965994 posted 5:18 pm on Aug 6, 2009 (gmt 0)

I've tried the first code:

<form action="yourscript.php" onSubmit="return confirm_delete(this);">

<input type="submit" name="delete_product">
<!-- use submit, not button, buttons have no inherent action and will not work if JS is disabled -->
</form>

<script type="text/javascript">
function confirm_delete(form) {
var result = confirm('Are you sure?');
if (result == true) { form.submit(); }
return false;
}
</script>

It didn't even pop a dialog box when I clicked...

rocknbil

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3965994 posted 3:31 pm on Aug 7, 2009 (gmt 0)

Did you paste it in a document with nothing else in it?

Valid doctype, no validation errors?

Do you have any ad blocking extensions running that might block the confirm?

Are you using IE, and have the active scripting thingie disabled? (When the test page loads LOCALLY - on your computer - a yellow bar should drop down with a warning you must right-click and set to allow scripting. Doesn't happen on a remote server.)

Works perfectly in the two main browsers here, no reason it shouldn't.

While I usually type code right out in posts and sometimes have errors, I just did a re-test of the code you posted into a blank document, works fine.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved