I have an app where I store captions for images. I have a delete function for captions that are routed like this:
Route::delete('caption/{id}', 'DetailController#deleteCaption');
to this function:
public function deleteCaption(Request $request, $id) {
$caption = Caption::findOrFail($id);
$caption->delete(); //doesn't delete permanently
}
unfortunately, as you can see in my comment, this function does not achieve its purpose of deleting the caption. It shows up again and again in the admin page upon pressing the delete button and reloading.
Admin page:
<p value='{{$caption->id}}'>{{$caption->content}}</p>
<form action="caption/{{$caption->id}}" method="delete">
<button type="submit">Delete caption</button>
</form>
<form action="caption/{{caption->id}}" method="post">
<button type="submit">Accept caption</button>
</form>
What can I do about this?
Related
I'm currently trying to add a debug page to my website. This page is simply dedicated to running some of the tasks done by my plugin and outputting some results on the same page.
I currently have a separate page in my Wordpress admin, this page contains a form and button that should be linked to a function that will do various steps and then return a value that must be printed on that page.
Here's the current code for the page and button :
function actu_admin_menu_option()
{
add_menu_page('Scripts', 'Sahar actus plugin', 'manage_options', 'actu-admin-menu', 'actu_scripts_page', '', 200);
}
add_action('admin_menu', 'actu_admin_menu_option');
// HTML page of the plugin
function actu_scripts_page()
{
?>
<div class="wrap">
<h2>Sahar actus plugin</h2>
<form action="<?php echo admin_url('admin-post.php'); ?>" method="post">
<input type="hidden" name="action" value="start_test">
<input class="button button-primary" type="submit" value="Start test">
</form>
</div>
<?php
}
function start_test()
{
return ("test started!");
}
add_action( 'admin_post_start_test', 'start_test' );
What I would like is for example to run the following function when clicking on the button and then outputting the returned value on the page.
function start_test()
{
return("test started!");
}
So when I press the button I want "test started!" to be printed on the page, currently clicking on the button redirects me to /wp-admin/admin-post.php which is a blank page.
I have no idea what is the best course of action to do it, should I make an ajax request on my button to run the function and then get the return value from that ajax call ? Or is there a better way to go about this ?
Thanks for helping me, have a great day
EDIT : Updated code, no errors but output is still not printed to page or console.
function actu_scripts_page()
{
?>
<div class="wrap">
<h2>Sahar actu plugin</h2>
<form action="" method="post">
<?php wp_nonce_field('do_test', '_test_nonce') ?>
<input type="hidden" name="action" value="start_test">
<input class="button button-primary" type="submit" value="Start test">
</form>
</div>
<?php
if (isset($_POST['start_test'])) {
if (isset($_POST['start_test'])) {
if (!wp_verify_nonce($_POST['_test_nonce'], 'do_test')) {
// error in nonce
} else {
start_test();
}
}
}
?>
<?php
}
function start_test()
{
echo("hhhhh");
die(); // tried with and without, no difference
}
add_action('admin_post_start_test', 'start_test');
There are two ways to acheive this, both are fine, it just depends on the user experience you desire. Use the normal http form submission (what you have already), or use ajax. The normal form submission is the easiest method.
Submit the form using the standard method (what you have there), which will reload the page. Important Note it would be better to leave the action attribute blank like action="" if you are submitting the page to itself.
You will also want to add a nonce to the form using wp_nonce_field() and check its value in the submission block with wp_verify_nonce().
So your form would like something like this:
<form action="" method="post">
<?php wp_nonce_field('do_test', '_test_nonce') ?>
<input type="hidden" name="action" value="start_test">
<input class="button button-primary" type="submit" value="Start test">
</form>
Add a php block to check if the form has been submitted and do your script in there, printing any output with php.
function start_test() {
// do things. If successful, return true. Otherwise return false
return true;
}
if( isset($_POST['start_test']) ) {
if( ! wp_verify_nonce( $_POST['_test_nonce'], 'do_test' ) {
// error in nonce
} else {
if( start_test() )
echo '<p>Success!</p>';
else
echo '<p>Failure!</p>';
}
}
There is a redirection happening through /wp-admin/admin-post.php that will return you back to the previous screen.
Your results are being printed on this page, and since it's redirecting, you don't get to see anything.
All you need to do to solve this issue, is add this after your debugging code:
die();
Or
exit();
This will stop the PHP from executing anything after your code. Thus, stopping the page from redirecting. Also, any functions that will run after you function, will not run normally, so if you're expecting saving for example, it won't happen after this piece of code.
My problem is that when a form is being submitted just reloads the page without doing anything.
My route looks like this:
Route::post('/subscription/cancel', 'SubscriptionController#cancelSubscription')->name('cancel');
My form:
<form class="subscription-action--forms" action="{{ route('cancel') }}" method="post">
#csrf
<input id="cancel-input" type="submit" value="Cancel Subscription">
</form>
And finally my SubscriptionController cancelSubscription() method, which is not being executed:
public function cancelSubscription() {
if (auth()->user()->isSubscribed()) {
auth()->user()->subscription('Subscription to Ebooks.am')->cancel();
return redirect('/subscription')->with('info', 'Canceled!');
}
return redirect('/subscription');
}
The interesting thing is that this code worked just fine until some point (maybe due to a change in other files, don't know).
Before i make this question i use javascript method to prevent multiple submit on my blade template. But i know it's client side that still possible to get attack by.
This is my javascript code
<script>
function submitForm(btn) {
// disable the button
btn.disabled = true;
// submit the form
btn.form.submit();
}
</script>
<input id="submitButton" type="button" value="Submit" onclick="submitForm(this);" />
my question is, is there another way to prevent without client side in laravel?
The most straightforward way to guarantee the uniqueness of a form submission (In the sense of stopping someone mashing submit twice) is to generate a random token and storing it in a session AND a hidden field.
If it doesn't match, reject the form, if it does match, accept the form and nuke the session key.
OR
Force Laravel to regenerate a new session token after each time a token is verified correctly. (Easy Way Out)
To achieve this, create a new function tokensMatch() in app/Http/Middleware/VerfiyCsrfToken.php (which will overwrite the inherited one). Something like this:
protected function tokensMatch($request)
{
$tokensMatch = parent::tokensMatch($request);
if ($tokensMatch) {
$request->session()->regenerateToken();
}
return $tokensMatch;
}
In case you validate the form and the validation fails, the old data will be passed back to the form. So you need to make sure not to pass back the old token by adding _token to the $dontFlash array in app/Exceptions/Handler.php
protected $dontFlash = ['password', 'password_confirmation', '_token'];
Step 1: write a class name in the form tag Exp: "from-prevent-multiple-submits"
<form class="pt-4 from-prevent-multiple-submits" action="{{ route('messages.store') }}" method="POST">
#csrf
Step 2:
Write a class in button section
<button type="submit" id="submit" class="btn btn-primary from-prevent-multiple-submits">{{ translate('Send') }}</button>
Step 3:
write this script code
<script type="text/javascript">
(function(){
$('.from-prevent-multiple-submits').on('submit', function(){
$('.from-prevent-multiple-submits').attr('disabled','true');
})
})();
</script>
give id to submit button
<input class="main-btn" id="register" type="submit" value="Make Appointment">
give id to form
<form id="appointment_form" method="post" action="{{route('appointment')}}">
in your js add these
$('#appointment_form').on('submit', function () {
$('#register').attr('disabled', 'true');
});
Step 1: give id to form
<form action="{{ route('web.reports.store') }}" method="POST" enctype="multipart/form-data" id="kt_stepper_form">
Step 2: give id or add class to submit button
<button type="submit" class="btn btn-primary submit-btn" data-kt-stepper-action="submit">
<span class="indicator-label">
Submit
</span>
<span class="indicator-progress">
Please wait... <span
class="spinner-border spinner-border-sm align-middle ms-2"></span>
</span>
</button>
Step 3: and then, you can add some jquery script like this
$('#kt_stepper_form').on('submit', function(){
$('.submit-btn').attr('disabled', true);
$('.indicator-label').hide();
$('.indicator-progress').show();
});
with code above, button will be disabled and show indicator progress when user clicked the button
I have a form that come with action like this.
<form action="edit.php" method="post">
.
.
<button class="btn btn-success" name="submit_mult" type="submit">Edit</button>
</form>
I need this to use for first button, and now I want to use same form for second button this time is for delete, but is because this form action using for "edit.php", so I use formaction="delete.php,this will This overrides the action attribute of the form when you click on that button.
But I want the second button to be show outside of form not inside of form, and I tried to figure out how that works..
Wrote like this...
<div id="delete"><button class="btn btn-success" formaction="Multi_Edit/delete.php" name="submit_mult" type="submit">delete</button></div>
Is not working when I put this button in outside of form, but is works fine inside of form.
How can I solve this to make works that button outside of forms with formaction="delete.php" if this works fine with jquery, how to write that in jquery!
try this
put and id to the form
<form action="edit.php" method="post" id="myForm" >
.
.
and an id to the button
<div id="delete"><button class="btn btn-success" formaction="Multi_Edit/delete.php" name="submit_mult" id="myDeleteButton" type="submit">delete</button></div>
then via jquery handle the form submit
$("#myDeleteButton").click(function(){
$("#myForm").attr('action','Multi_Edit/delete.php');
$("#myForm").submit();
});
You can use the form attribute, form="theForm":
<form action="edit.php" method="post" id="theForm">...</form>
<div id="delete"><button form="theForm" class="btn btn-success" formaction="Multi_Edit/delete.php" name="submit_mult" type="submit">delete</button></div>
See here: http://www.sitepoint.com/the-html5-form-attribute/
This question already has answers here:
How to submit a form to two different pages depending on the button clicked, without javascript
(5 answers)
Closed 9 years ago.
I have a form where I can add a new category item.
<form method="POST" action="backend/categories/form">
<input type="text" name="title" value="" />
<button type="submit">Save</button>
<button type="submit">Save and add new</button>
</form>
What I want to do, is that if i click on Save button, it will process a function in controller and redirect me automatically into the previous page (list of categories page), but whenever I click on Save and add new, it should process the function but reload the same page without redirecting to the page which is defined in controller's function.
Controller:
function form($id){
// Process the form
// ...
// Redirect to the category list page
redirect($this->config->item('backend_folder').'/categories');
}
Any tips to achieve it without using the Javascript ?
Use this HTML:
<form method="POST" action="backend/categories/form">
<input type="text" name="title" value="" />
<button type="submit" name="submitForm" value="formSave">SAVE</button>
<button type="submit" name="submitForm" value="formSaveNew">SAVE AND ADD NEW</button>
</form>
Then check the POST data like this:
$formSubmit = $this->input->post('submitForm');
if( $formSubmit == 'formSaveNew' )
redirect($this->config->item('backend_folder').'/categories/form');
else
redirect($this->config->item('backend_folder').'/categories');
Disclaimer: I didn't try that.
This might be helpful for you:
Use this in your save function in your controller. Immediate after Insert/Update code:
$task = $_POST['submit'];
//echo $task //Save or SaveNew depending on pressed button
switch ($task)
{
case 'Save':
$this->session->set_flashdata('message',$this->lang->line('changes_has_been_saved_successfully'));
$link = redirect('cashdrawer/cashdrawerform/12'); //Link after save button with id
break;
case 'SaveNew':
$this->session->set_flashdata('message',$this->lang->line('this_order_has_been_saved_successfully'));
$link = redirect('cashdrawer/cashdrawerform'); //Link after save and new
//echo $link;
}
redirect($link);
And Change your button as:
<button type="submit">Save</button>
<button type="submit">SaveNew</button>