show div in selecting option inside looping data - php

i have code in php:
<?php
$produk = mysql_query("SELECT * FROM produk ORDER BY id_produk DESC");
while($p=mysql_fetch_array($produk)){
<select name='urutan' onChange='yesnoCheck(this);'>
<option value='yes' selected>Yes</option>
<option value='no'>No</option>
</select>
<div id='ifY' style='display: none;'>
<input type=submit value=submit class=ui-btn-primary>
</div>
<div id='ifN' style='display: none;'>
<input type=submit value=submit class=ui-btn-primary>
</div>
}
in my jquery code:
<script>
function yesnoCheck(that) {
if (that.value == "other") {
document.getElementById("ifNon").style.display = "block";
} else {
document.getElementById("ifNon").style.display = "none";
}
if (that.value == "yes") {
document.getElementById("ifY").style.display = "block";
} else {
document.getElementById("ifY").style.display = "none";
}
if (that.value == "no") {
document.getElementById("ifN").style.display = "block";
} else {
document.getElementById("ifN").style.display = "none";
}
}
</script>
in my looping data,i try run selecting option to show div, but it just run in my first data,so cant run in another data. help me please

Try this, this should work
<?php
$produk = mysql_query("SELECT * FROM produk ORDER BY id_produk DESC");
$i = 0;
while($p=mysql_fetch_array($produk)){ ?>
<select name='urutan' onChange='yesnoCheck(this,<?= $i ?>);' rel="<?= $i ?>">
<option value='yes' selected>Yes</option>
<option value='no'>No</option>
</select>
<div id="ifY<?= $i ?>" style='display: none;'>
<input type=submit value=submit class=ui-btn-primary>
</div>
<div id="ifN<?= $i ?>" style='display: none;'>
<input type=submit value=submit class=ui-btn-primary>
</div>
<br>
<?php $i++; } ?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
function yesnoCheck(that,i) {
if (that.value == "yes") {
$("#ifY" + i).css("display","block")
$("#ifN" + i).css("display","none")
} else if (that.value == "no"){
$("#ifY" + i).css("display","none")
$("#ifN" + i).css("display","block")
}
}

Use Dynamic ID and name for the div and select element with using counter variable.
PHP Code
<?php
$produk = mysql_query("SELECT * FROM manufacturer ORDER BY id DESC limit 5");
$i = 1;
while($p=mysql_fetch_array($produk))
{
?>
<select name='urutan<?php echo $i?>' id="urutan<?php echo $i?>" onChange='yesnoCheck("<?php echo $i?>");'>
<option value='yes' selected>Yes</option>
<option value='no'>No</option>
</select>
<div id='ifY<?php echo $i?>' style='display: none;'>
<input type=submit value=submit class=ui-btn-primary>
</div>
<div id='ifN<?php echo $i?>' style='display: none;'>
<input type=submit value=submit class=ui-btn-primary>
</div>
<?php
$i++;
}
?>
Javascript Code
<script>
function yesnoCheck(id)
{
var that = document.getElementById("urutan"+id);
/*if (that.value == "other") {
document.getElementById("ifNon"+id).style.display = "block";
} else {
document.getElementById("ifNon"+id).style.display = "none";
}*/
if (that.value == "yes") {
document.getElementById("ifY"+id).style.display = "block";
} else {
document.getElementById("ifY"+id).style.display = "none";
}
if (that.value == "no") {
document.getElementById("ifN"+id).style.display = "block";
} else {
document.getElementById("ifN"+id).style.display = "none";
}
}
</script>

Something like this would help i guess. Also id should always be unique use class instead.
PHP code and html
<?php
$produk = mysql_query("SELECT * FROM produk ORDER BY id_produk DESC");
while($p=mysql_fetch_array($produk)){ ?>
<div> // new div added here.
<select name='urutan' onChange='yesnoCheck(this);'>
<option value='yes' selected data-selector="ifY">Yes</option> // added new data-selector attribute.
<option value='no' data-selector="ifN">No</option>
</select>
<div class='ifY commonclass' style='display: none;'> // changed id to class and added a commonclass to get all divs.
<input type=submit value=submit class=ui-btn-primary>
</div>
<div class='ifN commonclass' style='display: none;'>
<input type=submit value=submit class=ui-btn-primary>
</div>
</div>
<?php } ?>
Jquery
$('[name="urutan"]').change(function(){
var $this = $(this);
var $parentDiv = $this.parent();
var $selected = $("option:selected",$this),
$parentDiv.find('.commonclass').hide();
$parentDiv.parent().find("."+$selected.data('selector')).show();
}).trigger('change');

