more than 1 form at one page - php

I've problem with multiple form at one page. At page index I include 4 forms
include('./poll_1a.php');
include('./poll_2a.php');
include('./poll_3a.php');
include('./poll_4a.php');
The form code at every poll page is the same. I include some unique markers ($poll_code) for every scripts but the effect is when I use one form - the sending variable are received in the others. But I would like to work each form individually.
The variable $poll_code is unique for every script -> 1 for poll_1, 2 for poll_2 etc.
The same situation is with $cookie_name
$cookie_name = "poll_cookie_".$poll_code;
than, as I see, cookies have different names.
$poll_code = "1"; // or 2, 3, 4
?>
<p>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" name="<?php echo $poll_code; ?>">
<input type="hidden" name="poll_cookie_<?php echo $poll_code; ?>" value="<?php echo $poll_code; ?>">
<table>
<?php
//print possible answers
for($i=0;$i<count($answers);$i++){
?><tr><td style="\text-allign: left;\"><input type="radio" name="vote_<?php echo $poll_code; ?>" value="<?php echo $i; ?>"> <?php echo $answers[$i]; ?></td></tr><?php
}
echo "</table>";
echo "<br>";
if ($_COOKIE["$cookie_name"] == $poll_code ) {
echo "<br> nie można głosować ponownie ...";
} else {
?>
<p><input type="submit" name="submit_<?php echo $poll_code; ?>" value="głosuj !" onClick="this.disabled = 'true';"></p>
<?php
}
?>
</form>
</p>
Q: how to make this forms to work individually at one page?
//------------------- EDIT
the receiving part of the script
$votes = file($file);
$total = 0;
$totale = 0;
$poll_cookie = 0;
if (isset($_POST["vote_$poll_code"]) && isset($_POST["poll_cookie_$poll_code"])) {
$vote = $_POST["vote_$poll_code"];
$poll_cookie = $_POST["poll_cookie_$poll_code"];
}
//submit vote
if(isset($vote)){
$votes[$vote] = $votes[$vote]+1;
}
//write votes
$handle = fopen($file,"w");
foreach($votes as $v){
$total += $v;
fputs($handle,chop($v)."\n");
}
fclose($handle);
Of course, the $file have the unique declaration too (at top of the script, under the $poll_code declaration).
$file = "poll_".$poll_code.".txt";

I think the issue might be that <?php echo $poll_code; ?> is outside the loop so maybe that it's always using the same value assigned to it, maybe put it inside the loop

Related

Problems when using nested foreach on checkboxes loaded dynamically

I need to use nested foreach for dependent checkboxes.
<input type="checkbox" name="all[]" value="<?php echo $row_bus_details['busid'];?>" >
<?php
$book_side_result = mysqli_query($db,"select * from advt_sides");
while($book_side_row=mysqli_fetch_array($book_side_result))
{
?>
<input type="checkbox" name="bookingside[]" value="<?php echo $book_side_row['advt_side_id']; ?>" id="<?php echo $book_side_row['advt_side']; ?><?php echo $row_bus_details['busid'];?>" > <?php echo $book_side_row['advt_side']; ?><br/>
<?php } ?>
I need to loop the selected values of second checkbox if the first checkbox is selected.
I wrote the code like
$i = 0;
$busid = isset($_POST['all']) ? $_POST['all'] : array();
foreach ((array)$busid as $item) {
if(!empty($_POST['bookingside'])) {
foreach($_POST['bookingside'] as $side) {
$sql_book_side=mysqli_query($db,"INSERT INTO `advt_book_side`(bus_id,sides_id) VALUES ('$item','$side')");
$i++;
}
}
}
The result I need is just like the image below
You need to save data in serialize array from in data base like:
$sql_book_side=mysqli_query($db,"INSERT INTO advt_book_side(bus_id,sides_id) VALUES ('$item',serialize(array('left'=>1,'right'=>1,'back'=>0)))");
Print check box with check uncheck using below code
$book_side_result = mysqli_query($db,"select * from advt_sides");
while($book_side_row=mysqli_fetch_array($book_side_result))
{
$array = unserialize($book_side_row['sides_id']);
foreach($array[0] as $side){
?>
<input type="checkbox" name="bookingside[]" value="<?php echo ($side)? $side:0; ?>">
<?php }
} ?>

How to keep value in variable after refresh page in PHP?

I writing simple game. I randomizes two numbers and then user put your sum value number to input form whose I sent on the same site. Now if user guess sum numbers it he get 10 points, but if he will miss, lost one chance.
I have problem, because if i Refresh site i overiate he points and chance. Any solution?
$_SESSION['points'];
//$_SESSION['chance'];
$X = rand(1, 10);
$Y = rand(1, 10);
echo $X . " " . $Y;
?>
<form name="MyForm" action="" method="POST">
<input type="text" name="value">
<input type="hidden" value="<?= $X ?>" name="number">
<input type="hidden" value="<?= $Y ?>" name="number2">
<input type="submit" value="Generuj" name="Wynik?"/>
</form>
<?php
if (!empty($_POST) && isset($_POST)) {
$user = $_POST['value'];
if ($_POST['number'] * $_POST['number2'] == $user) {
echo ' ok !';
echo $_SESSION['points'] += 10;
} else {
echo 'error';
// echo 'You lost one chance' . $_SESSION['chance']--;
}
}
You can simply use a PHP session OR a PHP Cookie.
Using cookies (if information is not sensible)
<?php
$value = 'something from somewhere';
setcookie("TestCookie", $value);
setcookie("TestCookie", $value, time()+3600); /* expire in 1 hour */
?>
Then you can echo your cookie like this
<?php
echo $_COOKIE['TestCookie'];
?>
If you're using Sessions
<?php
session_start();
$_SESSION['myVar'] = $myValue;
?>
Then you can echo it that way
<?php
echo $_SESSION['myVar'];
?>
NOTE that with the session, you always have to start your session at the beginning of your PHP file.

