How do i disable a button if the prod_quantity == 0?
<?php
$prod_qty = $row['prod_quantity'];
if ($prod_qty == '0'){
echo "<h1>sold out</h1>";
}
?>
and here is the button that i need to be disabled when prod_qty == 0
<input type="button" value="Add to Cart" onclick="addtocart(<?php echo $row["prod_id"]?>)" />
Add an if condition to the button to set the property as disabled if $prod_qty == '0' like shown below :
<input type="button" value="Add to Cart" <?php if ($prod_qty == '0'){ ?> disabled <?php } ?> onclick="addtocart(<?php echo $row["prod_id"]?>)" />
if($prod_qty==0)
{
<?php
<input type="button" value="Add to Cart" disabled />
<?php
}
UPDATE 2 :
<input type="button" value="Add to Cart"
<?php if($row["prod_qty"]==0)
{
echo ' onclick="addtocart('.$row["prod_id"].')" ';
}
else
{
echo ' disabled=disabled ';
}
?>
/>
<?php
$prod_qty = $row['prod_quantity'];
if ($prod_qty == '0'){
echo "<h1>sold out</h1>";
echo '
<input
type="button"
value="Add to Cart"
onclick="addtocart('.$row['prod_id'].')"
disabled
/>'
}else{
<input type="button" value="Add to Cart" onclick="addtocart(<?php echo $row["prod_id"]?>)" />
}
?>
try this
javascript onclick function...
function addtocart(prod_qty){
var quantity=parseInt(prod_qty);
if (quantity == 0){
document.getElementById("addtocartButton").disabled = true;
}
}
your button
<input type="button" value="Add to Cart" id="addtocartButton" onclick="addtocart(<?php echo $row["prod_id"]?>)" />
<?php
$prod_qty = $row['prod_quantity'];
if ($prod_qty == '0'){
echo "<h1>sold out</h1>";
}
?>
<input type="button" value="Add to Cart" onclick="addtocart(<?php echo $row["prod_id"]?>)" {{($prod_quantity == 0) ? 'disabled' : ''}} />
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>
I am trying to get the selected radio button's value. These values are fetched from a MySQL database bound to the controls. When the program executes, it returns null when I check the selected radio buttons value.
Help is highly appreciated. Thanks in advance.
Snippets of my file are as follows:
HTML:
<form method="POST" action="">
<h3><?php echo $_SESSION['Question'] ?></h3>
<input type="radio" name="radio" value="<?php $_SESSION['Option1'] ?>"><?php echo $_SESSION['Option1'] ?><br/>
<input type="radio" name="radio" value="<?php $_SESSION['Option2'] ?>"><?php echo $_SESSION['Option2'] ?><br/>
<input type="radio" name="radio" value="<?php $_SESSION['Option3'] ?>"><?php echo $_SESSION['Option3'] ?><br/>
<input type="radio" name="radio" value="<?php $_SESSION['Option4'] ?>"><?php echo $_SESSION['Option4'] ?><br/>
</div>
</div>
<div class="row">
<input type="submit" class="btn btn-success" name="button_start" value="Start"/>
<input type="submit" class="btn btn-success" name="button_Back" value="Back"/>
<input type="submit" class="btn btn-success" name="button_save" value="Save"/>
<input type="submit" class="btn btn-success" name="button_Next" value="Next"/>
<input type="submit" class="btn btn-danger" name="button_Submit" value="Submit"/>
</div>
</form>
PHP:
function Save($question_id)
{
if (isset($_POST['button_save'])) {
if (isset($_POST['radio'])) {
$selectedValue = $_POST['radio'];
if (is_null($selectedValue) || $selectedValue == "") {
echo "Please select an option";
} else {
$connectionString = mysqli_connect("localhost", "root", "", "knowellaptitudetest");
if (!$connectionString) {
echo "Error unable to connect to MySQL" . PHP_EOL;
echo "Debugging error no." . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error" . mysqli_connect_error() . PHP_EOL;
exit;
}
if ($selectedValue == $_SESSION['Correct_Answer']) {
//save and update marks for correct ans
$marks = 1;
$query = "INSERT INTO section_marks VALUES('" . $question_id . "','" . $marks . "')";
$result = mysqli_query($connectionString, $query);
$row = mysqli_fetch_row($result, MYSQLI_ASSOC);
$count = mysqli_num_rows($result);
} else {
//save and update marks for wrong answer
$marks = -1;
$query = "INSERT INTO section_marks VALUES('" . $row['Question_ID'] . "','" . $marks . "')";
$result = mysqli_query($connectionString, $query);
$row = mysqli_fetch_row($result, MYSQLI_ASSOC);
$count = mysqli_num_rows($result);
}
mysqli_close($connectionString);
}
}
You have to echo the php variable in html.
<input type="radio" name="radio" value="<?php echo $_SESSION['Option1'] ?>"><?php echo $_SESSION['Option1'] ?><br/>
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>
Neither of the divs and buttons defined after the while loop are visible until the timer goes down. I don't know what's the problem. I tried it outside the php also but still it shows nothing. Everything was working well before adding this php code to display a number of distinct questions selected from a database randomly. Someone please help me. I'm a newbie in php. Thanks a lot in advance.
<?php
$record=array(); //creating array to keep record of Q_ID of questions
for($count=1;$count <= $NoOfQuestion;$count++)// to display $NoOfQuestion number of questions
{
while(1)
{
$sql=mysql_query("select * from questions order by RAND()");
$result = mysql_fetch_array($sql);
$temp=0;
if(count($record)>=1)
{
for($i=1;$i<=$count;$i++)
{
if($record[$i]==$result['Q_ID'])
$temp++;
}
}
if($temp==0)
{
$text ='Question '.$count. '. '.$result['Question'];
echo $text;
return 0;
}
else
return 1;
}
echo'</div>';
echo'<div id="option"></div>';
echo'<div id="buton">';
echo'<input type="submit" name="Previous" value="previous" id="previous"/>';
echo'<input type="submit" name="Next" value="next" id="next"/>';
echo'<input type="submit" name="Review" value="review" id="review"/>';
echo'<input type="submit" name="Submit" value="submit" id="submit"/>';
echo'</div>';
echo'<div id="div3">3</div>';
echo'</div>';
echo'</form>';
}
?>
Modify your code....
echo'<div id="option"></div>';
echo'<div id="buton">';
echo'<input type="submit" name="Previous" value="previous" id="previous"/>';
echo'<input type="submit" name="Next" value="next" id="next"/>';
echo'<input type="submit" name="Review" value="review" id="review"/>';
echo'<input type="submit" name="Submit" value="submit" id="submit"/>';
echo'</div>';
echo'<div id="div3">3</div>';
Try
<?php
$record=array(); //creating array to keep record of Q_ID of questions
for($count=1;$count <= $NoOfQuestion;$count++)// to display $NoOfQuestion number of questions
{
while(1)
{
$sql=mysql_query("select * from questions order by RAND()");
$result = mysql_fetch_array($sql);
$temp=0;
if(count($record)>=1)
{
for($i=1;$i<=$count;$i++)
{
if($record[$i]==$result['Q_ID'])
$temp++;
}
}
if($temp==0)
{
$text ='Question '.$count. '. '.$result['Question'];
echo $text;
return 0;
}
else
return 1;
}
}
echo '</div>';
echo '<div id="option"></div>';
echo '<div id="buton">';
echo '<input type="submit" name="Previous" value="previous" id="previous"/>';
echo '<input type="submit" name="Next" value="next" id="next"/>';
echo '<input type="submit" name="Review" value="review" id="review"/>';
echo '<input type="submit" name="Submit" value="submit" id="submit"/>';
echo '</div>';
echo '<div id="div3">3</div>';
echo '</div>';
echo '</form>';
?>
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"; } ?>