Can I have multiple request in one form using php (laravel) - php

I'm building a website in which the user can create articles with multiple images.
I would like to let the users while editing the form to can delete some image that they want. So I want to have in my form the main submit button that execute the function that store all details of article but also to I have another submit button that execute the function that delete the image.
How can I do that?
Thank you so much for your attention and participation.

If I got you right, you want 1 FORM, 2 SUBMIT buttons, and based on which one you press, do a different action? It is possible to do this, but not very practical.
To do it, create 2 submit buttons with each a value. I.e.:
<input type="submit" name="mysubmit" value="delete image" />
<input type="submit" name="mysubmit" value="send the form" />
when doing this, your post/get data will contain one item names mysubmit with the value, so you know which button was pressed, and you can do an action based on this.
However, when you submit a form by pressing a submit button, you do send the whole thing to your server, and have a page refresh. I usually prefer to use Ajax for the simple operation. For example, I would remove the delete submit button and replace it a simple button. When pressed, send an Ajax call to tell the server to delete the image, and use DOM to delete the image in the browser DOM tree (usually jQuery). Note that you can also use Ajax to post the form, nicer interface, and no page refresh.

Related

Multiple PHP buttons? (One button is a submit button the other buttons are functions)

I'm making a php site that has multiple buttons. One button saves, which requires it to be taken to a different page but the other buttons do simple things like add +1 to a score.
The function buttons are attached with javascript so the code looks like this for a function button:
<button onclick="addPoints(); ButtonStats()">Add</button></td>
I have tried multiple different ways to get the buttons working but I'm not sure how to do an if statement like this:
if ($_POST['SaveButton'])
$executestring = "location: process.php";
The if statement works for the save button (but clicking the other buttons currently refreshes the page).
So, how would I go about writing in the add button codes to an 'if' statement like that of the submit button? (I've tried putting in addPoints() for the executestring but that didn't work so I'm just not sure how to write it out).
Don't use <button> without the type attribute defined as button, cause they will try to submit the form, causing the page to be "refreshed". Use
<input type="button" value="click" />
or
<button type="button">Click</button>
if you don't want them to have the submit behavior.
Look at this jsFiddle example
You are calling javascript when button Add is clicked which means you can not do anything about it in php.
When you call javascript, all calculation is done on client side (for example users browser). Php is executed only when users comes to your page (because page is requested from server, server executes php and sends result).
It suggest you learn more about client side and server side languages.

Best method to deleting table row with button inside of form

I wanted some input as to what the best way to handle this would be. I have a submit button and a normal button inside a form. I don't want to do a submit on my delete button for the form. Is setting a link outside of the button to carry over into another file using a $_GET parameter the best way here? Basically, take the GET parameter in the php file and if its true, then do my delete functionality. Is there a better way here?
e.g. <input type="button" value="Delete Item" />
GET requests should be used when accessing data (SELECT). POST requests should be used when modifying data (UPDATE, CREATE, DELETE). i.e. You shouldn't be using a GET request to delete a system resource.
I had a similar thing, and there where two methods I used:
The first was that I used a standard button which had the onclick attribute set to a javascript function that would change the value of a hidden input and would then submit the form.
The second was a submit button, but following this question: Position div box at the end of after ensuing elements, I had the main form submit (that would save) at the beginning of the form, which then appeared at the bottom of the form. This mean't that when if the enter button was pressed, the delete submit wouldn't be clicked (and detected by the script), but the main submit would be.

Button INPUT that saves data and links at the same time

I'm doing a questionnaire (form) and I need to put a submit button that does two things:
Be a button type INPUT (because I need to use this kind of button on my PHP code, I've if(#$_POST['Next']) for save the dates of the form in my DB).
That this button will have a link for go to the next screen of the questionnaire. I tried with
<a href="demo2.html" target="_blank">
<input class="buttonNext" name="submit" type="submit" value="NEXT &#8592">
</a>
This code doesn't work, but with IE browser, on the page appears a circle next to my button that is the link. So the button doesn't work; it only saves the data, but doesn't link to the next page.
How can I solve it?
The form's action determines the URL where the form is sent, and this is just good for you (it is sent to the PHP that processes this step). It can save the data and/or return the form again to show validation errors. Once you decide to go to the next step, you can redirect the user to that url.
use if(isset($_POST['Next'])){ instead of #

Survey written in HTML using Generic HTML Form Processor, won't save a user's data if the user clicks the previous button

I'm using Generic HTML Form Processor in combination with an xampp lite installation to save data into a MYSQL database. However, I've encountered a problem while writing a multi-page survey. If users use the previous and forward buttons coded like:
<INPUT TYPE="button" VALUE="Previous Page" onClick="history.go(-1);">
<input type="submit" value="Go to last page">
The next button differs slightly per page. For example, on page 1 it's:
<input value="Next Page" type="submit">
Users can go back and forth in the survey. I would prefer them to use the backwards and forward button to let the browser take care of saving the data but sadly it's not an option. The next button is linked to a hidden input code like:
<input type="hidden" name="next_page" value="http://localhost/quislast.html">
It works like a dream except for one problem. Imagine there are three pages, A, B, and C. If a user is on page C, and they click the previous button twice so they return to page A. If they then click the next page button, the form data (radio buttons) that the users select on pages B and C are no longer remembered. The users then have to re-select their answers before submitting the form.
Any ideas? Thank you in advance. And I apologize if the answer is so simple a novice could understand it.
Use a Session to collect the data during the survey. Then, when the user goes back and forth through the pages, fill in the input value attributes from the collected values in the $_SESSION array. When the user is finished with the survey, save the data to the database.

What's the standard way to give a set of different clickable options all submitting the same form?

I have a PHP-generated page which is displaying some data about a set of films. The page is updated using POST. The form only shows films starting with a particular letter. I want to present a set of clickable options at the top of the screen, each of which is a letter. So if you click on "B" it submits the form and re-draws the page showing only films that start with B. (I know, Ajax would be a better way to do this, but I'm trying to get something done quickly).
Anyway, I know I can do this by having each link be a Javascript call which sets the value of a hidden field and then submits the form, or I could do it by having each letter be a button which has a particular value and submits the form directly, but neither of those strikes me as particularly elegant. Is there a standard way to do this? Am I missing something really obvious?
You can always create a number of submit buttons and give each a different name. Then you test to see which submit was pressed based on what is included in the POST array.
Please note that you can use the image input type in place of the submit input type so that you can substitute your own image, etc for your button.
I think you pretty much cover the options you have. I generally see it done with javascript and hrefs because people don't like to style real buttons.
Be aware that Internet Explorer (6/7?) doesn't POST a variable with the name of the button on an <input type="image"> - it only posts variables with name_x and name_y with the coordinates of where on the button was clicked.
You don't actually need to submit the form as you are not really using it.
Why not just a set of hyperlinks B
Use several different submit buttons, like this:
<input type="submit" name="letter" value="A" />
<input type="submit" name="letter" value="B" />
<input type="submit" name="letter" value="C" />
...

Categories