Related

Passing 2 value from 2 dropdown into 3rd dropdown

I have tried passing value on 3rd drop down but it doenst receive any of it
it doesnt show error but it doesnt get any data from 2 dropdowns Im new to just and im trying something but it doesnt work at all pls help thank you very much
dropdown.php
<select class="form-control" style="font-size: 21px;" name="Building" id="Building" onChange="change_floor(this.value)">
<option>Select</option>
<?php
$res=mysqli_query($conn,"select * from Building");
while($row=mysqli_fetch_array($res)){
?>
<option value="<?php echo $row['Building']; ?>"><?php echo $row['Building'];?></option>
<?php
}
?>
</select>
<select class="form-control" style="font-size: 21px;" name="floor" id="Floor" onChange="change_floor(this.value)">
<option>Select</option>
<?php
$res=mysqli_query($conn,"select * from floor");
while($row=mysqli_fetch_array($res)){
?>
<option value="<?php echo $row['Floor']; ?>"><?php echo $row['Floor'];?></option>
<?php
}
?>
</select>
<div id="Dept"></div>
</div>
</div>
<script type="text/javascript ">
function change_floor(str,str1) {
if (str.length == 0) {
document.getElementById("Dept").innerHTML = "";
return;
} else {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("Dept").innerHTML = this.responseText;
}
};
xmlhttp.open("GET", "deptajax.php?q="+str+"&d="+str1, true);
xmlhttp.send();
}
}
</script>
deptajax.php
If I use request here i shows error but if I use GET i doesnt show any error what is more usable for this scenario?
<?php
$link=mysqli_connect("localhost","id5592115_retep","Password");
mysqli_select_db($link,"id5592115_admin");
$q = $_REQUEST["q"];
$d = $_REQUEST["d"];
$res=mysqli_query($link,"select * from depttable where Flr='$d' and
Building='$q' order by deptname asc");
echo "<form method='get' action='table2.php'>";
echo '
<select class="form-control" style="font-size: 21px;" name="dept">
';
while ($row=mysqli_fetch_array($res)) {
$deptid = $row['deptid'];
$deptname = $row['deptname'];
echo "<option value='".$deptname."'>".$deptname."</option>";
}
echo "</select>";
echo '<button type="submit" class="btn btn-light
btnmove">Submit</button>';
echo "</form>";
?>
Your onclick function is call every time you select any of the dropdown , so at a time only one value is passed i.e : either str or str1 will have value . Instead to avoid this you can do like below :
When first selectbox is selected try to store it value in some variable like below :
<select class="form-control" style="font-size: 21px;" name="floor" id="Floor"
onChange="change_floor1(this.value)">
<!--^ make another function with name change_floor1-->
<option>Select</option>
...
</select>
Then in your <script></script>do like below :
var s; // declare globally
function change_floor1(str) {
s=str; //assigning value of first-select box in "s"
}
function change_floor(str1) {
if (str1.length == 0) {
document.getElementById("Dept").innerHTML = "";
return;
} else {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("Dept").innerHTML = this.responseText;
}
};
//passing both value
xmlhttp.open("GET", "deptajax.php?q="+s+"&d="+str1, true);
xmlhttp.send();
}
}

How to create checkboxs that fetch from database and pass on value

