Spoofing different methods within same form laravel - php

So within a single form I want to be able to change the request type between PATCH and DELETE depending on which button has been selected. Here's my code (simplified for readability):
<form id="edit-task-form" method="POST" action="">
<input type="hidden" name="_method" value="PATCH">
#csrf
<div>
<input type="text" id="modal-task-title" name="task_title" placeholder="Title"/>
</div>
<div>
<textarea name="task_desc" id="modal-task-desc" placeholder="Task description." rows="5"></textarea>
</div>
<div class="input flex-center-parent">
<input type="button" value="Delete" id="delete-task"/>
<input type="submit" value="Save" id="save-task"/>
</div>
</form>
I'm thinking I could just change the value onclick() with javascript but wondering if anyone knows a more laravel-like way for doing this?

You could use HTML buttons instead of inputs and set the name attribute of the buttons to _method allowing you to have form values assigned to them.
<div class="input flex-center-parent">
<button type="submit" name"_method" value="DELETE" id="delete-task">Delete</button>
<button type="submit" name"_method" value="PATCH" id="save-task">Save</button>
</div>

Related

Input in HTML not connecting to PHP post method

I have an HTML input and button:
<form action="validate.php" method="post">
<!-- THE CODE INSERT -->
<div id="code">
<form>
<label></label>
<input id="input" name="InputText" type="text"/>
</form>
</div>
<!-- THE BUTTON ITSELF -->
<input type="button" id="button" name="myButton"><b>Search Archive</b>
</form>
in my validate.php file I have this switch statement:
<?php
switch ($_POST["InputText"])
{
case "someval":
http_header("someaddress.com");
die();
break;
}
?>
the problem is that when I click the button it doesn't do anything. I did this with JS and it worked but it should be noted that I'm really new to web development so if anyone can explain to me what I did wrong and specifically why that would be great. Thanks!
You have a form inside of a form, that won't work. Also, you need to include an <input type="submit" value="submit" /> before you close your form. This is what submits the information from the form to your action="file.php".
A form would typically look like this:
file.html
<form action="validate.php" method="POST">
<input type="text" name="username" placeholder="Enter your username" />
<input type="submit" name="submit" value="Submit" />
</form>
Then you'd do something like this:
validate.php
<?php
echo "Your username is" . $_POST['username'];
The $_POST['username'] is the data gathered from the name="username" input from the HTML. If you write die($_POST); you'll get all the data that is sent through the form.
When you are using type='button' you have to perform the submit by yourself.
So, you can do that using javascript or change to type='submit'.
Example:
<input type="button" id="button" name="myButton"><b>Search Archive</b>
To
<input type="submit" id="button" name="myButton" value="Search Archive" />
you can try this
<form action="/validate.php" method="post">
<!-- THE CODE INSERT -->
<div id="code">
<label></label>
<input id="input" name="InputText" type="text"/>
</div>
<!-- THE BUTTON ITSELF -->
<button type="submit" id="button" name="myButton">Search Archive</button>
</form>
in the div id ="code" you used form tag that's why its not work...delete it will work and button type must be submit

prestashop file upload won't work

I am trying to implement file upload in Order Detail page.
Created form
<form action="" method="post" id="uploadForm" enctype="multipart/form-data">
  <label for="fileUpload">{l s='File to upload:'}</label>
  <input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
  <input type="file" name="fileUpload" id="fileUpload" />
