Hi I'm pretty new to PHP I need help with this small form I've made on my page.
I have a single checkbox which is in a loop. I seem to get only a single checkbox value passed. Here's my form and php code:
Form:
<?php if($show_captions) {
$pic_path=$album."/".$caption;
?>
<div class="caption" align="center">
<form name="store" id="str" action="store.php" method="get">
<input type="checkbox" name="pic[]" value="<?php echo $pic_path ?>"/>
</form>
</div>
<?php } ?>
store.php:
<?php
$path=$_GET['pic'];
if(isset($_GET['pic']))
{
foreach($path as $pic){
echo $pic;
}
}
?>
Related
<form action="addbusstop.php" method="POST" id="produceJSON" enctype="multipart/form-data">
<?php
echo "<div class = 'dropdown_container'><select class = 'dropdown select xs-mt1' name = 'line' id = 'line'>";
foreach($ptv_json_bus as $item)
{
$stopid = $item['stop_id'];
$stopname = $item['location_name'];
?>
<option value = "<?php echo $stopid;?>"> <?php echo $stopname;?></option>
<?php
}//end foreach
?>
<input type = "hidden" value ="<?php echo $stopid; ?>" name="stop_id" id="stop_id">
<input type = "hidden" value ="<?php echo $stopname; ?>" name="stop_name" id="stop_name">
</select>
<button type="submit" class="button button-latrobe-orange xs-ml2" onclick="changeState3()" id="submitstop2" name="submitstop2">OK</button>
</div>
</form>
Hey guys. Above is the code I'm working with.
What I am trying to achieve is to POST the selected value's stop ID and stop NAME (from a dropdown) to another file which then adds the data into mySQL.
What my problem is right now is that the current code I have is actually only POSTing the last set value of $stopid and $stopname which is not what I need.
I've been stuck on this for quite a while and I would love it it somebody here could point me in the right direction!!
Thank you
Move your <input> elements outside of the <select> element.
You can only put <option> elements inside of your <select> element.
I've improved your version a bit.
<form action="addbusstop.php" method="POST" id="produceJSON" enctype="multipart/form-data">
<div class = 'dropdown_container'>
<select class="dropdown select xs-mt1" name="line" id="line">
<?php
foreach ($ptv_json_bus as $item) {
$stopid = $item['stop_id'];
$stopname = $item['location_name'];
echo "<option value='$stopid'>$stopname</option>";
}
?>
</select>
<input type="hidden" value="<?php echo $stopid; ?>" name="stop_id" id="stop_id">
<input type="hidden" value="<?php echo $stopname; ?>" name="stop_name" id="stop_name">
<button type="submit" class="button button-latrobe-orange xs-ml2" onclick="changeState3()" id="submitstop2" name="submitstop2">OK</button>
</div>
</form>
I am using Mysql PDO query to fetch the required result and it is saved in and array $newfamily. Now with the help of this array I am implementing check-box with the given code-
<form method="get" action="specific_family.php">
<?php
foreach($newfamily as $name)
{
?>
<input type='checkbox'<?php echo $name;?>"><?php echo $name;?></input>
<?php
}
?>
</select> <input type = "submit" name="submit" >
</form>
Now in specific_family.php how can I retrieve al the selected check-box values in an array ?
Also when I use back button of browser I can see previously selected values as ticked. How can I remove this ?
Please guide.
The checkbox should have:
A label
The checkbox needs:
A name attribute
A value attribute
It must not have:
An end tag (unless you are using XHTML)
So:
<label>
<input type="checkbox"
name="new_families[]"
value="<?php echo htmlspecialchars($name); ?>">
<?php echo htmlspecialchars($name); ?>
</label>
The values of all the checked checkboxes will then appear in the array $_GET['new_families'] when the form is submitted.
If you add the name attribute to your input thus:
<form method="get" action="specific_family.php">
<?php
foreach($newfamily as $name)
{
?>
<label for="<?php echo $name?>"><?php echo $name?></label>
<input type='checkbox' name="<?php echo $name;?>" id="<?php echo $name;?>"></input>
<?php
}
?>
</select> <input type = "submit" name="submit" >
</form>
then your checkboxes will show up by name in your $_GET array.
Hope that helps :)
I would like to add an input element to the form dynamically using only PHP.
I know how to make this using php and JavaScript combination, thus do nto advice abotu JavaScript.
The example below does not work. Could you please advice and comment:
input.php
<br> <input type="text" name="mob[]" value="" size="3" >
form.php
<?php
if( isset($_POST['AddNum']) ){
$AddNumCount=$_POST['AddNumCount'];
$AddNumCount=$AddNumCount+1;
echo $AddNumCount;
}
if( isset($_POST['register']) ){
print_r($_POST['register']);
}
if (!isset($AddNumCount)) {$AddNumCount=5;}
?>
<form action="" method="post" id="form1" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" >
<br>
<?php for ($i=0; $i<$AddNumCount; $i++) { Include('input.php'); } ?>
<br> Add number: <input type="submit" name="AddNum" form="form1" value="Add NUmber"> </p>
<input type="hidden" name="AddNumCount" form="form1" value=" <?php $AddNumCount; ?> "> </p>
<br></form><input type="submit" name="register" id="regcont" value="register"> </p>
</form>
Maybe you know how to make single submit button for many forms?
I mean each input would be a separare form and all forms can be submittted with the button on the end?
You use two action attrs. Maybe you mean:
<form method="post" id="form1" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" >
For submitting many forms by one button - you need to use JavaScript and send them in cycle via AJAX.
I am sorry i change this post. This is working example for dynamical PHP.
Use EXform.php. Other files are generated or helping.
Maybe it is also possible to make this using Session variables and header for redirecting to regenerated webpage.
EXform.php
<?php if (isset( $_POST['AddNum'])) { Include("GENinput.php"); } ?>
<?php if (!isset( $_POST['AddNumCount'])) { $_POST['AddNumCount']=1; Include("GENinput.php"); } ?>
<form action="" method="post" id="form1" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" >
<?php Include("INCinput.php"); ?>
<br> Add number: <input type="submit" name="AddNum" form="form1" value="Add NUmber"> </p>
<input type="hidden" name="AddNumCount" form="form1" value="<?php echo $AddNumCount; ?>"> </p>
<input type="submit" name="register" id="regcont" value="register"> </p>
<br></form>
</form>
GENinput.php // generates included file
<?php
if( isset($_POST['AddNum']) ){
$AddNumCount=$_POST['AddNumCount']; //=
$fnameinp="INCinput.php";
$fileinp=fopen($fnameinp,"w");
$_POST['AddNumCount']=$AddNumCount=$AddNumCount+1;
//echo "AddNumCount=".$AddNumCount;
$strV=""; $stri="";
for ($i=0; $i<$AddNumCount; $i++) {
$strV.=" \n
<?php
if( isset(\$_POST['v']['tname']['colname'][".$i."]) )
{ \$v['tname']['colname'][".$i."]=\$_POST['v']['tname']['colname'][".$i."];}
else { \$v['tname']['colname'][".$i."]=".$i."; }
?>
";
$stri.=" <br> <input type=\"text\" name=\"v[tname][colname][".$i."]\" value=\"<?php echo \$v['tname']['colname'][".$i."]; ?>\" > \n\n";
}
fwrite($fileinp,$strV);
fwrite($fileinp,$stri);
fclose($fileinp);
}
I have a foreach that generate data from an sql select. When user click on the div a text message appears "points ganed" and the data inside the form will be submitted.
The entire script works but it always submit the last form on 4 generated... he doesnt target the right form.
There's a way to anchor the right form to submit? thanks in advance to all.
<script>
function(response) {
$('#mydiv').text('Points gained');
$("#form").submit()
}</script>
foreach ($usermeta as $post) {
<form method="post" id="form" action="<?php the_permalink(); ?>">
<div id="mydiv">
click here to earn points.
</div>
<input type="hidden" name="points" id="points" value="<?php echo $post->points; ?>" class="regular-text" />
</form>
}
Every single form has the same id. You need to make sure the id of the form is unique.
I don't have enough rep to comment, but this is what you could do.
<script>
$(document).ready(function() {
$('.mydiv').click(function() {
$(this).text('Points gained').closest('.form').submit();
});
});
</script>
<?php foreach ($usermeta as $post): ?>
<form method="post" class="form" action="<?php the_permalink(); ?>">
<div class="mydiv">
click here to earn points.
</div>
<input type="hidden" name="points" class="points" value="<?php echo $post->points; ?>" class="regular-text" />
</form>
<?php endforeach; ?>
I found a solution like this,
before the foreach i decleare that
$i = 1;
then
<form class="<?php echo $i; ?>">
then at the end of form but before the foreach end tag
$i ++;
so now i have
<form class="1">
<form class="2">
<form class="3">
<form class="4">
and so on, then i put a limit of display element from the sql select putting limit 10.
Hopefully I'm making this more difficault then it has to be. Here is what I'm trying to do. I have a form that does a POST and returns data. I have a second form that then asks the user a yes/no question based on data from the first form. Is it possible to capture the POST data from the first form submission and pass it along with the second form POST?
Here is my scenario
if ($_POST['button_1']) {
$params = $_POST;
print_r($_POST);
// process form data
}
if ($_POST['button_2']) {
// Retain the POST data from the first submission
$new_params = $params . $_POST;
print_r($new_params);
// process form data and do some additional stuff
}
<form id="form_1" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
...
<input type="submit" value="Button" name="button_1" id="button_1"/>
</form>
<form id="form_2" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
...
<input type="submit" value="Button" name="button_2" id="button_2"/>
</form>
Is there a way to do this easily or am I over complicating this?
You can either use a hidden field in your second form, or sessions. Start reading here:
http://www.php.net/manual/en/book.session.php
Yes, the standard way is to use type = "hidden" fields to pass this context data onto the second form. There are lots of worked examples to see how to do this. View the source of this HTML page or any other with forms on them and search for "hidden" to see how the applications do this.
There are a few ways to "fake" this.
Have the first form submit to itself and just load the $_REQUEST variables and use them to populate the second form with the appropriate data/options.
have the 2nd form loaded via ajax once the first is submitted and use javascript to grab the current form variables and provide them to the ajax function.
Which method would you prefer?
UPDATE:
This is long, but a working example
<!DOCTYPE html>
<html>
<body>
<form name="first" method="post">
<input type="hidden" name="action" value="firstformdone">
<div style="width:100px;float:left;">
Age:
</div>
<div style="width:200px;float:left;margin-left:20px;">
<input type="text" name="age">
</div>
<div style="clear:both;"></div>
<div style="width:100px;float:left;">
Name:
</div>
<div style="width:200px;float:left;margin-left:20px;">
<input type="text" name="name">
</div>
<div style="clear:both;"></div>
<br>
<?PHP
if (!$_REQUEST['action'] == "firstformdone") {
?>
<input type="submit" value="contine">
<?PHP
}
?>
</form>
<?PHP
if ($_REQUEST['action'] == "firstformdone") {
?>
<form name="second" action="something_else.php" method="post">
<input type="hidden" name="age" value="<?PHP echo $_REQUEST['age']; ?>">
<input type="hidden" name="name" value="<?PHP echo $_REQUEST['name']; ?>">
<div style="width:150px;float:left;">
Preferred games:
</div>
<div style="width:200px;float:left;margin-left:20px;">
<select name="games">
<option value="">Select games</option>
<?PHP
if ($_REQUEST['age'] <= 10) {
?>
<option value="tlddlywinks">Tiddly Winks</option>
<option value="Jacks">Jacks</option>
<option value="Go-Fish">Go-Fish</option>
<option value="Hid-And-Go-Seek">Hid-And-Go-Seek</option>
<?PHP
} else {
?>
<option value="Halo">Halo</option>
<option value="StarWars">The Old Republic</option>
<option value="LaserTag">Laser Tag</option>
<option value="spin-the-bottle">spin-the-bottle</option>
<?PHP
}
?>
</select>
</div>
<div style="clear:both;"></div>
<br>
<input type="submit" value="Next!">
</form>
<?PHP
}
?>
</body>
</html>
You need to package the data from the first form up for resubmission. You can use a hidden fields for that job:
foreach ($_POST as $key => $value) {
print "<input type='hidden' name='".htmlspecialchars($key, ENT_QUOTES, "UTF-8")."' value='".htmlspecialchars($value, ENT_QUOTES, "UTF-8")."'>";
}