I am new to php and can not turn a row into a checkbox field that passes onto a separate page.
if ($result = $db->query($sql) ) {
//*start of original
if ($result->num_rows > 0) {
?>
<!-- <form action="Confirmation.php" method="post"> -->
<form action="Payment.php" method="post">
<label for="PatientID">Choose the Patient you wish to register for:</label>
<select name="PatientID" id="">
<option value="" disabled>Please select a patient to enroll:</option>
<!-- <option value="All">All Patients</option> -->
<?php
while($result = fetch_assoc($result)) {
echo "<input type='checkbox' value='{$row['PatientID']}'>" . $row['PatientName'] . '</br>';
}
//
//while ($row=$result->fetch_assoc()) {
?>
//<option value="<?=$row['PatientID'];?>"> <?=$row['PatientName']?></option>
//*/ <?php
//}
echo '</select><input type= "submit" class="myButton" name="submit" value="Register Now" /></form>';
} else {
echo "There are no patients currently available for registration." ;
$rosterListFail = true;
}
} else {
$message = $db->error;
// echo $message;
}
//*/
//}
?>

How to keep form items selected after post request.Using onchange

I have a problem when submit form value from the second select don't show. Can someone explain me what I'm doing wrong because in new in this (maybe is because i use onchange event). Thanks in advance.
index.php
<?php
include("includes/conn.php");
$sql = "SELECT * FROM opstina";
$result = $conn->query($sql);
if($result->num_rows > 0){
$opstineArr = array();
while($row = $result->fetch_assoc()){
$opstineArr[] = $row;
}
}
if(isset($_POST["addNesto"])){
$opstine = $_POST["opstine"];
$ime = $_POST["ime"];
if(isset($opstine)){
if(empty($opstine)){
$error["opstine"] = "izaberite opstinu";
}
}
$naselja = $_POST["naselja"];
if(isset($naselja)){
if(empty($naselja)){
$error["naselja"] = "izaberite naselje";
}
}
if(isset($ime)){
if(empty($ime)){
$error["ime"] = "izaberite ime";
}
}
if(count($error) == 0){
echo 2222;
}
}
include("includes/header.php");
?>
<form action="" method="POST">
<select name="opstine" class="form-control" id="select_id" onchange="getNaselja(this.value)">
<option>..Izaberite Opstinu..</option>
<?php foreach($opstineArr as $key => $opstina): ?>
<option value="<?php echo $opstina['OpstinaID']; ?>" <?php if(isset($opstine) && $opstine==$opstina['OpstinaID']) echo "selected"; ?> ><?php echo $opstina['NazivOpstine']; ?></option>
<?php endforeach; ?>
</select>
<?php if(isset($error["opstine"])) echo $error["opstine"]; ?>
<p>
<select name='naselja' id='naseljeLista' class='form-control' style='margin-top:10px;'>
</select>
<span><?php if(isset($error["naselja"])) echo $error["naselja"]; ?></span>
</p><br>
<input type="text" name="ime" value="<?php if(isset($ime)) echo $ime; ?>" class="form-control col-md-7 col-xs-12"><?php if(isset($error["ime"])) echo $error["ime"]; ?><br><br>
<input type="submit" name="addNesto" class="btn btn-sm btn-success">
</form>
<?php include("includes/footer.php"); ?>
getNaselja.php
global $conn;
include("includes/conn.php");
if(!empty($_POST["opstina_id"])){
$opstinaID = $_POST["opstina_id"];
$sql1 = "SELECT * FROM naselje WHERE OpstinaID=".$opstinaID;
$result1 = $conn->query($sql1);
if($result1->num_rows > 0){
$naseljaArr = array();
while ($one = $result1->fetch_assoc()) {
$naseljaArr[] = $one;
}
}
echo "<option value=''>---Izaberite naselje---</option>";
foreach ($naseljaArr as $key => $value) {
if(isset($_POST["naselja"]) && $value["NaseljeID"]==$_POST["naselja"]){
echo "<option value='".$value['NaseljeID']."' selected='selected' style='display:block;'>".$value['NazivNaselja']."</option>";
}else{
echo "<option value='".$value['NaseljeID']."'>".$value['NazivNaselja']."</option>";
}
}
}
footer.php
<script>
function getNaselja(val){
$.ajax({
type: 'POST',
url: 'getNaselja.php',
data: 'opstina_id='+val,
success: function(data){
$('#naseljeLista').html(data);
console.log(data);
}
});
}
</script>
example of form after submit on index.php
When I choose from first select my data on the console looks like:
<option value=''>---Izaberite naselje---</option>
<option value='4610'>Beška</option>
<option value='4611'>Inđija</option>
<option value='4612'>Jarkovci</option>
<option value='4613'>Krčedin</option>
<option value='4614'>Ljukovo</option>
<option value='4615'>Maradik</option>
<option value='4616'>Novi Karlovci</option>
<option value='4617'>Novi Slankamen</option>
<option value='4618'>Slankamenački Vinogradi</option>
<option value='4619'>Stari Slankamen</option>
<option value='4620'>Čortanovci</option>

