Laravel 5.6, Form submit does nothing but page reload - php

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).

Related

How to run a function through my plugin page and print the result on that same page

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.

Laravel: Deleting something from database doesn't work

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?

embed html form to php code

I have a form on on html outside of php...
<form method="post" action="">
<input type="text" name="user"/></br>
<input type="submit" value="submit" name="login"/>
</form>
then call submit button from php and do this
if(isset($_POST["login"]))
{
print <<<this
<form method="post" action="">
<input type="submit" name="apply"/>
</form>
this;
if(isset($_POST["apply"]))
{ print "it works";}
}
Alright, so the problem is that, "it works" won't print from the second form thats inside the php. it just takes me back to where i came from. Perhaps it's a dumb question, please help though! thanks
The problem is that by the time you're checking if(isset($_POST["apply"])) the login condition becomes invalid because everything is inside the if(isset($_POST["login"])).
Try taking the if(isset($_POST["apply"])) outside the login IF.
Your "apply" code exists only INSIDE the login test code. When you submit that second form, there will be NO login form field, because you didn't include an input/textarea of that name in the second form. So the second form submits, there's no login, and the entire inner code never gets executed. You probably want:
if(isset($_POST["login"]))
{
print <<<this
<form method="post" action="" name="apply">
<input type="hidden" name="login" value="foo" /> <!-- add this line -->
etc...
I'm not sure to understand what you wanna do with this code but you obviously missed some details :
_You did not set the "action" field in your form tag, so I don't understant how you would like the PHP file to get called ?
_Your code if(isset($_POST['login'])) has no sense, you are testing the existence of a value sent by a validation button, you'd rather whrite isset($_POST['user'])
Hoping to have helped you
Your variables are declared in 2 forms, so there will be 2 calls (completely independant) to your php.
So you could have a second submit button inside your second form:
if(isset($_POST["login"]))
{
print <<<this
<form method="post" action="">
<input type="submit" name="apply" value="Second"/>
</form>
this;
}
if(isset($_POST["apply"]))
{ print "it works";}

POST datas not passing Laravel 4

I'm currently tutoring myself on Laravel 4 and I encounter a strange problem:
I have a blade form:
<form action="{{url('/')}}" method="POST">
<input type="hidden" name="foo" value="bar"/>
<input type="hidden" name="baz" value="boo"/>
<input type="submit" value="Send"/>
</form>
and a Route:
Route::any('/', function()
{
$data = Input::all();
var_dump($data);
});
Route::get('post-form',function()
{
return View::make('form');
});
If I use Get instead of Post (in the blade form) everything's running perfectly but when I try to use Post the returned array is empty
Any clues ? :)
(forgive my English since i'm french native)
try to use Route::post instead of Route::any and dd instead of var_dump
Route::post('/', function()
{
$data = Input::all();
dd($data);
});
Edit:
you have a little mistake in your html form:
<form action="{{url('/')}}" method="POST">
it should look like this:
<form action="/" method="POST">
I hope it will work this time
I tried your code out and it works perfectly. There is no problem with it.
I suggest you using Route::post when you are sure sending POST data

Kohana 3.3 request->post() no data

I am having a problem with Kohana 3.3. I cannot get the $_POST values using $this->request->post() on my Controller. I dont know what I did wrong on my code. Hope you can help me out here. BTW, I was able to use Twig on all my templates using Kohana 3.3 but I wasn't able to process the data from my forms. Thank you. :-)
Here is my code:
Controller:
class Controller_Setup extends Controller{
public function action_item_group(){
if (HTTP_Request::POST == $this->request->method()){
// Post has no data
print_r($this->request->post());
}
$this->response->body( Twig::factory('setup/sample_form') );
}
}
View
<form class="form-horizontal" action="item_group" method="post" name="setup_form">
<input type="text" value="">
<button type="submit">Save</button>
</form>
You need to set name or id attributes to your HTML elements. Try this code and see if it's working now:
<form class="form-horizontal" action="item_group" method="post" name="setup_form">
<input name="group_name" type="text" value="">
<button type="submit">Save</button>
</form>

Categories