Is there a piece of Javascript that would stop a form submit button from carrying out a function
I am using the CodeIgniter framework which uses MVC pattern.
So when you click the button it calls a controller function which then calls a model function etc.
I tried using various things in Javascript including go back, history go back. Is there anything that will stop any event happening?
Here is my code:
function confirmation() {
var answer = confirm("Delete Record?")
if (answer)
{
}
else
{ //reloads current page - however it still continues to function
window.redirect("http://localhost/midas/index.php/control_panel/user_records");
}
}
It still continues to do the function if I say "Cancel". Is there any way to stop the browser?
This is my php code:
//js = is a variable which stores the javascript code
echo form_submit('view','View', $js);
This creates html output like so:
<input type="submit" onclick="confirmation()" value="View" name开发者_开发知识库="view">
You need to put the javascript event in your form tag and use onSubmit
instead of onClick
, also you don't need an external js for this.
<form action="url/to/form" onSubmit="return confirm('are you sure?')">
</form>
On the button which is causing the POST.. then put a function call on the OnClick event for that button. Have that function return true to continue with the POST or return false to cancel the action.
function confirmation() {
var answer = confirm("Delete Record?")
if (answer) {
} else {
//reloads current page - however it still continues to function
return false;
}
}
Maybe returning false to the method will stop the form submit. Depends how you're calling the confirmation function.
Try adding return false;
at the end of your else
statement after your window.redirect
.
Edit:
After looking at your onclick
event code I would suggest changing your onclick
to look something like this:
<input type="submit" onclick="confirmation(); return false;" value="View" name="view">
using codeigniter form helper it would be :
echo form_open('url/to/form', array('onsubmit'=>'return confirm(\'use triple escape to escape special chars like \\\'quotes\\\'. \')'));
I create a view helper and do something to this effect
function delete_button($id)
{
$button = form_open(current_url().'/delete');
$button .= form_hidden('id', $id);
$button .= form_submit(array(
'name' => 'delete',
'onclick' => "return confirm('are you sure you wish to delete?');"
), 'Delete');
$button .= form_close();
return $button;
}
then in my view, i'll use <?= delete_button($row->id) ?>
to render the necessary form and submit (delete) button code. since the javascript confirm()
method returns a boolean, we can simply just return
the method since it'll return false
when the user cancels on the confirmation dialog and naturally a click event that returns false;
will prevent it from proceeding to the form action.
PHP code:
$attributes = array('id' => 'formId','onsubmit'=>'return validate()');
echo form_open('controller', $attributes);
where validate() is the javascript function being called on submit. And if it return false, form will not submit. :-)
精彩评论