I'm quite new to php so this might seem very easy. I have a table called 'products' and for each product in that table I want to create a button with the id of that product. I have no problem creating the buttons but I can not see which of the buttons has been pressed.
How can I solve this?
This is my code:
$sql = "SELECT id FROM `products` WHERE subcategory = 'laptop' ORDER BY id desc limit 1";
$query = mysql_query($sql);
$id = mysql_result($query,0);
for($i=1; $i<= $id; $i++){
$product2 = R::load('products', $i);
echo "<input type='submit' name='$product2->id' value='Add to cart'/>";
}
Thank you !
assign a value to the button/input
<input type="submit" name="btn" value="button1" />
<input type="submit" name="btn" value="button2" />
<input type="submit" name="btn" value="button3" />
<?php echo filter_input(INPUT_POST, 'btn'); ?>
or
<input type="submit" name="btn1" value="button1" />
<input type="submit" name="btn2" value="button2" />
<input type="submit" name="btn3" value="button3" />
<?php if (filter_has_var(INPUT_POST, 'btn1')) { echo "button 1 clicked"; } ?>
Related
how can i submit a pre checked checkbox on submit?
right now i have
$checked= 'checked="checked"';
<input type="checkbox" name="entity_id['.$row['entity_id'].']" value="Yes" '.$checked.'>
on submit, this is not getting posted, only when checked/unchecked by hand.
my $checked is actually coming from a for-each mysql query.
any help is appreciated.
UPDATE
<form method="post">';
if($hass_lights == 'Yes'){$user->getLights($userid);}
exit('
<br><b>Google is requiring access to your basic profile information.</b><br><br>
<input type="submit" class="btn btn-text" style="height:40px; width:100px" name="authorized" value="Allow" /> <input type="submit" class="btn btn-text" style="height:40px; width:100px" name="authorized" value="Deny" />
</form>
');
function getLights($userID){
$stmt = $this->db->prepare("SELECT * FROM hass_entities WHERE id = :id AND devicetype= 'light' ORDER BY friendly_name ASC");
$stmt->bindParam(':id',$userID);
$stmt->execute();
$userData = $stmt->fetchAll();
echo '
<div class="container">
<button type="button" class="btn btn-info" data-toggle="collapse" style="width:120px" data-target="#lights">Lights</button>
<br/><div id="lights" class="collapse"><br/><table border=0>';
foreach( $userData as $row ) {
if($row['enabled'] == 'Yes'){
$checked = 'checked="checked"';
}else{
$checked = '';
}
echo '<tr><td><label id="'.$row['entity_id'].'">'.$row['friendly_name'].' </label></td><td><input type="checkbox" name="entity_id['.$row['entity_id'].']" value="Yes" '.$checked.'></td></tr>';
}
echo '</table></div></div><br/>';
}
-Dennis
Try this:
<input type="checkbox" name="entity_id['.$row['entity_id'].']" value="Yes" checked>
Your input field is been written in an echo statement? Would be good to see the entire form, but here is your code working:
<form method=POST>
<?php
var_dump($_POST);
$row = [];
$row['entity_id'] = 1;
$checked= 'checked="checked"';
echo '<input type="checkbox" name="entity_id['.$row['entity_id'].']" value="Yes" '.$checked.'>';
?>
<button type="submit">submit</button>
</form>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<ul>
<input type="radio" name="team" id="1" value="Real Madrid" size='5'><font size='5'><u> Real Madrid<br>
<input type="radio" name="team" id="2" value="Chelsea"> Chelsea<br>
<input type="radio" name="team" id="3" value="Milan"> Milan<br>
<input type="button" id="submit" name="submit" value="VOTE" >
</form>
this is my button in html
if(isset($_POST['submit'])){
$selected_radio = $_POST['id'];
$query = "UPDATE favourite_team SET likes = likes + 1 WHERE id = '" . $selected_radio . "'";
$q = mysqli_query($conn, $query);}
this is my code in php
Name is 'team'. Not, 'id'. Use this code. Give id value to value attribute
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<ul>
<input type="radio" name="team" value="1" size='5'><font size='5'><u> Real Madrid<br>
<input type="radio" name="team" value="2" > Chelsea<br>
<input type="radio" name="team" value="3" > Milan<br>
<input type="button" id="submit" name="submit" value="VOTE" >
</form>
<?
if(isset($_POST['submit'])){
$selected_radio = $_POST['team'];
$query = "UPDATE favourite_team SET likes = likes + 1 WHERE id = '" . $selected_radio . "'";
$q = mysqli_query($conn, $query);}
?>
You need to write $_POST['team']. Try this code:-
$selected_radio = $_POST['team'];
$query = "UPDATE favourite_team SET likes = likes + 1 WHERE id = '" . $selected_radio . "'";
$q = mysqli_query($conn, $query);
Check this link for more detail.
you need to change the value of the radio to be the id this mean value='1'
and need to change
$selected_radio = $_POST['id']; to
$selected_radio = $_POST['team'];
and change input type to submit type='submit'
<input type='submit' id='submit' name='submit' value='VOTE'>
i hope this will fix your problem
Actually i want to develop a application from where user can set attendance for student. so the html form for attendance will come from my db query. and it's coming too but the prob is that how can i insert that form information to my db . actually i searched lot but i didn't get any result for this as perfect as i want i mean please can anyone help me . thanks in advance
<form action="attendance.php" method="post">
<?php include '../database-config.php';
foreach($dbh->query("SELECT * FROM student WHERE active_class='VII'") as $row){
echo "<div>
<label>".htmlentities($row['student_id'])."</label>
<input type='radio' name='atten".htmlentities($row['student_id'])."' checked='checked'>Present
<input type='radio' name='atten".htmlentities($row['student_id'])."'>Absent
</div></br>";
}
?>
<button type="submit" class="btn btn-success btn-lg">Submit</button>
<button type="reset" class="btn btn-danger btn-lg">Reset</button>
</form>
<form action="attendance.php" method="post">
<?php include '../database-config.php';
$result = mysql_query("SELECT * FROM student WHERE active_class='VII'");
foreach($result as $row)
{
?>
<div>
<label><?php echo $row['student_id']?></label>
<input type="radio" name="attend" value="present" checked>Present
<input type="radio" name="attend" value="absent">Absent
</div>
</br>
<?php
}
?>
<button type="submit" class="btn btn-success btn-lg">Submit</button>
<button type="reset" class="btn btn-danger btn-lg">Reset</button>
</form>
so in php you can get value like this
<?php
$attend = $_POST['attend'];
echo $attend;
?>
So in $attend it contain value(value="present") of radio button.
it may be present or either absent
damn getting tired xD
this should work though but you have to add the column attendency to the database table by yourself cheers
<form action="" method="post">
<?php
include '../database-config.php';
if(isset($_POST['attendency']) && isset($_POST['id']))
{
$id_to_update = $_POST['id'];
$status = $_POST['attendency'];
$ar = array('p','a');
$attend = !empty($status) && in_array($status,$ar) ? $status : 'p';
//you have to create a column named attendency for this to work
$sql = "INSERT INTO student(attendency) VALUES ('$attend ') WHERE user_id = '$id_to_update '";
$dbh->query($sql);
}
foreach($dbh->query("SELECT * FROM student WHERE active_class='VII'") as $row)
{
if($row['attendency'] == 'p')
{
$p = 'checked="checked"';
$a = '';
} else {
$a = 'checked="checked"'
$p = '';
} ?>
<div>
<input type="hidden" name="id" value="<?=$row['student_id']?>">
<label><?=$row['student_id']?></label>
<input type='radio' name='attendency' <?=$p?>>Present
<input type='radio' name='attendency' <?=$a?>>Absent
</div></br>
<?php } ?>
<button type="submit" class="btn btn-success btn-lg">Submit</button>
<button type="reset" class="btn btn-danger btn-lg">Reset</button>
</form>
I am trying to display mysql records through hidden field values, but nothing displays.
A little help!
Here's the code;
Html:
<form name="form11" method="post" action="hpdata.php" enctype="multipart/form-data">
<input name="pro" id="pro" type="hidden" value= "CMS" />
<input name="piror" id="piror" type="hidden" value= "P1" />
<input name="stat" id="stat" type="hidden" value= "In Progress" />
<input type="submit" name="submit" id="submit" class="groovybutton" value="...">
</form>
PHP:
<?php
$project = $_POST["pro"];
$pirority = $_POST["piror"];
$status = $_POST["stat"];
mysql_connect ("one", "two", "three");
mysql_select_db ("wsms");
$rest = mysql_query("SELECT * FROM sheet WHERE project='$project' AND
pirority='$pirority' AND status='$status'");
while($row = mysql_fetch_array($rest))
{
echo $row['id'] . " " . $row['date']; echo "<br>";
}
?>
Put isset into your php code
Example
<?php
if(isset($_POST['submit'])){
echo $project = $_POST["pro"]."<br>";
echo $pirority = $_POST["piror"]."<br>";
echo $status = $_POST["stat"];
/* mysql_connect ("one", "two", "three");
mysql_select_db ("wsms");
$rest = mysql_query("SELECT * FROM sheet WHERE project='$project' AND
pirority='$pirority' AND status='$status'");
while($row = mysql_fetch_array($rest))
{
echo $row['id'] . " " . $row['date']; echo "<br>";
}*/
}
?>
<form name="form11" method="post" action="" enctype="multipart/form-data">
<input name="pro" id="pro" type="hidden" value= "CMS" />
<input name="piror" id="piror" type="hidden" value= "P1" />
<input name="stat" id="stat" type="hidden" value= "In Progress" />
<input type="submit" name="submit" id="submit" class="groovybutton" value="...">
</form>
Output
CMS
P1
In Progress
First of all check that if the data is coming in the post or not:
<?php
echo "<pre>";
print_r($_POST);
exit;
?>
If yes than remove the print code i provided , and use extract($_POST); at the top of your PHP code. You query will become like this:
$rest = mysql_query("SELECT * FROM sheet WHERE project='$pro' AND
pirority='$piror' AND status='$stat'");
I'd like that the variable $order is increase by 1 every time I push the submit button.
(The name of the page is study.php so every time I push the button the page is refreshed):
<?php
$order = $_GET['number'];
echo "<form action='study.php' method='GET'>
<input type='hidden' name='number' value='$order++' />
<input class='big_b' type='submit' value='next' />
</form>";
echo "$order";
?>
The first time 1 push the button $order is 1, the second 2, the third is 3 ... etc
Thanks for your help!
EDIT: SOLVED
<?php
session_start();
if(empty($_SESSION['count'])) $_SESSION['count'] = 0;
$order = $_SESSION['count']+1;
$_SESSION['count'] = $order;
echo "<form action='study.php' method='GET'>
<input class='big_b' type='submit' value='next' />
</form>";
echo "$order";
?>
How you currently have it, is that it will increment on each page refresh regardless whether button was clicked or not, do you need it only to increment if the button is pressed?
<?php
session_start();
// Reset to 1
if(isset($_POST['reset'])){unset($_SESSION['number']);}
// Set or increment session number only if button is clicked.
if(empty($_SESSION['number'])){
$_SESSION['number']=1;
}elseif(isset($_POST['next'])){
$_SESSION['number']++;
}
echo '
<form action="" method="POST">
<input class="big_b" type="submit" name="next" value="Next" />
<input type="submit" name="reset" value="Reset" />
</form>';
echo $_SESSION['number'];
?>
<?php
session_start();
if(empty($_SESSION['order'])){
$_SESSION['order'] = 1;
}
echo "<form action='study.php' method='GET'>
<input type='hidden' name='number' value='".$_SESSION['order']++."' />
<input class='big_b' type='submit' value='next' />
</form>";
echo $_SESSION['order'];
?>
You need to put $order++ outside of the quotation marks to make an operation (incrementing by 1). Here's the code:
<?php
$order = $_GET['number'];
echo "<form action='study.php' method='GET'>
<input type='hidden' name='number' value='".$order++."' />
<input class='big_b' type='submit' value='next' />
</form>";
echo "$order";
?>