HTML Submit Button Explained - php

I'm just trying to understand the submit button within php.
I know that it performs that action stated within the form tag. So basically what I have is an form tag that only defines it's ID, i.e. no method attribute nor action. And within this form is a submit button. This input element only defines the type as submit, i.e. no name attribute nor id nor value.
Quickly describing the file: It has two input text elements which are required and a submit button. When i view this file in chrome, and i've clicked the submit button, a pop up shows below the required fields which i have not entered text in stating "required field".
I love this function however, it doesn't check for spaces, i.e. " ".
So back to my question, could someone possibly tell me what the submit button actually does or possibly what methods does it call when i click on it even though the form it is in has no action defined.

When the button is clicked, the browser detects this and submits the form back to the server. This has nothing to do with PHP, it's simply the browser implementing what the HTML specification stipulates.
Since your form does not have an action attribute, what happens is that the browser gathers the values of all eligible input controls in the form, turns that into a query string and makes an HTTP GET request to the current URL using that query string. The HTML5 spec covers this in detail.

The submit button offers one possible interface for the submission of the form. It's like the send button for a text message. While there are alternatives to submit the form, the submit button is the HTML option.

When a form's action is empty, the form submits the GET data to the page that form is on. (Basically, it reloads itself, with the new form data attached.) So you could write your PHP code at the top of the same page to manipulate the data.
In your PHP code at the top of the page, you can test whether or not your form sent data in those two required fields. If one or both are empty, you can echo a message to the user telling them the fields are required.

Related

Gravity Forms - Entering text in a field to also appear in another field without a page reload

How to enter text in one field to then appear in another field without reloading the page.
I have a page with a php form with two text fields and a submit button. When clicking the submit button, a pop-up Gravity Form appears to enter extra details. I need the information entered in the first two php text fields to appear in two text boxes on the Gravity Form pop-up without a reload.
Is this possible? Thanks
Yes, this is possible, but only using JavaScript. These are the steps:
You need to add an event listener to the submit button. E.g. document.querySelector('.class-of-submit').addEventListener('click', function(){ /* code here */ });
Once the event fires, you then need to target the input field(s) that contain the data you wish to access. For example, var enteredText = document.querySelector('.class-name').value.
Then sent the value of the pop-up field(s) like so: document.querySelector('.class-of-target').value = enteredText;
That's pretty much it. You'll want to add way more logic to the above (check elements exists and that the fields are not empty), but hopefully this will get you started.
If you're looking for a codeless solution, check out my Gravity Forms Copy Cat plugin. It allows you to auto-copy the value entered in almost any field to any other field.

dynamically filled forms with Jquery & Ajax

I have a bit of a problem achieving what I'm trying to do.
I'm doing a mailbox like page and I'd like to add a "Respond" button which will fill a form with some values.
I'm generating my page message by message in php. For each message I generate an input button with its own id and when I click on the button, I'd like to open a form in a new window, with the recipient and title prefilled and not changeable by the user, with another textarea that the user can fill with his message.
For that, I have a general template for a form containing 3 values (recipient, title and message). All are (for the moment) text or textarea values.
Is it possible to use one form to send a message to anyone, or do I have to generate a form for each message with pre filled values ? The last solution seems quite horrible to do.
I tried to find something that I could do with only javascript, since I want the form to be submitted asynchronously by Ajax.
I can make an onClick event and pass the values to that function, but in that case I won't have the values of the textarea the user will have entered.
Is there a way to do, for example, an entire form in Javascript with the values I'll pass through the onClick event, get the value of the textarea from the form that would have been populated with these values, and then send everything through Ajax ?
Well, here is what I did.
For each Respond button generated, I set an onClick event passing the fields I wanted to be pre-filled in my form to a JS function RespondMessage()
I also made a form at the bottom of my page that I hid with css.
This form contains as much hidden fields as I want pre-filled datas, and as much spans as I want to show infos.
When I call the RespondMessage function, I manually set the hidden fields in my form with the arguments I called the function with.
After that I fill my spans with the infos I want to show (for example, I set the recipient's name for my message like that).
Once all of that is done, your form is ready to be displayed. So after setting the fields in the form, I just pass the form from display:none to display:block, and do a lot of css tricks to make that smooth and beautiful.
So now you have your pre-filled form, you just need another function that will make an Ajax call to avoid reloading the page after each response, and you'll call it with the onClick event of your form's submission button.
I hope that this process will help someone.
I'm not quite sure that it's the best way to do it and I find it quite dirty and exploitable so you need a bit more server side checking than usual, but it does the job.
I don't provide code because it's mostly all really specific to the application you're setting up.

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.

How to submit part of inputs from a form to another domain?

When user create a post, inside the post form, I want to provide a button. When user click the button, it will launch an iframe and submit some of the inputs of the post form. When nested form is not valid, how can I submit those inputs to the other website?
Two methods, Javascript, and PHP
Javascript:
You can use jQuery Post to do this by javascript.
Basic steps:
Use an "onsubmit" handler for the form
In the function for handling the submit, build a data array with data you want to submit and send to the second form.
Either return true (to allow the programmed submit to continue) or build a secondary submit if required.
This one allows you to build the validation you refer to client-side (i.e. in the submit handler).
PHP
Receive the data in the php form, then use CURL in the receiving PHP script to pass data as needed to the second form.

Two Submit buttons in one form

I have a form which has got five (5) file input controls along with other controls. This form also has got two (2) submit buttons.
I have used JQuery Validation plugin to validate form inputs.
Name of one submit button is "Upload_Images" and name of another is "Upload_Project".
What I want to do is that if user clicks on "Upload_Images" button all files controls should be validated and files uploaded to server but not rest of the data.
If use clicks on "Upload_Project" button then the whole form should be validated and the files are not already uploaded they should be first uploaded and then the project should be saved to DB.
Now if I click on any of the two submit buttons the form Validation kicks in and if there is invalid data it stops form submit process.
How can I achieve this?
Use javascript to change your form 'Action' property to redirect to wherever other page you need to go, then submit.

Categories