Function not renaming field pulled from another page

I have a form which is cloned when needed, inside this form I have a div, this div is replaced by a div pulled from another page which has new select options based on a select option from a above field.
Each 'cloned' forms fields are given a new name with a function, but this function seems to have trouble seeing the field that is pulled in as part of the form and isn't generating a new name for it.
Could Someone kind enough show me the way please?
function;
$(document).ready(function() {
var newNum = 2;
cloneMe = function(el) {
var newElem = el.clone().attr('id', 'container' + newNum);
newElem.html(newElem.html().replace(/form\[1\]/g, 'form['+newNum+']'));
newElem.html(newElem.html().replace(/id="(.*?)"/g, 'id="1'+newNum+'"'));
$('#cloneb').before(newElem);
$('#delete_name'+ newNum).html('<p id="rem_field"><span>Delete Line</span></p>');
newNum++;
};
$('p#rem_field').live('click', function() {
$(this).parents('div').remove();
return false;
});
});
form;
<form action='' method='post' enctype='multipart/form-data' name='form' id='form'>
<div id="container1">
<div class="instance" id="instance">
<label>Style:</label>
<select name='form[1][style]' id='style' class='style' onchange="showDim(this)">
<option value='0' class='red'>Select a style...</option>
<?php
include ('connect.php');
$getsty = $db->prepare("SELECT Style_ID, Style_Type FROM style ORDER BY Style_Type ASC LIMIT 1, 18446744073709551615;");
$getsty->execute();
while($row = $getsty->fetch(PDO::FETCH_ASSOC)) {
$Style_ID = $row['Style_ID'];
$Style_Type = $row['Style_Type'];
echo " <option value='$Style_ID'>$Style_Type</option>";
}
?>
</select>
<br />
<div class='dimdiv'>
<label>Dimensions:</label>
<select name='form[1][Dim]' id='Dim'>
<option value='0' class='red'>Select the dimensions...</option>
</select>
</div>
<br />
<label>Colour:</label>
<select name='form[1][Colour]' id='Colour'>
<option value='0' class='red'>Select a colour...</option>
<option value='Colour1'>Colour #1</option>
<option value='Colour2'>Colour #2</option>
<option value='Colour3'>Colour #3</option>
<option value='Colour4'>Colour #4</option>
</select>
<br />
<label>Quantity:</label>
<input type='text' name='form[1][Quantity]' id='Quantity'>
<br />
</div>
<div id="delete_name" style="margin:15px 0px 0px 0px; width:120px; height:30px;"></div>
</div>
<input type="button" id="cloneb" value="Clone" onclick="cloneMe($('#container1'));" />
<input type='submit' name='submit' value='Submit' class='buttons'>
</form>
Field pulled from get_dim.php;
<label>Dimensions:</label>
<select name='form[1][Dim]' id='Dim'>
<option value='0' class="red">Select the dimensions...</option>
<?php
$id = intval($_GET['id']);
include ('connect.php');
$getcus = $db->prepare("SELECT Dim_ID, Dim FROM dimentions WHERE Style_ID=? ORDER BY Dim ASC ");
$getcus->execute(array($id));
while($row = $getcus->fetch(PDO::FETCH_ASSOC)) {
$Dim_ID = $row['Dim_ID'];
$Dim = $row['Dim'];
echo " <option value='$Dim_ID'>$Dim</option>";
}
?>
</select>
Function to replace the dimdiv with get_dim.php;
function showDim(elem)
{
var elems = document.getElementsByClassName('style'),
groupIndex = -1,
targetDimDiv,
i;
for( i = 0; i < elems.length; ++i ) {
if( elems[i] == elem ) {
groupIndex = i;
break;
}
}
if( groupIndex == -1 )
{
return;
}
targetDimDiv = document.getElementsByClassName('dimdiv')[groupIndex];
if (elem.value == "")
{
targetDimDiv.innerHTML="";
return;
}
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
xmlhttp.onreadystatechange = function( ) {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200 ) {
targetDimDiv.innerHTML = xmlhttp.responseText;
}
};
xmlhttp.open("GET","get_dim.php?id="+elem.value,true);
xmlhttp.send();
}
Your problem is, that form[1][Dim] is hard coded into get_dim.php. When you clone a form, you change the name of every element, but this AJAX request would still return a form element with name form[1][Dim] there too.
You can fix this, by reading out the current form id and passing it to get_dim.php and making the name generation there dynamic.
The parts you have to change (roughly):
replace function:
form_id = groupIndex + 1; // if I get groupIndex right
xmlhttp.open("GET","get_dim.php?id="+elem.value+"&form_id="+form_id,true);
get_dim.php:
<select name='form[<?php echo intval($_GET['form_id']); ?>][Dim]' id='Dim'>

