POST extra values in an HTML <form> - html

I have a simple form which passes the value of an <input /> element:
<form action="updaterow.php" method="POST">
<input type="text" name="price" />
</form>
How can I post extra values along with the <input /> value? For example, an arbitrary string, or a variable inside the current PHP script.
I know this is possible with GET:
<form action="updaterow.php?foo=bar" method="GET">
<input type="text" name="price" />
</form>
or:
<form action="updaterow.php?foo=<?=htmlspecialchars($bar)?>" method="GET">
<input type="text" name="price" />
</form>
but I need POST.

You can include a hidden form element.
<input type="hidden" name="foo" value="bar" />

You can simply use a hidden field. Like so:
<input type="hidden" name="your-field-name" value="your-field-value" />
This field will then be available in your PHP script as $_POST['your-field-name']

As mentioned already, using hidden input fields is an option, but you can also use a session. That way you donĀ“t have to post anything, the variables remain on the server and are not exposed in the html.

Related

Html form get request not adding the form data

I'm making a desktop app that requires user to activate a key.
So the form here is making a get request to the api.
How do I make it so that when the user submits a form data (a key)
it is added as a parameter in the string?
UPDATE: Question answered.Thanks!
But can anyone tell me how to make it so if the key is valid, the software loads, but if not then it throws an exception?
<form method="get" formenctype="text/plain" action="https://app.cryptolens.io/api/key/Activate?token=blahblahblah&ProductId=xxx2&Key=" >
<input type="text" maxlength="23" size="80" placeholder="XXXXX-XXXXX-XXXXX-XXXXX" />
</form>
I don't think you quite understand how GET works. Get sends data via the URI, any field(using the 'name' property) you add into your form is added to the query string.
<form method="get" formenctype="text/plain" action="https://app.cryptolens.io/api/key/Activate" >
<input type="text" name="token" maxlength="23" size="80" placeholder="XXXXX-XXXXX-XXXXX-XXXXX" />
<input type="text" name="ProductId" maxlength="23" size="80" placeholder="XXXXX-XXXXX-XXXXX-XXXXX" />
<input type="text" name="otherData" maxlength="23" size="80" placeholder="XXXXX-XXXXX-XXXXX-XXXXX" />
</form>
This will produce: https://app.cryptolens.io/api/key/Activate?token=VALUE&ProductId=VALUE&otherData=VALUE
Note I also removed the query string from the ACTION="" As the request will do that automatically for you.
EDIT: I also wouldn't recommend having your token in plain view. Either you make it a hidden field or you use javascript to execute the request with some masking. Hidden is usually fine providing the API has the necessary security to handle it.
Using hidden inputs and naming them like the GET you need.
Check the name token with hidden value like example.
Remove all the GET from the action-url.
The final URL will be: https://app.cryptolens.io/api/key/Activate?Key=yourKey&token=blahblahblah&ProductId=xxx2
<form method="get" formenctype="text/plain" action="https://app.cryptolens.io/api/key/Activate" >
<input type="text" maxlength="23" size="80" name="Key" placeholder="XXXXX-XXXXX-XXXXX-XXXXX" />
<input type="hidden" name="token" value="blahblahblah" />
<input type="hidden" name="ProductId" value="xxx2" />
</form>

HTML code to send SMS message through a form

I am trying to create an HTML code in order to send SMS messages through a form.
My provider give to me the Http address that i could send these SMS and it looks like bello (of course i have hide passwords, etc)
http://sms.services/send-sms?app-id=108744&password=1111111&to={1}&from=TestSender&message={0}
When i call this address with a phone number instead of {1} and a text insted of {0} it works fine.
But when i try to create the bellow page it says that the attributes are wrong.
Can you please help?
<form action="http://sms.services/send-sms?app-id=108744&password=1111111&to=telnumber&from=TestSender&message=textsms" method="post">
<input type="number" name="telnumber" />
<input type="text" name="textsms" />
<input type="submit" value="Submit" />
Change your method to GET
Make other parameters input type hidden so user will not see those but internally it will be passed.
<form action="http://sms.services/send-sms" method="get">
<input type="number" name="to" />
<input type="text" name="message" />
<input type="hidden" name="app-id" value="108744" />
<input type="hidden" name="password" value="1111111" />
<input type="hidden" name="from" value="TestSender" />
<input type="submit" value="Submit" />
</form>
Note: It is not recommended to do like it because it will expose your app-id and password. You should send it to a php or any backend framework and then from there call this.
Try to use input type="text" instead of number.
Probably, input type="number" tries to convert string to demical, but your provider use string

Using HTML input element (type="submit"), how can we customize the GET field?

Using <form method="get"> element including <input type="submit"> element, we can have a way to GET a web page with some fields specified by the <input type="text" name="studentId"> elements, but can I customize those fields?
For example: I always want to add a action=true to the GET url to let the URL be something like this: http://example.com/?studentId=123&action=true?
Use <input type="hidden" name="action" value="true" />
inside your form.
You can add a hidden form field, though the name action is not a good one, as form has an action attribute and this name can conflict when scripting the form:
<input type="hidden" id="something" name="something" value="somthingelse" />
<div id="gbqffd">
<input type="hidden" value="en" name="h1">
<input type="hidden" value="d" name="tbo">
<input type="hidden" value="search" name="output">
<input type="hidden" value="psy-ab" name="sclient">
</div>
Google always has the answer; in this case I never had to do a search just look at the source :)

HTML Form POST re-writing action attribute

I have an HTML form that I'm trying to get to post with part of a query string already inplace, but it keeps re-writing the URL.
<form id="mls_form" action="/index.php?option=com_mls&view=mls" method="get">
<label>MLS#:</label>
<input type="text" name="mlsnum" />
<input type="submit" value="Go" />
</form>
Output is:
http://www.mysite.com/index.php?mlsnum=value
It seems really simple, but I don't know why it's re-writing the action attribute.
Use the POST method rather than the GET method. The URL parameters will be sent as specified in the action attribute, and the form inputs will be sent in the post data. Your server script can then read them each using whatever API is appropriate (in PHP, $_GET versus $_POST, or find them all in $_REQUEST).
If you must use GET you can give the additional parameters as hidden input fields.
<form id="mls_form" action="/index.php" method="GET">
<input type="hidden" name="option" value="com_mls" />
<input type="hidden" name="view" value="mls" />
<label>MLS#:</label>
<input type="text" name="mlsnum" />
<input type="submit" value="Go" />
</form>

GET form action with URL parameter gets removed on form submission

This doesn't go to the URL http://example.com/a.php?r=1&go=1. Instead it goes to http://example.com/a.php?go=1 .
<form method="GET" action="a.php?r=1">
<input type="radio" name="go" value="1">
</form>
Even if I clear action="" it again goes to the same wrong URL. How can I include the r=1 in the URL of the form submission?
When you use method="get", you can't put values in the query string of the action. They will be replaced by the values from the form fields when you post the form.
Put the values in hidden fields instead:
<form method="GET" action="a.php">
<input type="hidden" name="r" value="1">
<input type="radio" name="go" value="1">
</form>
Passing additional parameters is done with hidden fields:
<input type="hidden" name="r" value="1" />
(sorry, i edited it; it didn't take my code on the first attempt)