php code for button counter

Am trying to run a php code which counts button clicks. It increments to 1 and then it doesn't count. here is my code:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$counter = isset($_POST['counter']) ? $_POST['counter'] : 0;
if(isset($_POST["button"])){
$counter++;
echo $counter;
}
}
?>
<form action = "<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method = post>
<input type = "submit" name = "button" value = "vote" >
</form>
Am not a php expert so can anybody please tell me where am going wrong?
Thanks
Use this php:
<?php
if(isset($_POST["button"])){
$counter++;
echo counter;
}
?>
Also, use this for the opening <form> element
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Try this:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$counter = isset($_POST['counter']) ? $_POST['counter'] : 0;
if(isset($_POST["button"])){
$counter++;
echo $counter;
}
}
?>
<form action = "<?php echo $_SERVER["PHP_SELF"]; ?>" method = post>
<input type = "hidden" name = "counter" value = "<?php echo $counter; ?>" />
<input type = "submit" name = "button" value = "vote" />
</form>
Store the count in a session. Note that this stores a separate count for each user. If you want one single count that is shared between all users then you'll need to store it in a database.
<?php
// Start the session
session_start();
// Make sure a session variable exists
if ( !isset($_SESSION['count']) ) {
$_SESSION['count'] = 0;
}
// Check to see if a vote has been submitted
$vote = isset($_POST['button']) ? $_POST['button'] : false;
if ( $vote ) {
// Increment the vote
$_SESSION['count']++;
}
?>
<form action="<?php echo $_SERVER["PHP_SELF"] ?>" method="post">
<input type="submit" name="button" value="Vote">
</form>

created input tag in php for loop and get value

I want to create limit input tag with php for loop an get inputs values. My sample code is this:
<?php
$limit = 10;
for ($i=1; $i<=$limit; $i++) { ?>
<input name="<?php echo $i ?>" type="text" /><br>
<?php } ?>
Is my code correct? How can I get input values?
You can try my script.
<?php
$limit = 10;
?>
<form method="post">
<?php
for ($i = 1; $i <= $limit; $i++) {
?>
<input name="anything[]" type="text" /><br>
<?php } ?>
<input type="hidden" name="op" value="sent" />
<input type="submit" value="submit" />
</form>
<?php
if (!empty($_POST["op"])) {
for ($i = 1; $i <= $limit; $i++) {
if (strlen($_POST["anything"][$i]) !== 0) {
?>
<p>The value of the <?php echo $i; ?> text field is: <?php echo $_POST["anything"][$i]; ?>
<?php
} else {
?>
<p><?php echo $i; ?> was not set.</p>
<?php
}
}
}
Looks alright but I would use an array as the input name. For example:
<?php
$limit = 10;
for ($i=1; $i<=$limit; $i++) {
?>
<input name="number[<?php echo $i; ?>]" type="text" /><br>
<?php
}
?>
That way in the back end you can just loop through the number array like so.
foreach ($_POST['number'] as $key => $value) {
// Do stuff
}
Your code should render 10 html input fields with name 1, 2, 3, ... 10 correctly
To get input values, wrap your input fields in a form element with an action pointing to the php script in which you want to read the values (e.g. action="myscript.php").
(You should add a input type="submit" to have a way to submit the form. I assume you know HTML good enough to create a simple form.)
The script invoked by submitting the form (e.g. myscript.php) will now be able to read the values using the $_GET array. See http://php.net/manual/de/reserved.variables.get.php
You could print the values like so:
<?php
for($i=1;$i<=10; $i++) {
echo $i . ' : '. $_GET[$i];
}
?>
Edit: As #David Jones mentioned it would be better to use an array as input name

How to create isset($_POST['id'.$var]) within loops?

I am trying to create a page where there are several fields and users can comment on each one. To create these fields and text inputs, I am running a while loop with the following html within it:
<form name = "replyform" method = "post" action = "">
<input id = "replytext<? echo $replyID; ?>" value = "replytext<? echo $replyID; ?>" name = "replytext<? echo $replyID; ?>" type="text" class = "span5">
</form>
And then using the following code to call the 'wall_reply()' function, submitting the text values.
if (isset($_POST['replytext'.$replyID])) {
echo wall_reply();//5, $_POST['replytext'.$replyID]);
}
Something's a miss though. Any ideas what could be wrong here?
You have loop to create these form and input?
put the loop inside the form tag, so that only one form will be created with multiple inputs.
this seem to work correctly, use it as your guide ;)
<?php
$maxposts=7;
if (isset($_POST['submit'])){
function wall_reply($id,$text){
echo '<hr />updating id '.$id.' with '.$text;
}
var_dump($_POST);
for ($i=0;$i<$maxposts;$i++){
$replyID = $i;
if (isset($_POST['replytext'.$replyID])) {
wall_reply($i,$_POST['replytext'.$replyID]);//5, $_POST['replytext'.$replyID]);
} else {
echo 'not set';
}
}
}
?>
<form name = "replyform" method = "post" action = "">
<?php
$replyID = 5;
for ($i=0;$i<$maxposts;$i++):
$replyID = $i;
?>
<input id = "replytext<?php echo $replyID; ?>" value = "replytext<?php echo $replyID; ?>" name = "$
<?php endfor; ?>
<input type="submit" name="submit" value="go"/>
</form>

Categories