I want to pass a GET parameter to a PHP file , by data typed in a text field. I want something like this :
<form id="Form1" action="action.php?nameExample=textFieldData" method="get">
the textfield is out of this form that's why I need to affect the value of the textfield into get PHP parameters so I can send it with another data in the same msg
the textfield code :
Username: <input type="text" name="nameExample"><br>
anyway to do it ?
You can't use $_GET parameters in an HTML form action attribute if the <form method='get'. Use <input type='hidden' name='nameExample' value='whatever' />, or better yet, use AJAX without a form.
This should do what you want to. The JS code copies the value of an external input field to a hidden input field.
HTML:
<form action="action.php" method="get" id="Form1">
<input type="text" name="otherData" value="xxx">
<input type="hidden" name="nameExample" value="" id="nameExampleCopy">
<input type="submit">
</form>
Username: <input type="text" name="nameExample" id="nameExampleOriginal">
JS:
document.getElementById('Form1').addEventListener('submit', function () {
var originalElement = document.getElementById('nameExampleOriginal');
var hiddenElement = document.getElementById('nameExampleCopy');
hiddenElement.value = originalElement.value;
});
But you should think about some points:
Is get the right method here? It sounds like post could be better.
Is it semantically a good idea to pass a value from outside the form inside it? We don't know your application, but generally it is a good idea to have all submitted data inside the form. As HTTP pointed out in his comment, sessions could also help with that.
Related
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'.
I dont know if this is even possible without javascript but i want an form input and when i send the form it should add some text on the value.
<form method="post" action="https://somesite.com">
<input name="snapname" style="width: 177px; margin-top: 340px;" type="text" placeholder="Your username">
<input type="Submit" name="send">
</form>
So right now the form sends the value that is typed from the user in the browser.
Is there an way for me to add text when i send the form that the user cant see? Do i need to do it in javascript? Or is it possible in HTML? HTML5?
For example lets say the user writes adam
The value of snapname is now adam
When they click submit it will submit it as adam
But what i want is to add text to the value.
So i want mytext+adam to be the value of snapname where mytext is a static value.
I do not want to use javascript if i dont need to. Is it a good idea to use some kind of redirect php script? And let the script add the text and then send the users to the correct webadress with right value?
Use javascript or simply:
$my_var = "My Text" . $_POST['snapname'];
And then use this variable in php.
If you are submitting to an external site your best bet will be to use javascript. You can remove the name from the visible input (so its data is not submitted), add a hidden field with the correct name, then populate the hidden fields value with javascript:
<form method="post" action="https://somesite.com">
<input onkeyup="appendvalue(this.value);" type="text" placeholder="Your username">
<input name="snapname" type="hidden" id="snapname">
<input type="Submit" name="send">
</form>
<script>
function appendvalue(userval){
var append = "somestring";
var hidden = document.getElementById("snapname");
hidden.value = userval+append;
}
</script>
PHP is going to be your best bet here, as Faiz said above create a simple variable and extend the $_POST['snapname'] to include what you need.
<html>
<script>
function changeText()
{
document.getElementById("input1").value = <?php echo '"'.$_POST['input'].'"'; ?>;
return true;
}
</script>
<form name="mainform" action="" method="post">
<input type="text" name="input" id="input1" />
<input type="submit" onclick = "changeText()" name="Submit" value="Submit!" />
</form>
<html>
i have this code here. can you make it work as intended ?
everytime i click Submit! i want to change the value of the textarea to the last input the user inserted.
PHP code is parsed by a PHP interpreter before any HTML output is sent to the browser.
If your form action is the same page and the same form will be shown before and after submission, then you can let PHP print the value of the input field directly into it.
<input type="text" name="input" id="input1" value="<?php echo htmlspecialchars($_POST['input']);" />
If you're trying to revert the value of this input field whenever a user clicks the submit button, then your code (even if it's prone to code injection) should work but this is useless since the page will be requested again when submit is clicked.
I assume you need to fill in
action=""
By the name of your file, like
action="myFile.php"
Few tips :
NEVER trust the user. The user can manually change the value of the input and send some dangerous values in your $_POST variable. You need to check it using filter_input() by example.
Like #Charles said this is pretty simple problem, use google next time.Here for example
I was originally using a $_GET method:
''
Using this works, but i've been told that using a $_POST method is better, less messier and just the preferred choice.
So i'm wondering how i would implement the very same thing as above, but using a $_POST method.
You can submit a form with method post or use an ajax request. In jquery this would look like:
$("#myLinkId#").on('click',function() {
$.ajax({url:'phpFile.php',type:'post',data:{...you data here...}});
});
You need to turn this into a form or use jQuery to trigger an Ajax call on click. Here's a form example:
<form action="editNews.php" method="post">
<input type="hidden" name="starts" value="<php echo $starts?>">
<input type="hidden" name="end" value="<php echo $end?>">
<input type="hidden" name="event" value="<php echo $event?>">
<input type="submit" name="submit" value="Your Link" data-icon="edit">
</form>
To send data via PHP to another page/form, you can do this:
Original form:
<form method='post' action='new-page.php'>
inputs here, etc.
submit button here
</form>
So this will, upon clicking submit, send you to new-page.php with all of the form data held in the PHP variable $_POST which is an array.
You can access the data by referencing the array with the name of the input, for example, say that you had:
<input type='text' name='NAME' />
You can reference this data on new-page.php with $_POST['NAME'].
I hope this helps you!
How do I link the PHP to the HTML form? I understand how to do the PHP and how to do the HTML, but how do I link the php to the html or is that automatic,
how does the HTML form know about the PHP?
In your form set the action attribute to the path of your php script eg:
<form action="/path/to/php/script.php" method="post">
...
</form>
You set your action="" in your form to point to your PHP script. When the user clicks the submit button in your form, the PHP script will be called and the formdata will be handed over to the PHP script.
The method you choose when making your form is how PHP will gather the values passed in.
As such:
<form action="handler.php" method="post">
<!-- OR -->
<form action="handler.php" method="get">
The action tells where the form values will be sent to and the method tells how the values of the items in the form will be passed back to the server. The post method will send the values back so they may be retrieved by the $_POST array (both post and get can be retrieved by the $_REQUEST array). For example:
<input type="text" name="myInput">
Will post back to the server and can be retrieved by
$var = $_POST['myInput'];
It's always best to test if there is actually an input, and the following can be used
if(isset($_POST['myInput'])) { /*do something if set*/}
else{ /*do something if not set*/}
If the form was submitted by the get method, the values of the form is passed back in the URL, like such:
http://www.domain.tld/handler.php?myInput=someValue
The value is then retrieved by using the $_GET array:
$var = $_GET['myInput'];
Once again, you should test that it exists.
For good examples and explanations, please read a PHP book or search for PHP and HTML forms. This is the very basics of PHP.
Try this in a php file
<form action="" method="post">
<input type="text" value="html form data" name="name" />
<input type="submit" name="submit" />
</form>
<?php
if(isset($_POST['submit']))
echo 'I am php. I know this value is from html - '. $_POST['name'];
?>
If you are talking about refilling your form with the php values: inside your input fields, just add the request variable.
<input type="text" name="input1" value="<?=$_REQUEST['var_name']?>" />
If you are talking about sending date to php, just point to a file using the form action.
<form action="file.php" method="post">
</form>
Then you process all the data in that php file.