HTML update input textarea everytime "Submit" it clicked - php

<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

Related

How to get user input and apped to the URL in PHP?

I want to create a simple PHP website were user should enter any string in text box and click submit then that string should be placed in the URL
For example:
If user entered "Java" as their input then after clicking submit, the URL should change to:
example.com/Java
How it's possible any example or a sample program if possible
Well, taking your question literally, this should work
<?php
if(isset($_POST['submit'])){
$userInput = $_POST['userInput'];
header('Location:example.com/'.$userInput);
}
?>
<form action="" method="POST">
<input type="text" name="userInput"/>
<input type="submit" name="submit" value="Send"/>
</form>
Of course, depending on your codes, this might be used to tamper or do something bad to your codes.

How does the submit button on a form work?

I have a simple form that collects data and sends it to a PHP script using POST.
<form method="post">
<input type="text" name="cost">
<button name="submit" type="submit">Submit</button>
</form>
The PHP script is,
if(isset($_POST['submit'])){
echo "set";
}
I want to know what happens when I click on the submit button?
The PHP manual says the following about isset,
isset — Determine if a variable is set and is not NULL
When exactly is the submit button SET? When I echo out echo $_POST['submit']; it outputs nothing.
It's only when I use the value attribute along with the submit button that I get something on $_POST['submit'];. Why should I use the value with the submit button? What exactly does it do?
I want to know what happens when I click on the submit button?
It submits the form.
When exactly is the submit button SET?
When the user submit's the form.
When I echo out echo $_POST['submit']; it outputs nothing.
You didn't specified a value for it, so it returns an empty string ($_POST['submit'] === "")
Why should I use the value with the submit button? What exactly does it do?
Well on an button the value is not needed, it is enough when it is set, so you can check if the button was submitted and not an other form f.ex.
Try with this
<form action="" method="post">
<input type="text" name="cost" />
<input type="submit" name="submit" value="Submit" />
</form>
In php side
if(isset($_POST['submit']) && $_POST['submit']=="Submit"){
echo "set";
}
It submits the whole form data into targeted location and the GET and POST methods are used to send encoded data to the targeted location
The GET method is restricted to send upto 1024 characters only.
The POST method does not have any restriction on data size to be sent.

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.

How do you receive the data when you click the submit button of a form?

After I add a button and a text field, how can I program that button to simply take what's in the text box and put it into a variable? I have no idea how the button click event works.
<form id="form1" name="form1" method="post" action="">
<label>
<input type="submit" name="Searchbydistro" id="Searchbydistro" value="Submit" onclick="xxxxxxxxx " />
</label>
<label>
<input type="text" name="txtboxsearchbydistro" id="txtboxsearchbydistro" />
</label>
</form>
Would I put a PHP statement in the space where the xxxxxxxx is at?
Any help would be great!
You can't execute PHP code in onclick() statements because PHP gets executed on the server, before the page is sent to the browser, and the onclick() function is exectued at the browser.
Solution would be (assuming this page is form.php) set the action of the form for "form.php" and on that page have
if(isset($_POST)){
$variable = $_POST['txtboxsearchbydistro'];
// Here you can run validation on $variable, sanitize it and pass it to a DB query
}
No, php is server-side, and onClick is client-side event.
I'm not entirely sure what are you trying to accomplish. If you wish to submit your txtboxsearchbydistro value to some PHP script, you would put something like this:
<form id="form1" name="form1" method="post" action="somePhpScript.php">
Then you would use something like Bobby proposed.
If you wish to do something before you actually send the form, or you want to do something on client side (i.e. in visitor's browser), you'd need to do something like
<input type="submit" name="Searchbydistro" id="Searchbydistro" value="Submit" onclick="myScript();" />
You could then need to define your script, and assign your value there.
Hope it helps.

PHP sticky form question

I'm using a PHP sticky form and was wondering is there a way I can clear a forms fields once its been submitted by the user?
A sticky form is simply a standard HTML form that remembers how you filled it out. This is a particularly nice feature for end users, especially if you are requiring them to resubmit a form (for instance, after filling it out incorrectly in the first place).
You could have some default values and update the fields to the values using Javascript , in case you do not want to reload the page. If you do want to reload the page, and use PHP sticky form, populate the form after clearing out the GET values.
If you want it to clear the data, you can do this:
<html>
<head>
<title>A Self-Clearing Form</title>
<script>
function clearForms()
{
var i;
for (i = 0; (i < document.forms.length); i++) {
document.forms[i].reset();
}
}
</script>
</head>
<body onLoad="clearForms()" onUnload="clearForms()">
<h1>A Self-Clearing Form</h1>
<form method="post" action="page2.php" name="test">
<input name="field1"/> Field One
<p>
<input name="field2" type="radio" value="One"/>One
<input name="field2" type="radio" value="Two"/>Two
<input name="field2" type="radio" value="Three"/>Three
<input name="field2" type="radio" value="Four"/>Four
<p>
<input type="submit" value="Submit Form Data"/>
</form>
</body>
</html>
This will clear the form when the HTML body loads completely.
It depends on your implementation, but the solution probably involves clearing the session variables that are used to hold the form data. If you're using a cut and paste solution I would suggest reading the documentation further or maybe looking over the code in detail to see how everything is stored.
If you want to check the session variables to see if the form data is in there you can use:
echo '<pre>';
print_r($_SESSION);
echo '</pre>';
die; // this line is optional

Categories