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?
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.
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.
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.
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.
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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With