html form does not work inside fieldsets

I have a field set and inside it i have a form . it does not work . i mean to say . when i see the tags using firebug , the form tags will not be there at all..how do u i get over it.
this is how the code goes...its a php code..
<div id="dialog-form_surg_couns" title=" Surgical Counselling">
<?php
$surgCount = 0;
foreach($this->surgery as $surgery) {
$surgCount++;
$newId = str_replace(' ','',$surgery->getSurgeryname());
?>
<div class='fieldreq1Pct'>
<div class='fieldItemLabel'>
<label for=''><?php echo $surgery->getSurgeryname() ?></label>
</div>
<div class='fieldItemValue'>
<input type='checkbox' class='surg_couns_tests' id="<?php echo $newId ?>" name='surg_couns_tests' value="<?php echo $surgery->getSurgeryname() ?>" <?php echo (($showValue && strstr($visitRecord->getSurgcounstests(),$surgery->getSurgeryname())) ? 'checked' : "" ); ?> onClick="javascript:showBlock(this.id);">
</div>
</div>
<?php
if(($surgCount % 3) == 0)
{
?>
<div class='clear'></div>
<?php
}
}
?>
<div class='clear'></div>
<hr/>
<?php
foreach($this->surgery as $surgery) {
$newId = str_replace(' ','',$surgery->getSurgeryname());
$fieldCount = 0;
?>
<div id='<?php echo $newId ?>_block' style='display:none;' class='check_block'>
<form method='POST' action ='' id ='<?php echo $newId ?>_form'>
<table border='0' class='surg_table'>
<?php
foreach($this->surgeryTemplate as $surgerytemplate) {
if($surgery->getSurgeryid() == $surgerytemplate->getSurgeryid())
{
$fieldCount++;
$fieldName = 'field'.$fieldCount;
$fieldId = $surgerytemplate->getFieldid();
if($surgerytemplate->getRequired() == 'Y')
{
$required = 'required';
}
else
{
$required = '';
}
if($surgerytemplate->getType() == 'AN')
{
$validation = 'alpha';
}
else
{
$validation = '';
}
?>
<tr>
<td>
<?php echo $surgerytemplate->getFieldname(); ?>
</td>
<td>
<?php
if($surgerytemplate->getType() == 'B')
{
echo '<input type=\'radio\' name=\''.$fieldName.'\' value=\'Yes\'>Yes';
echo '<input type=\'radio\' name=\''.$fieldName.'\' value=\'No\'>No';
}
else
{
echo '<input type=\'text\' name=\''.$fieldName.'\' id=\''.$fieldName.'\' class=\''.$required.' '.$validation.'\' onblur="checkValid(this.id)"><div id=\''.$fieldName.'error\'></div>';
}
?>
</td>
</tr>
<?php
}
}
?>
</table>
<center><input type='button' name='submit' value='submit' onclick='javascript:submitSurgeryForm("<?php echo $newId ?>")'></center>
</form>
</div>
<?php
}
?>
</div>
You can't have a form tag inside another form. The following HTML is invalid:
<form>
<fieldset>
<form>
<input>
</form>
</fieldset>
</form>
The browser will silently ignore the second form, and instead will interpret your page as:
<form>
<fieldset>
<input>
</fieldset>
</form>

Categories