Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Submit An HTML Form Using PHP?

This is what I am trying to do without success :

<form name="something" action="ht.php" method="post">
   <a href="#" onclick="document.url.submit('helloworld');">Submit</a>
</form>

When I click on the link I want to post the value helloworld to ht.php. How can I do this?

like image 779
donvitto Avatar asked Mar 04 '11 04:03

donvitto


People also ask

What are the method to submit form in PHP?

Use the action attribute to send the request to another PHP file. The method is used to GET the information or POST the information as entered in the form. Use isset() method in PHP to test the form is submitted successfully or not. In the code, use isset() function to check $_POST['submit'] method.

How do you submit a HTML form data?

The method attribute specifies how to send form-data (the form-data is sent to the page specified in the action attribute). The form-data can be sent as URL variables (with method="get" ) or as HTTP post transaction (with method="post" ). Notes on GET: Appends form-data into the URL in name/value pairs.

How send data from PHP to HTML?

php , you can do so by sending the values through URI and fetching it from $_GET method. assume you have the values in page1. php and want to send the values to page2. php while redirecting then you can do it this way while redirecting.

Can PHP handle forms?

PHP Form HandlingWe can create and use forms in PHP. To get form data, we need to use PHP superglobals $_GET and $_POST. The form request may be get or post. To retrieve data from get request, we need to use $_GET, for post request $_POST.


2 Answers

You can't just do document.url.submit(), it doesn't work like that.

Try this:

<form id="myForm" action="ht.php" method="post">
    <input type="hidden" name="someName" value="helloworld" />
    <a href="#" onclick="document.getElementById('myForm').submit();">Submit</a>
</form>

That should work!

like image 146
Flipper Avatar answered Oct 08 '22 03:10

Flipper


Using jQuery, its rather easy:

$('form .submit-link').on({
    click: function (event) {
        event.preventDefault();
        $(this).closest('form').submit();
    }
});

Then you just code as normal, assigning the class submit-link to the form submission links:

<form action="script.php" method="post">
    <input type="text" name="textField" />
    <input type="hidden" name="hiddenField" value="foo" />
    <a href="#" class="submit-link">Submit</a>
</form>

I find this method useful, if you want to maintain an aesthetic theme across the site using links rather than traditional buttons, since there's no inline scripting.

Here's a JSFiddle, although it doesn't submit anywhere.

like image 37
Dan Lugg Avatar answered Oct 08 '22 02:10

Dan Lugg