how to put a form value in to variable in php? - php

i am doing url masking to pass the values. the code below input a variable.
<form >
<input type="text" name="name" required />
<button type="button" id="btn" action="drive/<?php echo$_GET['name'];?>
/">submit</button>
</form>
i want to pass the value like
drive/12
but when i click the submit button it is passing like
drive/?name=3.
what is the correct way of submitting the value?

The easiest option is to just set the form method to POST.
Otherwise, you'll have to add a javascript handler to the form's submit event, encode the text field and redirect the browser to the desired url.

the form URL is where your action should be and the submit stays the same - right now the NAME is being passed as a get by default for the form.
<form method="post" action="drive/<?php echo$_GET['name'];?">
<input type="text" name="name" required />
<input type="submit" value="submit">
</form>

Related

Can not pass the action get param to the PHP file

I have a smarty project, and in the .tpl file, there is a form:
<form method="get" action="{$smarty.server.PHP_SELF}?action=func1">
<input type="text" name="username"/>
<input type="submit">
</form>
there is a question, if the php file have many function for different action requests, so in the template if have many forms, I want to through the action for distinguish.
but in my practice, see upper code, I write like this, this can not delivery the action to my php file.
I want to write the action in the form action, because this can be more standard. so I don't want to write it in a hidden input. why write in the action can not pass into the php file?
You could use submit button with specified name and value:
<form method="get" action="{$smarty.server.PHP_SELF}">
<input type="text" name="username" />
<input type="submit" name="action" value="func1" />
</form>
and then you'll get a global variable $_POST['action'] with value func1. But the value will be showing on your button title, so I offer you to find forms only by submit name, for example name='submit_form1'.

Why the submitted data is not getting displayed in the URL when an HTML form is submitted using "get" method?

I'm using PHP 7.0.2
Consider following HTML form code :
<form action="foo.php" method="get">
Name: <input type="text" name="username" /><br />
Email: <input type="text" name="email" /><br />
<input type="submit" name="submit" value="Submit me!" />
</form>
Consider file foo.php
<?php
print_r($_REQUEST);
?>
After filling the form fields I submitted the form by clicking on the Submit button. Then the REQUEST array values get printed but I didn't see those values in the URL of a browser like a query string.
How did that happen? I expected to see the form fields values in my browsers's URL as I am submitting the form using "get" method.
If this going to happen then what's the difference between using "get" method and "post" method for submitting an HTML form to some PHP file?

Button functioning as link does not work with $_GET

I am using a button to function in the same way as a hyperlink:
<form action="intro.html"><input type="submit" value="CLICK HERE TO ENTER" ></form>
This works for static links, but does not work with PHP $_GET arguments.
<form action="wrong_choice.php?stage=0"><input type="submit" value="Wrong Choice!" ></form>
Clicking that will proceed to "wrong_choice.php" but not "wrong_choice.php?stage=0"
How can I fix that?
Thank you
Better to use:
<input type="button" value="Wrong Choice!" onClick="document.location.href('wrong_choice.php?stage=0');" />
If you do not want javascript, add method to form, delete parameter from action and add input with type hidden, which stands for parameter.
Action does not accept query string!
If you want to append data into the form which isn't part of the inputs filled by the user, add inside the <form>
<input type="hidden" name="stage" value="0" />
Action is what you want to do with the information in the form: you want to send the form in a email or send the information to another script to manage or comeback to same script.
If you want pass arguments in the form you should put them in form's fields like that:
<form action="wrong_choice.php>
<input type='hidden' value='0' name="stage">
<input type="submit" value="Wrong Choice!" >
</form>
Thanks

How to get a form name in php script?

For my php file, I need to grab the unique form name.
The php file is executed when a user clicks the submit button. However, there are multiple submit button each with the same id, but they all have unique names. I need the name when they click on the submit button.
you dont want elements in html with the same id - bad practice in general. Your page will likely load normally but an html validator will notice it as an error.
html validator: http://validator.w3.org/
without seeing your code, its difficult to give you a definitive answer. if you have miltuple forms you can use hidden inputs. e.g.
<input type="hidden" name="form_name" />
Otherwise you can use javascript to put data in the form when the button is clicked. example javascript using jquery
html:
<form id="formid" >
<button type="button" id="someid" onclick="submitForm('btn1')" />
<button type="button" id="someid" onclick="submitForm('btn2')" />
<input type="hidden" id="btnsubmitid" value="" />
</form>
js:
function submitForm(btnID){
$("#btnsubmitid").val(btnID);
$("#formid").submit();
}
1 way is to put a hidden input inside of your form.
<input type="hidden" name="formName" value="[name of form]" />
then in your php, you can get it using
$form-name = $_POST['formName'];
pretty sure there are other ways, but this came to mind first.

Post-variables only passed, when action="" is empty

In a php-script i have a form, method is post, action-attribute is empty, which is working so far. but when i add a value into the action-atribute, like so:
action="index.php?id=9&get-id=5"
the whole post-array is empty after submitting.
Someone has any idea what this could be about?
Thanx in advance, Jayden
edit: here is an Example:
$form = '<form name="form1" method="post" enctype="multipart/form-data" action="index.php?id=9&get-id=5">
<input type="text" name="name1" value="">
<input name="submit" type="submit" value="submit">
</form>';
The form is displayed in a tab in a js-tabmenu, which opens also by get-parameters, in each tab is a form and after submitting the get-param is needed to display the right tab with the right form.
try to use $_REQUEST
which is collection of $_GET and $_POST
You should not use both GET and POST in a request.
You must only use post, therefore the two variables 'id' and 'get-id' should be in the form (use hidden fields)
edit:
try changing your code to:
<form name="form1" method="post" enctype="multipart/form-data"
action="index.php?id=9&get-id=5">
<input type="hidden" name="id" value="9">
<input type="hidden" name="get-id" value="5">
<input type="text" name="name1" value="">
<input name="submit" type="submit" value="submit">
</form>
then if you :
print_r($_POST);
at the top of the index.php page you should be able to see what is going on.
Also - just to check are there any redirects in your code, ie does index.php then redirect somewhere else as that would cause the $_POST to get lost
If you're trying to access id or get-id from your script: Those were appended to the url, even if you submit that form via post. Therefore you will find their values in $_GET, as usual. Only the values of <input> fields (and textarea etc., simply: all form elements) are in $_POST.

Categories