Is it possible to create multiple html text inputs on my
<form method="POST" id="2">
using:
<input type="text" value="">
with an initial value of none and fill it with a value after clicking submit from a previous
<form method="POST" id="1">
Can anyone help me with this?
Thanks in advance! By the way, I'm using PHP.
This question is a bit vague and tagged incorrectly.
PHP is executed server side. The only way to modify your form is by using Javascript.
The answer to your "Is it possible" question is simply yes.
<form name="myform" action="handle-data.php">
Input: <input type='text' name='name' value='none' />
Submit
</form>
<script type="text/javascript">
function submitform()
{
// Do things here
document.myform.submit();
}
</script>
#zerey: I coded up this commented contrived example of what I think you might have been hoping to achieve using PHP only and I've combined it all into one form --
<?php
if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST")
{
$foo = intval($_POST['foo']);
$bar = $_POST['bar'];
if (count($bar) > 0)
{
// the additional inputs that were created now contains data
// and furter validation can take place here
}
}
if (!isset($foo))
{
$foo = 0; // default the # of inputs field to 0
}
?>
<form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post">
<div>
<label for="foo"># inputs:</label>
<input type="text" name="foo" size="3" maxlength="1" value="<?php echo $foo; ?>">
<input type="submit" value="Submit">
</div>
<?php
// a check to limit the amount of additional inputs that can be
// created
if ($foo < 10 && $foo > 0)
{
?>
<div>
<?php
// loop to output the # of inputs the user previously entered
for ($i = 0; $i < $foo; $i++)
{
?>
<br>
<label for="bar[<?php echo $i; ?>]"><?php echo $i + 1; ?></label>
<input type="text" name="bar[<?php echo $i; ?>]" id="bar[<?php echo $i; ?>]" value="<?php echo $bar[$i]; ?>">
<?php
}
?>
</div>
<?php
}
?>
</form>
Do they have to be separate forms?
You could use something like the jQuery Form Wizard Plugin to split up the form in the UI and use the step_shown event to populate the fields you need.
Related
Guys I just needed to display inputs one by one each time the user click the submit. The problem on using php is that when you use loop, it display all at one time. Please Help guys.
Output: enter image description here
This is my code
<?php
for($c=1;$c<=10;$c++){
echo '<form method="POST" class="form-anticlear">
Enter Number :
<input type="number" name="num[$c]" required />
<input type="SUBMIT" name="button" value="Submit" required>
</form>';
}
?>
Your question isn't really clear as too what it is you are trying to accomplish. Assuming you want to show another input field every time submit is called, the code below will give you that. The file is called test.php change the action="test.php" if your file is called something different.
<?php
$total = 1;
if (isset($_POST["background-number"])) {
$total += $_POST["background-number"];
}
for ($c = 1; $c <= $total; $c++) { ?>
<form method="POST" class="form-anticlear" action="test.php">
Enter Number :
<input type="hidden" name="background-number" value="<?= $total ?>"/>
<input type="number" name="number-input" required/>
<input type="SUBMIT" name="button" value="Submit" required>
</form>
<?php
}
?>
I’m new to PHP and making a website to add an arbitrary number of values in a given base. How would I generate several fields based on a user’s input in a previous field?
The simple code without any validation will be like this:
<?php
if (isset($_POST['count_of_fields'])) {
echo '<form method="POST" action="">';
for ($i = 0; $i < (int) $_POST['count_of_fields']; ++$i) {
echo '<input type="text" name="field[$i]" /><br>';
}
echo ' <input type="submit"></form>';
} else {
echo '
<form method="POST" action="">
<input type="number" name="count_of_fields">
<input type="submit">
</form>
';
}
Beside the answer from #lis-dev which is generating fields in server side you will have to load the page each time to render the new fields, let's use JavaScript to do that for you without refreshing the page. and yes using mix and max you can put limit also
function generate()
{
var value = parseInt(document.getElementById("no").value);
for(var i =1; i <= value ; i++)
{
var input = document.createElement("input");
var br = document.createElement("br");
input.type = "text";
input.placeholder = "I am dynamic field " + i;
document.getElementById('form').appendChild(input);
document.getElementById('form').appendChild(br);
}
}
<html>
<head>
<title>dynamic fields test</title>
</head>
<body>
<form id="form">
<input id="no" type="text" min="5" max="10" placeholder="Enter no of Fields">
<input type="button" value="Generate" onclick="generate()">
<br />
</form>
</body>
</html>
Pulling simplexml and parsing them into php variables. I have a form with arrays in them. I want a button outside the form that will "essentially" go to the same form but with the next array number. IE:
<?
if( $xml = simplexml_load_file('my.xml') ) {
foreach( $xml as $SAVED_EXPORT) {
$mfg = $SAVED_EXPORT->productmanufacturer;
}
}
?>
<form id="myform" method="post" action="coderdb.php">
<input type="text" value="<? echo $mfg[0] ?>" name="MFG" />
<input type="submit" />
</form>
I'd like to have a button that says NEXT that when clicked it will pull up the next array ie. $mfg[1]. I believe the page would have to be reloaded which is fine. I read somewhere I may have to use $key but have never used and I am not exactly sure its what I need here.
You need some javascript use JQuery.
<script type="text/javascript">
$(function(){
var array = [];
<?
if( $xml = simplexml_load_file('my.xml') ) {
$i=0;
foreach( $xml as $SAVED_EXPORT) {
$mfg = $SAVED_EXPORT->productmanufacturer;
}
}
foreach($mgf as $key=$value) {
// if $key in not numeric then add iterator for this foreach
echo "array[$key]=$value";
}
?>
iterator = 0;
$('#nextExport').click(function(){
var theInput = $('#setExport');
// theInput.attr('val',array[iterator]);
theInput.val(array[iterator]);
if(iterator==array.length) {
iterator = -1;
}
iterator++;
});
});
</script>
<form id="myform" method="post" action="coderdb.php">
<input type="text" id="setExport" value="<? echo $mfg[0] ?> name="MFG" />
<input type="button" id="nextExport" value="Next Export" />
<input type="submit" />
</form>
I hope this helps. if something goes wrong check the syntax , logic is right.
You need a way of keeping track of which array index you are at because as of right now you only have it set to a static number 0. So if you add a hidden form you can have it post the value that you are currently at and then using PHP you can increment it before displaying the new form. Also your form should point to the PHP document that it is on.
<?
if( $xml = simplexml_load_file('my.xml') ) {
foreach( $xml as $SAVED_EXPORT) {
$mfg = $SAVED_EXPORT->productmanufacturer;
}
}
if(isset($_POST["index"])) $index = $_POST["index"] + 1;
else $index = 0;
?>
<form id="myform" method="post" action="coderdb.php">
<input type="hidden" value="<? echo $index ?>" name="index" />
<input type="text" value="<? echo $mfg[$index] ?>" name="MFG" />
<input type="submit" />
</form>
I'm a bit of a noob when it comes to PHP and forms but I will do my best to explain.
What I am trying to do is create a series of forms that are included in a content container. The user fills out form#1, clicks 'submit' and moves on to fill in form#2 and so on. The data from each form is sent to the next form and is stored in hidden fields and once form#3 is completed the data is all inserted into a MYSQL database.
All of this works fine so far. The problem I am having is that I can't seem to get the submit buttons to work as intended. What I had envisioned is that the user would navigate to 'blah.com/recruit.php?p=form1' and fill out form#1 and then the submit button would take them to 'blah.com/recruit.php?p=form2' and so forth.
<form id="form" action="recruit.php?p=form2" method="post">
This does not work but I don't understand why. I've looked around the internet and I've found a few forum topics that discuss similar issues but none of them actually go into much detail about the solution or why this approach won't work.
Can anyone explain to me what it is I am doing wrong please? I have a feeling it is stupidly obvious but I can't put my finger on it.
Many thanks,
Splatgore
Some working sample code to do what I think you're trying to achieve:
<?php
$p = intval($_GET['p']);
if ($p == 0)
{
$p = 1; // default to first form
}
if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
{
// get / set all forms values here since subsequent forms will be
// posting the previous forms' data via hidden fields
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$tel_number = $_POST['tel_number'];
if ($p == 3)
{
// all forms done, insert to MySQL here
}
if ($p == 2)
{
// form 2's validation
$p = 3; // set so that the form that follows will now show the
// third form
}
if ($p == 1)
{
// form 1's validation
$p = 2; // set so that the form that follows will now show the
// second form
}
}
?>
<form id="form" action="recruit.php?p=<?php echo $p; ?>" method="post">
<h2>Form #<?php echo $p; ?></h2>
<?php
if ($p == 1)
{
?>
<!-- form 1's fields // -->
<p><label for="first_name">First Name:</label>
<input type="text" name="first_name" id="first_name" size="40" value="<?php echo $first_name; ?>" /></p>
<p><label for="last_name">Last Name:</label>
<input type="text" name="last_name" id="last_name" size="40" value="<?php echo $last_name; ?>" /></p>
<?php
}
if ($p == 2)
{
?>
<!-- form 2's fields // -->
<p><label for="tel_number">Tel Number:</label>
<input type="text" name="tel_number" id="tel_number" size="25" value="<?php echo $tel_number; ?>" /></p>
<p><input type="hidden" name="first_name" value="<?php echo $first_name; ?>" />
<input type="hidden" name="last_name" value="<?php echo $last_name; ?>" /></p>
<?php
}
if ($p == 3)
{
?>
<!-- form 3's fields // -->
<div>
<input type="hidden" name="first_name" value="<?php echo $first_name; ?>" />
<input type="hidden" name="last_name" value="<?php echo $last_name; ?>" />
<input type="hidden" name="tel_number" value="<?php echo $tel_number; ?>" />
</div>
<?php
}
?>
<p><input type="submit" value="Submit"></p>
</form>
It looks somehow you have nested forms.
This is not possible in HTML.
If you are going with your approach, just use always the same submit button which leads to the same php script.
In the php script check which hidden fields are already set to see how far the process is.
A better approach IMO would be tough to store the data in a session var and always lead to another form page.
How do I do unlimited fields in php? Here is the scenario:
At first, there are only 2 fields, lets called: first name1, last name1
What I want to do is, when I click the "add" button, it will add another 2 fields in new row, the fields label/name should be first name2, last name2. And when I click again, it will have first name3, last name3, and so on..
Can anyone give me some sample script in php? I am new to PHP.
The form should be in HTML. If somebody can give Ajax sample code, would be a big plus.
That depends on what you mean by "field." It sounds as though you're talking about a form, which wouldn't be PHP, but instead HTML. You could have a button [Add] post back to the server, which then refreshes the page with another set of form-inputs. You also do that via javascript without having to refresh the page.
Simple Javascript (jQuery) Example:
$(document).ready(function(){
$("input[value='Add']").click(function(event){
event.preventDefault();
$("p.field:last").clone().insertAfter("p.field:last");
});
});
<form method="post">
<p class="field">
<input type="text" name="firstname[]" value="" />
<input type="text" name="lastname[]" value="" />
</p>
<p>
<input type="submit" name="submit" value="Add" />
<input type="submit" name="submit" value="Done" />
</p>
</form>
Simple PHP Example:
I don't encourage you use this as-is
<?php
$count = 1;
if ($_POST["submit"] == "Add") {
$count = ($_POST["firstname"]) ? (count($_POST["firstname"]) + 1) : 1;
} else
if ($_POST["submit"] == "Done") {
print "<pre>";
print_r($_POST["firstname"]);
print_r($_POST["lastname"]);
print "</pre>";
}
?>
<form method="post">
<?php for($i = 0; $i < $count; $i++) { ?>
<p class="field">
<input type="text" name="firstname[]" value="<?php print $_POST["firstname"][$i]; ?>" />
<input type="text" name="lastname[]" value="<?php print $_POST["lastname"][$i]; ?>" />
</p>
<?php } ?>
<p>
<input type="submit" name="submit" value="Add" />
<input type="submit" name="submit" value="Done" />
</p>
</form>
There are two ways to do this, either using solely PHP or by some fancy JavaScript. I will tackle the PHP-only solution. A JavaScript solution would be much more responsive as there wouldn't be repeated round trips to the server but it would also only work for users who have JavaScript enabled, whereas a PHP solution works for everybody.
A general outline of the solution is this:
Initially $count is 1, and one row is generated.
If the user clicks Add, the form is posted back to the very same PHP file with a hidden count variable included. The script restarts from the beginning, increments $count, and displays one more row than the last time.
If the user clicks Submit, the names that have been entered are processed.
Here's some sample code. I apologize that I do not have PHP installed on the machine I'm writing this one so this is entirely untested. Hopefully there aren't too many horrendous syntax errors!
<?php
$count = isset($_POST['count']) ? $_POST['count'] : 1;
if (isset($_POST['add']))
++$count;
else if (isset($_POST['submit']))
{
header('Content-Type: text/plain');
print_r($_POST);
exit;
}
?>
<html>
<body>
<form action="<?php echo htmlspecialchars($_SERVER['REQUEST_URI']) ?>" method="post">
<input type="hidden" name="count" value="<?php echo $count ?>" />
<?php for ($i = 1; $i <= $count; ++$i) { ?>
[<?php echo $i ?>]
First: <input type="text" name="firstName<?php echo $i ?>"
value="<?php echo htmlspecialchars($_POST["firstName$i"]) ?>" />
Last: <input type="text" name="lastName<?php echo $i ?>"
value="<?php echo htmlspecialchars($_POST["lastName$i"]) ?>" />
<br />
<?php } ?>
<input type="submit" name="add" value="Add" />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
Oh and you want a JavaScript solution, eh? Well you've got the really nice jQuery answer already. How about a ridiculously long plain-JavaScript solution, then?
<html>
<head>
<script type="text/javascript">
// <![CDATA[
var count = 0;
function addRow() {
var table = document.getElementById("table");
var row = document.createElement("tr");
var countCell = document.createElement("td");
var countText = document.createTextNode(++count);
var firstCell = document.createElement("td");
var firstInput = document.createElement("input");
var lastCell = document.createElement("td");
var lastInput = document.createElement("input");
firstInput.type = "text";
firstInput.name = "firstName" + count;
lastInput.type = "text";
lastInput.name = "lastName" + count;
table .appendChild(row);
row .appendChild(countCell);
countCell.appendChild(countText);
row .appendChild(firstCell);
firstCell.appendChild(firstInput);
row .appendChild(lastCell);
lastCell .appendChild(lastInput);
}
// ]]>
</script>
</head>
<body>
<form action="somewhere.php" method="post">
<table id="table">
<tr>
<th>Row</th>
<th>First</th>
<th>Last</th>
</tr>
</table>
<script type="text/javascript">
addRow();
</script>
<input type="button" value="Add" onclick="addRow()" />
<input type="submit" value="Submit" />
</form>
</body>
</html>