Buttons defined inside php are not visible - php

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>';
?>

Related

post pre checked checkbox on form submit PHP MYSQL

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>

Disable the submit button after 5 or more submission [duplicate]

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' : ''}} />

php-mysql insert multiple rows from echo radio button

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>

How can I see which button has been pressed in php?

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"; } ?>

passing variable from a href to pop up box through php

after i click on this a href, it ll pop out a form and i trying to pass a variable to the pop up form. This is in a while loop.
while($row = mysql_fetch_array($result))
{
$id=$row['file_id'];
echo '<span class=right>[edit]
}
This is the pop up form code
<?php
$con=mysql_connect("localhost","root","");
mysql_select_db("isiti");
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if (isset($_GET["id"]))
{
$id =$_GET["id"];
}
$result = mysql_query("SELECT * FROM publication where file_id='$id'");
$row1 = mysql_fetch_array($result);
$title=$row1['title'];
$author=$row1['author'];
$year=$row1['year'];
$abstract=$row1['abstract'];
if(!empty($_SESSION['username']))
{
echo'<div class="popup">';
echo'<form method="POST" action="public_edit.php" enctype="multipart/form-data">';
echo'<div>';
echo'<label for="citation">Title</label>';
echo'<input style="width:100%;" type="text" name="title" id="title" value="'.$title.'">';
echo'</div>';
echo'<div>';
echo'<label for="citation">Author</label>';
echo'<input style="width:100%;" type="text" name="author" id="author" value="'.$author.'">';
echo'</div>';
echo'<div>';
echo'<label for="citation">Year</label>';
echo'<input style="width:100%;" type="year" name="year" id="year" value="'.$year.'">';
echo'</div>';
echo'<div>';
echo'<label for="abstract">Abstract</label>';
echo'<textarea name="abstract" id="abstract" size="5000">'.$abstract.'</textarea>';
echo'</div>';
echo'<p>Rechoose your file here</p>';
echo'<input type="hidden" name="MAX_FILE_SIZE" value="2000000">';
echo'<input name="userfile" type="file" id="userfile"> ';
echo'<br/> ';
echo'<input name="submit" type="submit" value="Upload" style="width: 150px">';
echo'<a class="close" href="#close"></a>';
echo'</form>';
echo'</div>';
}
The problem i faced is that it do not pass the right id. no matter what link i click it show the data from the first id.
Thanks for helping me .
You need to pass the id as get parameter when calling that pop up. Replace with this the echo statement in first code
echo '<span class=right>[edit]';
$_GET works well for data sent by a query string. In this case, you are sending the data as the id of href tag.
so try attaching id to your query string.
echo '<span class=right>[edit]

Categories