There may be a better way to do this, but I'm trying to create a button group where the left and right buttons are increment/decrement, and the middle button is a disabled button representing the value.
The field properly increments/decrements, but the value isn't getting passed to php.
I do like how the button group looks:
HTML
<form method="POST" action="send_email.php">
<div class="row"">
<div class="col-sm-4 col-xs-4">
<h5><b>Attendees</b></h5>
</div>
<div class="col-sm-8 col-xs-8">
<div class="btn-group">
<input type="button" class="btn btn-default" name="decrease" value="-" onclick="decreaseBtnOnclick('attendees')"/>
<input type="button" class="btn btn-primary" name="attendees" value="0" id="chairs" disabled/>
<input type="button" class="btn btn-default" name="increase" value="+" onclick="increaseBtnOnclick('attendees')"/>
</div>
</div>
</div>
<div class="text-right">
<input type="hidden" name="email" value="contact">
<a href="./index.php">
<button type="submit" class="btn-main">Finish!</button>
</a>
</div>
</form>
Javascript (works fine to change the value in the middle button)
<script>
function increaseBtnOnclick(item) {
document.getElementById(item).value = Number(document.getElementById(item).value) + 1;
}
function decreaseBtnOnclick(item) {
if(document.getElementById(item).value > 0){
document.getElementById(item).value = Number(document.getElementById(item).value) - 1;
}
}
PHP (works fine for other types of inputs, e.g., text)
<?php
if(isset($_POST['email'])) {
$attendees = $_POST['attendees']; // required
echo "Attendees: ".$attendees;
}
?>
Try adding a hidden input then add the value of your disabled button to it with JS.
JS
function increaseBtnOnclick(item) {
var newValue = Number(document.getElementById(item).value) + 1;
document.getElementById(item).value = newValue;
document.getElementById("hidden_input").value = newValue;
}
function decreaseBtnOnclick(item) {
if(document.getElementById(item).value > 0){
var newValue = Number(document.getElementById(item).value) - 1;
document.getElementById(item).value = newValue;
document.getElementById("hidden_input").value = newValue;
}
}
HTML:
<form method="POST" action="send_email.php">
<div class="row"">
<div class="col-sm-4 col-xs-4">
<h5><b>Attendees</b></h5>
</div>
<div class="col-sm-8 col-xs-8">
<div class="btn-group">
<input type="button" class="btn btn-default" name="decrease" value="-" onclick="decreaseBtnOnclick('attendees')"/>
<input type="button" class="btn btn-primary" name="attendees" value="0" id="chairs" disabled/>
<input type="button" class="btn btn-default" name="increase" value="+" onclick="increaseBtnOnclick('attendees')"/>
<input type="hidden" name="hiddenvalue" id="hidden_input" value="0">
</div>
</div>
</div>
<div class="text-right">
<input type="hidden" name="email" value="contact">
<a href="./index.php">
<button type="submit" class="btn-main">Finish!</button>
</a>
</div>
</form>
PHP:
<?php
if(isset($_POST['email'])) {
$attendees = $_POST['hiddenvalue']; // required
echo "Attendees: ".$attendees;
}
?>
You could switch to a regular input field with button addons.
Disabled input fields do not get posted, however you can set it to readonly.
Please have a look at this thread,
Disabled form inputs do not appear in the request
You could use a hidden input field:
<input type="hidden" id="h_attendees" name="h_attendees" value="0"/>
In your increaseBtnOnclick and decreaseBtnOnclick you could set the value of the hidden field.
function increaseBtnOnclick(item) {
document.getElementById(item).value = Number(document.getElementById(item).value) + 1;
document.getElementById("h_attendees").value = document.getElementById(item).value;
}
function decreaseBtnOnclick(item) {
if(document.getElementById(item).value > 0){
document.getElementById(item).value = Number(document.getElementById(item).value) - 1;
document.getElementById("h_attendees").value = document.getElementById(item).value;
}
}
And access it in PHP
$_POST['h_attendees'];
Related
I have a large php string variable. I want to put php if/else condition inside it, so the edit and delete buttons to be displayed only to the author of the comment and not to everyone, but my code is not working. I used a colon to define the if/else condition, but it doesn’t work. Regular if/else condition also doesn’t work.
Here is my php code:
$user_id = $_SESSION['user_id'];
$if = "if ($user_id == $row['user_id']): ";
$else = "else: ";
$endif = "endif; ";
foreach($result as $row)
{
$output .= '
<div class="panel panel-default">
<div class="panel-body">'.$row["comment"].'</div>
<div class="panel-footer">
'. $if .'
<form class="input-group form-row" action="comment_delete1.php" method="post" enctype = "multipart/form-data">
<div class="input-group-prepend">
<input type="hidden" name="comment_id" id="comment_id" value="'.$row["comment_id"].'" />
<input type="hidden" name="user_id" id="user_id" value="'.$row["user_id"].'" />
<input type="submit" name="submit" id="submit" class="submit btn btn-default" value="Delete" />
</div>
</form>
<button type="button" class="btn btn-default edit" id="'.$row["comment_id"].'" >Edit</button>
'. $else .'
<button type="button" class="btn btn-default reply" id="'.$row["comment_id"].'" >Reply</button>
'. $endif .'
</div>
</div>
';
}
echo $output;
In order to do this correctly the condition must be evaluated:
foreach($result as $row)
{
$output .= '
<div class="panel panel-default">
<div class="panel-body">'.$row["comment"].'</div>
<div class="panel-footer">';
if ($user_id == $row['user_id']) {
$output .= '<form class="input-group form-row" action="comment_delete1.php" method="post" enctype = "multipart/form-data">
<div class="input-group-prepend">
<input type="hidden" name="comment_id" id="comment_id" value="'.$row["comment_id"].'" />
<input type="hidden" name="user_id" id="user_id" value="'.$row["user_id"].'" />
<input type="submit" name="submit" id="submit" class="submit btn btn-default" value="Delete" />
</div>
</form>
<button type="button" class="btn btn-default edit" id="'.$row["comment_id"].'" >Edit</button>';
} else {
$output .= '<button type="button" class="btn btn-default reply" id="'.$row["comment_id"].'" >Reply</button>
</div>
</div>
';
}
}
echo $output;
This is really not a good way to do this and you should consider refactoring your code to make this much neater and easier to read.
Here is the solution:
If we put conditions inside php string or heredoc, php will treat them as string and will display them on the screen as string. So we cannot have conditions inside string.
I put the if else conditions outside the php string variable, and I created two php string variables, one with delete and edit buttons, and the other one without delete and edit buttons. So if the user_id is equal to session user id, the php string variable with delete and edit buttons will be displayed. And, else if the user_id is not equal to session user id, the php string variable without delete and edit buttons will be displayed. The php string variable will be echoed at the end of all conditions.
Here is the php code:
$user_id = $_SESSION['user_id'];
foreach($result as $row) {
if ($row['user_id'] === $user_id) {
$output .= '
<div class="panel panel-default">
<div class="panel-body">'.$row["comment"].'</div>
<div class="panel-footer">
<form class="input-group form-row" action="comment_delete1.php" method="post" enctype = "multipart/form-data">
<div class="input-group-prepend">
<input type="hidden" name="comment_id" id="comment_id" value="'.$row["comment_id"].'" />
<input type="hidden" name="user_id" id="user_id" value="'.$row["user_id"].'" />
<input type="submit" name="submit" id="submit" class="submit btn btn-default" value="Delete" />
</div>
</form>
<button type="button" class="btn btn-default edit" id="'.$row["comment_id"].'" >Edit</button>
<button type="button" class="btn btn-default reply" id="'.$row["comment_id"].'" >Reply</button>
</div>
</div>
';
} else if ($row['user_id'] !== $user_id) {
$output .= '
<div class="panel panel-default">
<div class="panel-body">'.$row["comment"].'</div>
<div class="panel-footer">
<button type="button" class="btn btn-default reply" id="'.$row["comment_id"].'" >Reply</button>
</div>
</div>
';
}
}
echo $output;
I want to ask some question about looping a card.. (https://ibb.co/WpxghNg) when I loop the card, the card can loop while the elements in it can't. all elements don't work, can't switch on / off.
when I loop the card, the card can loop while the elements in it can't. all elements don't work, can't switch on / off.
this is sql farm_node (https://ibb.co/S304TyQ)
elseif($_POST['p']=="show_node"){
$sn=$_POST['sn'];
$q=mysqli_query($link, "SELECT node from farm_node WHERE SN=\"$sn\"");
while($d = mysqli_fetch_row($q)){
$node[]=$d[0];
}
$daftar=array('node'=>$node);
echo json_encode($daftar);
}
$("#newval").change(function() {
sn = $("option:selected").attr('sn');
namafarm = $("option:selected").val();
isi = $("#show_siram2");
if (namafarm == "1") {
console.log("value 1")
console.log(sn)
$("#bungkus_siram").empty();
$("#show_hidro").show();
} else {
console.log("value 2")
console.log(sn)
$("#show_hidro").hide();
$("#bungkus_siram").empty();
$.ajax({
method: "POST",
url: "../inc/olah.php",
data: {
p: "show_node",
'sn': sn,
},
dataType: "json"
})
.done(function(d) {
var html = '<div class="card mx-auto" style="width:350px" >';
html += '<div class="card-body">';
html += '<form id="form_node" >';
html += '<div class="custom-control custom-switch" style="margin-left:60px;">';
html += '<input type="checkbox" class="custom-control-input" id="pompa">';
html += '<label class="custom-control-label" for="pompa">Pompa</label>';
html += '</div>';
html += '</form>';
html += '<h5 class="card-title"></h5>';
html += '<button type="submit" name="nama"class="data-farm btn btn-success btn-sm" id="farm">Set Node</button>';
html += '</div>';
html += '</div>';
jml = d.node.length;
var isi = $("#form");
var node = d.node;
for (var i = 0; i < jml; i++)
//$("#form").show();
{
isi = $("#show_siram2");
$("#field").append(html);
$('#field #field .custom-control .custom-switch:eq(' + i + ')').val(node[i]);
$('#field .card-title:eq(' + i + ')').append(node[i]);
$("#field .data-farm:eq(" + i + ")").val(node[i]);
console.log(node[i]);
}
})
}
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="form-group ">
<label for="serial-number">Serial Number:</label>
<select class="serial-number form-control" name="serial-number" class="custom-select mb-3" id="newval">
<option>Serial Number</option>
<?php
$q=mysqli_query($link,"SELECT produk.SN, produk.nama, produk.produk FROM produk LEFT JOIN farm ON produk.SN=farm.SN WHERE username=\"$_SESSION[username]\"");
while($d=mysqli_fetch_row($q)) {
echo "<option value=$d[2] sn=$d[0]>$d[1]</option>";
}
?>
</select>
</div>
<div class="form-group" id="field">
<div class="row" id="bungkus_siram" style="display:block">
<div class="card" id="show_siram1">
<div class="card-body">
<h5 class="card-title">Node X</h5>
<form id="form_node">
<div class="custom-control custom-switch" style="margin-left:80px;">
<input type="checkbox" class="custom-control-input" id="pompa">
<label class="custom-control-label" for="pompa">Pompa</label>
</div>
<label for="k_max">Kelembapan Maksimum</label>
<input type="range" class="custom-range" id="k_max" name="k_max">
<label for="k_min">Kelembapan Minimum</label>
<input type="range" class="custom-range" id="k_min" name="k_min">
<button type="submit" name="nama" class="data-farm btn btn-success btn-sm" id="farm">Set Node</button>
</div>
<div class="form-group">
<button type="submit" id="save_addPlant" class="btn btn-primary btn-success">Save</button>
<button type="button" class="btn btn-outline-danger pull-left" data-dismiss="modal">Close</button>
</div>
</form>
</div>
<div class="card" id="show_siram2">
<div class="card-body">
<h5 class="card-title">Node X</h5>
<form id="form_node">
<div class="custom-control custom-switch" style="margin-left:60px;">
<input type="checkbox" class="custom-control-input" id="pompa">
<label class="custom-control-label" for="pompa">Pompa</label>
</div>
<label for="k_max">Kelembapan Maksimum</label>
<input type="range" class="custom-range" id="k_max" name="k_max">
<label for="k_min">Kelembapan Minimum</label>
<input type="range" class="custom-range" id="k_min" name="k_min">
<button type="submit" name="nama" class="data-farm btn btn-success btn-sm" id="farm">Set Node</button>
</div>
<div class="form-group">
<button type="submit" id="save_addPlant" class="btn btn-primary btn-success">Save</button>
<button type="button" class="btn btn-outline-danger pull-left" data-dismiss="modal">Close</button>
</div>
</form>
</div>
</div>
</div>
</div>
I want the looping card and its contents as much as possible.
What do you mean by --- the card can loop while the elements in it can't. all elements don't work, can't switch on / off.
Do you mean nothings showing up anything on the browser?
because I noticed that, on your parent div. it has style like this
<div class="row" id="bungkus_siram" style="display:none">
maybe you change the style to- display:block
to show?
Im guessing.
I have the following code displaying input fields in a form. I want to have the submit button active only once all fields are filled. I cant seem to figure out where I've gone wrong. I have omitted some text inputs here for space.
Form:
<?php
if(#$_GET['q']==4 && !(#$_GET['step']) ) {
echo '
<div class="row">
<span class="title1" style="margin-left:40%;font-size:30px;"><b>Enter Quiz Details</b></span><br /><br />
<div class="col-md-3"></div><div class="col-md-6"> <form class="form-horizontal title1" name="form" action="update.php?q=addquiz" method="POST">
<fieldset>
<!-- Text input-->
<div class="form-group">
<div class="col-md-12">
<label for="name">Enter Title</label>
<input id="name" name="name" class="form-control input-md" type="text">
</div>
</div>
<div class="form-group">
<label class="col-md-12 control-label" for=""></label>
<div class="col-md-12">
<input type="submit" style="margin-left:45%" class="btn btn-primary" value="Submit" class="btn btn-primary" id="submit" disabled="disabled"/>
</div>
</div>
</fieldset>
</form>
</div>';
}
?>
<script>
(function() {
$('form input').keyup(function() {
var empty = false;
$('form > input').each(function() {
if ($(this).val() == '') {
empty = true;
}
});
if (empty) {
$('#submit').attr('disabled', 'disabled'); //Leave as disabled if any of the fields are empty
} else {
$('#submit').removeAttr('disabled');//Remove the disabled attribute once all fields are filled
}
});
});
</script>
The code is exactly as it appears here. If I've missed something, kindly point me in a direction. Thank you.
You are using the attribute as disabled="disabled" buts its an attribute having no value you should use it like this <input type="submit" style="margin-left:45%" class="btn btn-primary" value="Submit" class="btn btn-primary" id="submit" disabled/>
Here I have created a working JSFiddle for you check it and do correction https://jsfiddle.net/g1cra5f8/
I'd appreciate your help with this!
Here is my bootstrap HTML code:
<div class="form-group centered">
<input type="submit" class="btn btn-primary btn-block" name="login" value="Log In"></input>
</div>
<div class="centered">
<div class="form-group btn-group" data-toggle="buttons">
<label class="btn btn-default active">
<input type="radio" id="app" value="app" name="dashboard"/>Application
</label>
<label class="btn btn-default">
<input type="radio" id="setup" value="setup" name="dashboard"/>Setup
</label>
</div>
</div>
I would like to add some logic that after the "Log In" button is selected, the "active" class on the label is retained properly.
Example:
Setup is selected
The user clicks login
The password is wrong so the user is thrown back to the same log in form
This time the form has the setup button "active" automatically
I tried playing around with the following within the app input type:
<?php if($_POST['dashboard'] == "app") { echo checked="\"checked\""; } ?>
I now realize, however, that this button group does not work like a regular radio button, and I probably need to toggle the class on the labels somehow.
Thanks in advance for your help!
I got it working piggybacking on the advice of user1844933 :)
<div class="form-group centered">
<input type="submit" class="btn btn-primary btn-block" name="login" value="Log In"></input>
</div>
<div class="centered">
<div class="form-group btn-group" data-toggle="buttons">
<label class="btn btn-default <?php setAppDefault( $lastDashboardSetting ); ?>">
<input type="radio" value="app" name="dashboard"/>Application
</label>
<label class="btn btn-default <?php setSetupDefault( $lastDashboardSetting ); ?>">
<input type="radio" value="setup" name="dashboard"/>Setup
</label>
</div>
</div>
My PHP code:
$lastDashboardSetting = $_POST['dashboard'];
function setAppDefault( $setting ) {
if( $setting === "app" )
echo "active";
else
echo "";
}
function setSetupDefault( $setting ) {
if( $setting === "setup" )
echo "active";
else
echo "";
}
try this
php
if($_POST['dashboard'] == "app" { $appchk='checked'; } else {$appchk='';}
if($_POST['dashboard'] == "setup" { $setupchk='checked'; } else {$setupchk='';}
HTML
<input type="radio" id="app" value="app" name="dashboard" <?php echo($appchk);?>/>Application
<input type="radio" id="setup" value="setup" name="dashboard" <?php echo($setupchk);?> />Setup
I have a file, let`s name it dashboard.php, in this file i load a modal window and trough this window i want to submit a form and the form action is also written in this dashboard.php file. is this possible? i am acutally trying it this way, but it s not working:
dashboard:php
<?php $modal_gerade_geschlossen = $_POST['abgespeichert'];
if ($modal_gerade_geschlossen == 1){
$neue_addy = $_POST['neue_addy'];
$benach_ja_nein = $_POST['benach_ja_nein'];
mysql_query("INSERT INTO mitglieder (email,benachrichtigung)VALUES('$neue_addy','$benach_ja_nein')");
}
?>
<div class="modal-body" style="height:194px">
<div>
<form name="losgehts" method="post" action="../dashboard.php">
<div style="float:left">
<input name="neue_addy" style="width:270px" type="text"/>
</div>
</div>
<div>
<select name="benach_ja_nein" >
<option>ja</option>
<option>nein</option>
</select>
</div>
<input type="hidden" name="abgespeichert" value="1"/>
<button type="button" class="btn btn-default" data-dismiss="modal">Schließen</button>
<input type="submit" name="submit" class="btn btn-primary" value="Speichern"/>
</form>
</div>
</div>