<div class="submit">
<input type="hidden" name="id_order" value="{$order->id|intval}" />
<input type="submit" class="unvisible" name="submitMessage" value="{l s='Send'}"/>
<button type="submit" name="submitMessage" class="button btn btn-default button-medium"><span>{l s='Send'}<i class="icon-chevron-right right"></i></span></button>
</div>
</form>
and added this to OrderDetailController.php
if (Tools::isSubmit('submitMessage')) {
$idOrder = (int)Tools::getValue('id_order');
$msgText = Tools::getValue('msgText');
if (isset($_FILES['fileUpload']['name']) && !empty($_FILES['fileUpload']['name']) && !empty($_FILES['fileUpload']['tmp_name']))
{
$this->errors[] = Tools::displayError('Works');
}
...
Now when i submit form - it saves message and completely ignores file.
Does anyone know why?
Solved myself. Answered here
It was ajax issue not sending $_FILES[] data
I'm assuming you placed this into order-detail.tpl of default template.
You put your file input into a separate form that doesn't do anything since it does not have a submit button. The comments form on that page is a completely different one that's why your input is ignored.
What you want to do is place your html code without <form> tags into a proper form that submits message. The form is at the bottom of order-detail.tpl.
<form action="{$link->getPageLink('order-detail', true)|escape:'html':'UTF-8'}" method="post" class="std" id="sendOrderMessage">
<h3 class="page-heading bottom-indent">{l s='Add a message'}</h3>
<p>{l s='If you would like to add a comment about your order, please write it in the field below.'}</p>
<p class="form-group">
<label for="id_product">{l s='Product'}</label>
<select name="id_product" class="form-control">
<option value="0">{l s='-- Choose --'}</option>
{foreach from=$products item=product name=products}
<option value="{$product.product_id}">{$product.product_name}</option>
{/foreach}
</select>
</p>
<p class="form-group">
<textarea class="form-control" cols="67" rows="3" name="msgText"></textarea>
</p>
<!-- Your html snippet -->
<p class="form-group">
<label for="fileUpload">{l s='File to upload:'}</label>
<input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
<input type="file" name="fileUpload" id="fileUpload" />
</p>
<div class="submit">
<input type="hidden" name="id_order" value="{$order->id|intval}" />
<input type="submit" class="unvisible" name="submitMessage" value="{l s='Send'}"/>
<button type="submit" name="submitMessage" class="button btn btn-default button-medium"><span>{l s='Send'}<i class="icon-chevron-right right"></i></span></button>
</div>
</form>

Button give $_GET extra parameter

I have two buttons in my form, one is for answer a question and the other is for copy the question.
<div id="question">
<?php echo($question->content) ?>
</div>
<form action="script.php" method="GET" id="question">
<input type="text" name="question">
<button id="answer" onclick="document.getElementById('question').submit()">Answer the question</button>
<button id="copy" onclick="document.getElementById('question').submit()">Copy the question</button>
</form>
The URL of script.php look now like:
script.php?question=sometext
Now I want that when you click at the copy button the URL looks like this:
script.php?question=sometext&copy
And for the answer button:
script.php?question=sometext&answer
EDIT:
There are much answers where is said: "use <input type> instead of <button>"
The problem is that I can't use a input field as button because the button is outside my form. And I can't put it inside my form
What you can do is to use one hidden field and change it's name according to the pressed button. Something like the following:
<div id="question">
<?php echo($question->content) ?>
</div>
<form action="script.php" method="GET" id="question">
<input type="text" name="question">
<input id="action" type="hidden" name="" value="">
<button id="answer" onclick="document.getElementById('action').setAttribute('name','answer'); document.getElementById('question').submit()">Answer the question</button>
<button id="copy" onclick="document.getElementById('action').setAttribute('name','copy'); document.getElementById('question').submit()">Copy the question</button>
</form>
Although this would give you the result you want at the url, it would be more appropriate to have as the hidden's field name the "action" and to change it's value to "copy" or "answer" through javascript.
Try to make two forms, with a hidden input field with the values. Then you get the extra parametrt in your url when submitting
Change your form to the following
<form action="script.php" method="GET" id="question">
<input id="question" type="text" name="question">
<input id="answer" type="submit" name="answer" value="true">
<input id="copy" type="submit" name="copy" value="true">
</form>
url:
script.php?question=hello&copy=true
Then you can check
if(isset($_GET['answer']) && $_GET['answer']=="true"){
//answer action
}
if(isset($_GET['copy']) && $_GET['copy']=="true"){
//copy action
}

Performs form action even out of the form

Hi having a problem with this code even if i clicked on cancel it will still proceed to use the action of my form even though the CANCEL button is not part of the form, Would appreciate any help.
Here is the code.
<form method="post" action="input_enroll.php" >
<div id="overlay1">
<div>
<h1> Enter Educational Level Entry</h1>
<input type="text" name="level" >
<input type="submit" value="Proceed To Enroll" '>
</form>
<input type="submit" value="Cancel" onclick='overlay()'>
</div>
</div>
EDIT: Any suggestions what would be a better idea? I'm thinking putting the cancel outside the div.
You are having form started, then divs started, then form closed..start form after divs..
as your markup is not correct, browsers will change it as thier parser suggest,
In chrome </form> tag is postponed after </div>s..
<div id="overlay1">
<div>
<form method="post" action="input_enroll.php" >
<h1> Enter Educational Level Entry</h1>
<input type="text" name="level" />
<input type="submit" value="Proceed To Enroll" />
</form>
<input type="submit" value="Cancel" onclick='overlay()' />
</div>
</div>

Form with two button submit with different value

<form action="here.php" method="POST">
<input type="text" name="text">
<div id="one">
<input type="hidden" name="aaa" value="one">
<input type="submit" value="Send">
</div>
<div id="two">
<input type="hidden" name="aaa" value="two">
<input type="submit" value="Send">
</div>
</form>
Now if i click on Send of div ONE or div TWO i have always in $_POST['aaa'] = 'two';
Is possible make one form with two submit with different values?
If i click on div one submit i would like reveice $_POST['aaa'] = 'one' and if i click on div two submit i would like receive $_POST['aaa'] = 'two'.
How can i make it?
I can use for this PHP and jQuery.
EDIT:
I dont want create two form - i dont want showing two many times <input type="text" name="text">
EDIT: maybe i can instead button submit ? but how?
It seems that what you actually want to do is have a value in each of the buttons, see this, for example:
<form action="demo_form.asp" method="get">
Choose your favorite subject:
<button name="subject" type="submit" value="fav_HTML">HTML</button>
<button name="subject" type="submit" value="fav_CSS">CSS</button>
</form>
You'd need two different forms:
<div id="one">
<form ...>
<input type="hidden" name="aaa" value="one">
<input type="submit" value="Send">
</form>
</div>
<div id="two">
<form ...>
<input ...>
<input ...>
</form>
</div>
Standard practice is that when two fields have the exact same name, to use only the LAST value encountered in the form and submit that.
PHP does have a special-case notation (name="aaa[]") to allow submitting multiple values with the same name, but that wouldn't help you here, as that'd submit ALL of the aaa values, not just the one closest to the submit button.
HTML form:
<form ...>
<input type="text" name="textfield">
<div id="one">
<input type="hidden" name="one_data" value="aaa" />
<input type="submit" name="submit_one" value="Submit" />
</div>
<div id="two">
<input type="hidden" name="two_data" value="bbb" />
<input type="submit" name="submit_two" value="Submit" />
</div>
</form>
server-side:
if (isset($_POST['submit_two'])) {
$data = $_POST['two_data'];
} else if (isset($_POST['submit_one'])) {
$data = $_POST['one_data'];
} else {
die("Invalid submission");
}
Instead of showing two submit button, you can show a radio list with two options and one submit button.
Try this:
in html-
<input id="PreviousButton" value="Previous" type="submit" />
<input id="NextButton" value="Next" type="submit" />
<input id="Button" name="btnSubmit" type="hidden" />
in jOuery-
$("#PreviousButton").click(function () {
$("#Button").val("Previous");
});
$("#NextButton").click(function () {
$("#Button").val("Next");
});
then you can see in the form results - what "Button" contains.

Categories