I need to copy the input text to an array like this:
If the input is "12345":
<input type="text" name="data1">
Then in text input (array) also written "12345"
<? foreach ($countries as $data2)
{
<input type="text" name="$data2['location'][]">
}
<script language="javascript">
function copy()
{
document.form1.data2.value=document.form1.data1.value
}
</script>
<form action="" method="post" name="form1">
<input type="text" name="data1" onKeyUp="copy()"/>
<br /><br>
<input type="text" name="data2">
</form>
no problem...
But if like this:
<input type="text" name="data2[]">
<input type="text" name="data2[]">
<input type="text" name="data2[]">
OR in array PHP, like this :
<?php foreach ($countries as $data2): ?>
<input type="text" name="<?php echo $data2['location'][] ?>" />
<?php endforeach ?>
dont work...
It seems you need to open and close your PHP tags in the right place. If the name of your HTML input control is $data2['location'][], then try this:
<?php foreach ($countries as $data2): ?>
<input type="text" name="<?php echo $data2['location'][] ?>" />
<?php endforeach ?>
So, I've closed PHP mode before and after both the loop keywords (the start of the loop and the end of it). This means that everything outside of those tags is in HTML mode, so to get the name of the control, we need to open the tags again. I've chosen to use <?php rather than <? because the latter can be turned off on some servers. However, it is fine to stick with the short form if you don't mind that the code is not portable.
Notice also that I've used the colon with the more explicit end keyword. This is a popular way of constructing loops in the view layer; for ordinary controller code, I suggest you stick to ordinary braces.
Related
I was wondering if it was possible to take HTML user input using PHP (preferably the ID or something I can use numbers in) and to save confusion just echo it back.
So I have some example code here:
<input type="number" maxlength="3" name="test" id="1">
<input type="number" maxlength="3" name="test" id="2">
<input type="number" maxlength="3" name="test" id="2">
What I was looking for is a way where I could use their input and well.... echo it back for now.
if you already know how to submit a form you can use php on the other side like this to echo it out
<form method="POST" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>">
<input type="text" name="name"><br>
<input type="submit" name="submit" value="Submit Form"><br>
</form>
<?php
if (isset($_POST['name'])){
$userinput = $_POST['name'];
echo $userinput;}
?>
in your example all three inputs are named "test" so youll need a different name for each one. My example above uses the "name" of the input to capture it. If your using "GET" change my $_POST['name'] to $_GET['name']
Which method did you use for these inputs? Name them differently, then retrieve the data with:
<?php echo $_GET['test1']; ?>
<?php echo $_GET['test2']; ?>
<?php echo $_GET['test3']; ?>
If you used POST type in the input method, then switch for:
<?php echo $_POST['test1']; ?>
<?php echo $_POST['test2']; ?>
<?php echo $_post['test3']; ?>
I have a form with multiple text inputs that all have the same name. How would I process that with my PHP when the user submits the form?
HTML:
<input type="text" name="interest"/>
I've assumed you're using POST.
You would use
<input type="text" name="interest[]">
Then on the post page, you could use:
foreach($_POST['interest'] as $i){
echo $i. "<br>";
}
or whichever method you wanted to use to get the POST data.
You could also do something like:
<input type="text" name="interest[music]"/>
<input type="text" name="interest[food]"/>
You can then call this data by using:
<?php echo $_POST['interest']['music']; ?>
<input type="text" name="interest[]"/>
You should add square brackets. This triggers PHP to put them in an array like this:
HTML
<input type="text" name="interest[]"/>
<input type="text" name="interest[]"/>
PHP
//Get values
var_dump($_POST['interest']);
Use brackets in your input field to create an array of POST values:
<input type="text" name="interest[]"/>
<?php
var_dump($_POST['interest']); // will be an array
?>
I've been two days searching how to do this. I have this form:
<form action="preview.php" method="post">
In the city of <input name="city" type="text" /> at <input name="days" type="text" /> days of <input name="month" type="text" /> gathered the following people: <input name="name1" type="text" /> and <input name="name2" type="text" /> with the objective of...
<button type="submit">Preview</button></form>
And preview.php should have this:
In the city of <?php echo $_POST['city'];?> at <?php echo $_POST['days'];?> days of <?php echo $_POST['month'];?> gathered the following people: <?php echo $_POST['name1'];?> and <?php echo $_POST['name2'];?> with the objective of...
The thing is the form is created via CMS so I have no way of knowing what the names of the inputs will be.
Is there a way to dinamically replace, for example, <input name="city" type="text" /> with <?php echo $_POST['city'];?>?
There are some thing I have in mind but since I'm new to PHP I don't know how to implement them.
Maybe preg_replace could do it but I don't know how to prevent the name of the input from changing.
Or maybe I could use an array, for example <input name="data[]" type="text" /> and something like:
for($i=0;"";$i++){
if( isset( $_POST["data{$i}"] )) {
$string = $form;
$patterns = '<input name="data[]" type="text" />';
$replacement = $_POST["data{$i};
preg_replace($patterns, $replacements, $string);
}
}
I'm really lost here and I'd appreciate it if someone could help me with this.
PS: I'm not a native english speaker son I'm sorry if I made some mistake.
UPDATE:
The user can make different forms in the CMS that will be saved in a database. Then he can choose which one he wants to fill. Once he fills it he can preview it and save it as a pdf.
The preview.php will have the same text in the form but instead of the inputs it will have the value that the user entered.
(I have been a bit out of touch with PHP.)
Capture the HTML of the form with ob_ functions (output buffer).
if ($_SERVER['REQUEST_METHOD'] == 'POST' {
ob_start();
... the CMS outputs HTML
ob_end_flush();
$s = ob_get_contents();
Use the name attribute to replace things
function postText($matches) {
return $_POST[$matches[1]];
}
$s = preg_replace_callback('/<[^>]* name="([^">]+)"[^>]*>/',
"postText",
$s);
echo $s;
This uses a function for replacing a HTML tag with a name attribute.
I call a PHP script from my HTML form submit.
Then I process the values retrieved from this HTML form in the PHP script and now I want to display these values in another HTML form that I am calling from this PHP script.
How do I retrieve this variable?
Note: I tried echo but I think I actually want to display this value in the form (HTML) and not break it again in a PHP tag.
I'm not sure what you mean by "not breaking it again with a PHP tag". HTML on its own cannot access PHP variables. This is because PHP is a server-side language and HTML is a client side language. But here is the simplest way to print php variables retrieved from a form.
<form>
<p>Your name: <?php echo $_POST['name'] ?></p>
</form>
This is assuming that there was a form that submitted a field called 'name' using POST.
You can process the name in a php script at the top of the file and then simply echo it when you're printing the html. This way, you won't have too much php code mixed in with the HTML (which makes it look cleaner).
Once you got the values in the PHP script, are you calling a new script? If so, you might wanna save the values in $_SESSION["varible_name"]. If not, you just have to echo it.
It depends on how you are accessing your form data, either through $_POST or through $_GET. For simplicity, I'll assume your using $_GET and modify this example for more clarity.
So lets say you have a form hosted on welcome.php:
<form action="welcome.php" method="get">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
Now the results will be returned back to the same page, so you want to modify the page to say:
<form action="welcome.php" method="get">
Name: <input type="text" name="fname" value="<?php echo $_GET["fname"]; ?>"/>
Age: <input type="text" name="age" value="<?php echo $_GET["age"]; ?>" />
<input type="submit" />
</form>
Though you'll notice that we're using the same page, and we can only have one version of the page, so we want to render if upon the condition that our variable has been set.
if (isset($_GET["fname"])){
//code to print second form
}
else{
//code to print first form
}
Or, in another way (using the ternary operator):
<form action="welcome.php" method="get">
Name: <input type="text" name="fname" value="<?php echo ((isset($_GET["fname"]))?$_GET["fname"]:""); ?>"/>
Age: <input type="text" name="age" value="<?php echo ((isset($_GET["age"]))?$_GET["age"]:""); ?>" />
<input type="submit" />
</form>
I'm learning php and I've got to say I've googled everywhere and I'm stuck :(
My question is, in other words, how can I get new variables added to previous variables on the page?
Imagine this being www.mysite.com/getvartest.php?orgvar=12345
but after submitting this form it'll go to
www.mysite.com/getvartest.php?varselection=selection1
What I would expect would be:
Imagine this being www.mysite.com/getvartest.php?orgvar=12345&varselection=selection1
How could I resolve this?
<p>Testing two ore more variables</p>
<form action="getvartest.php?orgvar=12345" method="get">
<select name="varselection">
<option value="selection1">selection1</option>
<option value="selection2">selection2</option>
</select>
<input type="submit" />
</form>
<?php
#$originalvar = $_GET['orgvar'];
#$varselection = $_GET['varselection'];
if($originalvar&&$varselection)
echo "Testing original variable $originalvar. Testing second passthrough variable through here: $varselection";
?>
Instead of submitting to an action URL with part of the query string already filled out, use a hidden input field to hold the values of the variables you want to be fixed. See if you can get your PHP script to generate a form like this:
<form action="getvartest.php" method="get">
<input type="hidden" name="orgvar" value="12345" />
<select name="varselection">
<option value="selection1">selection1</option>
<option value="selection2">selection2</option>
</select>
<input type="submit" />
</form>
Perhaps something like:
<input type="hidden" name="orgvar" value="<?= htmlspecialchars($_GET['orgvar']) ?>" />
(The htmlspecialchars() call escapes characters like <, &, and " just in case there are any of these in orgvar.)
Or if you wanted to save every query parameter you could use a generic loop like:
<?php foreach ($_GET as $name => $value) { ?>
<input type="hidden" name="<?= htmlspecialchars($name) ?>"
value="<?= htmlspecialchars($value) ?>" />
<?php } ?>