Welcome to WebmasterWorld Guest from 54.167.209.198

Forum Moderators: open

Message Too Old, No Replies

Dialog box

     
10:03 am on Aug 5, 2009 (gmt 0)

Junior Member

5+ Year Member

joined:Jan 21, 2009
posts: 69
votes: 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.

2:11 pm on Aug 5, 2009 (gmt 0)

Junior Member

10+ Year Member

joined:Dec 7, 2004
posts:130
votes: 0



var result = confirm('Click button');

result now contains the true or false;

HTH, Tom

2:29 pm on Aug 5, 2009 (gmt 0)

Junior Member

5+ Year Member

joined:Jan 21, 2009
posts: 69
votes: 0


Thanks,but how do I get the result in php?
4:11 pm on Aug 5, 2009 (gmt 0)

Senior Member

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

joined:Nov 28, 2004
posts:7999
votes: 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]

4:25 pm on Aug 5, 2009 (gmt 0)

Junior Member

5+ Year Member

joined:Jan 21, 2009
posts: 69
votes: 0


OK,I'll see how it works...
4:51 pm on Aug 5, 2009 (gmt 0)

Junior Member

5+ Year Member

joined:Jan 21, 2009
posts:69
votes: 0


by the way,what does "this" stand for in onsubmit?
8:11 am on Aug 6, 2009 (gmt 0)

Junior Member

10+ Year Member

joined:Dec 7, 2004
posts:130
votes: 0


'this' is the form element.

Tom

3:21 pm on Aug 6, 2009 (gmt 0)

Senior Member

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

joined:Nov 28, 2004
posts:7999
votes: 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.

5:18 pm on Aug 6, 2009 (gmt 0)

Junior Member

5+ Year Member

joined:Jan 21, 2009
posts: 69
votes: 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...

3:31 pm on Aug 7, 2009 (gmt 0)

Senior Member

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

joined:Nov 28, 2004
posts:7999
votes: 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.

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members