I am entry level developer in PHP and have no knowledge in PHP, i am trying to call database values on selecting a drop down menu, have done this so far with the help of tutorial,.. the problem is when i select 1st drop down the second drop down is getting values from DB, but when i select option from 2nd drop down menu, i am not getting any data displayed on page. Nothing Happens after selecting an option from 2nd drop down.
<?php
include '../connect.php';
$unm=htmlentities($_SESSION['username']);
?>
<html>
<head>
<title> User Panel</title></head>
<body background="../image/pencilback.jpg">
<div id="welcome"><?php
echo '<align="right">'.'<h3>'."Welcome ".$unm.'</h3>';
?></div>
<link rel="stylesheet" type="text/css" href="../css/heading.css">
<table border="0" width="50%" height="150px" align="center" >
<tr width="100%" height="200px" > <td colspan="3"> <img src="../image/banner.jpg"> </td> </tr>
</table>
<table id="tableborder" border="0" width="83%" height="40px" align="center" bgcolor="#BDBDBD" >
<td align="center"><b> Take a Test</b> </td>
<td align="center"><a href="myaccount.php"><b> My Account Details</b> </td>
<td align="center"><a href="testhistory.php"><b> View Test History </b> </td>
<td align="center"><a href="feedback.php"><b> Give Feedback</b> </td>
<td align="center"><a href="../logout.php"><b> Logout</b> </td> </td> </tr>
</table>
<form method="POST" action="">
<div id="viewset"><center>
Select Desired Test </center> <br></div><br><br><br><br><br><br>
<table border="1px" align="center" cellspacing="10px" cellpadding="10px">
<tr> <td> <select name="tstcat" id="countrydd" onChange="change_country()">
<option> Select Test Category</option>
<?php
$res=mysqli_query($connection,"select * from sub");
while($row=mysqli_fetch_array($res))
{
?>
<option value="<?php echo $row["subname"]; ?>"> <?php echo $row["subname"]; ?> </option>
<?php
}
?>
</select>
</td>
<td>
<div id="state" align="center">
<select id="details" onChange="display_details()">
<option>Select Test</option>
</select>
</div>
</tr> </td></table>
<br>
<div id="display">
<table border="1px" height="100px" width="30%" align="center">
<tr style=color:maroon> <th> Test Name </th> <th> No. of Questions </th> <th> Duration </th> </tr>
<tr> <td> </td> <td> </td> <td> </td> </tr>
</table>
</div>
<center> <input type="submit" name="submit" value="Go"></center>
<?php
if(isset($_POST['submit']))
{
$tstct=$_POST['tstcat'];
$tnm=$_POST['testid'];
$_SESSION['testname']=$tnm;
$_SESSION['testcatg']=$tsc;
header('location:teststart.php');
}
?>
</div>
</form>
<script type="text/javascript">
//========== FOR DROPDOWN SELECTION
function change_country()
{
var xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","ajax.php?category="+document.getElementById("countrydd").value,false);
xmlhttp.send(null);
document.getElementById("state").innerHTML=xmlhttp.responseText;
}
//========== TO LOAD TEST DETAILS
function display_details()
{
var xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","ajax2.php?testname="+document.getElementById("details").value,false);
xmlhttp.send(null);
document.getElementById("display").innerHTML=xmlhttp.responseText;
}
</script>
</body>
</html>
AJAX.PHP
<?php
include '../connect.php';
$category=$_GET["category"];
if($category!="")
{
$res=mysqli_query($connection,"select * from test_detail where test_category='$category'");
echo "<select name='testid' onchange='change_test()'>";
while($row=mysqli_fetch_array($res))
{
?>
<option value="<?php echo $row["test_name"];?>"> <?php echo $row["test_name"];?> </option>
<?php
}
echo "</select>";
}
?>
AJAX 2.PHP
<?php
include '../connect.php';
$tstname=$_GET["testname"];
if($tstname!="")
{
$res1=mysqli_query($connection,"select * from test_detail where test_name='$tstname'");
echo "<table>";
while($row1=mysqli_fetch_array($res1))
{
?>
<tr> <td> <?php echo $row1["test_name"];?> </td>
<td> <?php echo $row1["total_ques"];?> </td>
<td> <?php echo $row1["test_dur"];?> </td> </tr>
<?php
}
echo "</table>";
}
?>
I would recommend you to check three things.
in the select that is inside to the div with id #state, you have an onChange calling the javascript function 'display_details()' which you use to load the table element with detail data. But when you return the select from AJAX.PHP the onchange event has another function 'change_test()', it should be 'display_details()'
When you insert dynamically new html elements in a document, normally you have to use dynamic event binding to attach the event handlers. For this I recommend you to use jquery, this link explain how Event binding on dynamically created elements
to make to ajax request jquery is very good library, but if you want to use the XMLHttpRequest object , It will be a good idea check the status of the response:
if (xmlhttp.status == 200){
document.getElementById("state").innerHTML=xmlhttp.responseText;
}else{
// Handle the error;
}
Related
Sorry for spaghetti code :( I can't figure out why is it not working. Button submits only last form, but i want to submit every generated form. What can i do? I tried AJAX but it gives the same result. I wanted to recursively add selections but i think it's the hardest way and there is another solution.
<table id="datatable" class="table table-striped table-bordered">
<thead>
<tr>
<th>Product Name</th>
<th>Product Weight</th>
<th>Product Image</th>
<th>Status</th>
<th>Choose suplier</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php
foreach ($result as $k=>$v)
{
$oid[] = $v['op_id'];
$pui[] = $v['pui'];
?>
<tr>
<td><?php echo substr($v['name'], 0,10); ?></td>
<td><?php echo substr($v['gram'], 0,10); ?></td>
<td><img style="width: 50%" src="<?php echo $site_url.'images/'.$v['image']; ?>" alt=""></td>
<td><?php echo substr($v['opia'], 0,10); ?></td>
<td>
<form method="POST" enctype="multipart/form-data" id="demo-form2" data-parsley-validate oidd="<?php echo $v['pui']; ?>" class="form-horizontal form-label-left sels<?php echo $v['op_id'];?>">
<div class="form-group">
<label class="">
</label>
<div class="" style="width: 100%">
<?php
$sups = $db->prepare('SELECT * FROM product p, suplier s, prod_suplier ps WHERE p.u_id=ps.p_id AND ps.suplier_id=s.id AND p.u_id=:u_id GROUP BY s.id');
$sups->execute(array('u_id'=>$v['pui']));
$count_sups = $sups->rowCount();
if ($count_sups==1) {
$get_sups = $sups->fetch(PDO::FETCH_ASSOC);
echo $get_sups['full_name'];
} else {
?>
<select style="" name="supliers" id="">
<?php
while ($get_sup=$sups->fetch(PDO::FETCH_ASSOC)) {
?>
<option value="<?php echo $v['sfn']; ?>"><?php echo $get_sup['full_name']; ?></option>
<?php }?>
</select>
<?php } ?>
</div>
</div>
</form>
</td>
<td>
<form style=" float: right;" method="POST" action="<?php echo $site_url.$state.'delete/' ?>">
<input type="hidden" name="delid" value="<?php echo $v['oid'] ?>">
<button type="button" onclick="dsomo(this);" name="dbtn" style="color: red; background-color: rgba(0,0,0,0); border:none;" href="">
<i class="fa fa-trash fa-2x" ></i>
</button>
</form>
</td>
</tr>
<?php }?>
</tbody>
</table>
</div>
</div>
</div>
<button type="submit" id="acception" name="submit">submit</button>
<script async>
$('#acception').on('click', function () {
<?php
for ($i=0;$i<count($oid);$i++) {
?>
$("tr>td>.sels<?php echo $oid[$i];?>").submit();
<?php } ?>
})
</script>
Thanks to MojoAllmighty I found the solution. As described in the link below if I want to submit multiple forms by one button I have to assign equal classes to form and use ajax for async submitting forms. So maybe it can be marked as a duplicate of a link below.
Jquery - Submit all forms by one button
I'm getting a warning saying requested unknown parameter, and after I press ok the search is not working correctly.
any solution for this
DataTable warning .......... Requested unknown parameter
Please find how i created the table
<table class="dataTable border bordered striped" data-role="datatable" data-auto-width="false" id="brandTable">
<thead>
<tr>
<td style="width: 20px">
</td>
<td class="sortable-column sort-asc">ID</td>
<td class="sortable-column">Brand Name</td>
<td class="sortable-column">Account Manager</td>
<td class="sortable-column">Date Updated</td>
</tr>
</thead>
<tbody>
<?php foreach ($fw->customer($_SESSION['USERID'])->getAllBrands() as $v) { ?>
<tr>
<td>
<label class="input-control checkbox small-check no-margin">
<input type="checkbox">
<span class="check"></span>
</label>
</td>
<td>
<?php echo $v['id']; ?>
</td>
<td>
<a href="updateBrands.html?id=<?php echo $v['id']; ?>">
<?php echo $v['brandName']; ?>
</a>
</td>
<td>
<?php echo $fw->customer($_SESSION['USERID'])->getAccountManagerbyId($v['accountManager_id']); ?></td>
<td>
<?php echo $v['dateCreated']; ?>
</td>
</tr>
<?php } ?>
</tbody>
</table>
You have an undefined or null value at 3 column of 35 row. You should pass some value at that place .
I need to know how to pass the date while loop through JavaScript to a popup page and put it inside a <P> tag on the popup window?
<div class="header-img" align="center">
<img src="header.png" width="300">
</div>
<div class="maincontent" align="right">
<?php
include 'config.php';
$con->set_charset('utf8');
$sql = "SELECT * FROM de40v_subventions order by id desc";
$names = mysqli_query($con,$sql);
while($row = mysqli_fetch_array ($names)){
?>
<table class="maintable" align="right" width="400px" dir="rtl">
<form name="form" method="post" action="delconfirm.php" onSubmit="return checknone();">
<tr>
<td> <label> <b>الرقم: </b> </label> </td>
<td> <input type="text" value="<?php echo $row['id'] ?>" class="inputid" readonly/> </td>
</tr>
<tr>
<td> <label> <b>التاريخ والوقت: </b> </label> </td>
<td> <p> <?php echo date("Y-m-d H:m:s" , $row['datetime']); ?> </p> </td>
</tr>
<tr>
<td> <label> <b> الإسم كاملاً:</b> </label> </td>
<td> <p id="name"> <?php echo $row['firstname'] . ' ' . $row['fathername'] . ' ' . $row['grandfathername'] . ' ' . $row['familyname'] ?> </p> </td>
</tr>
<tr>
<td> <label> <b> رقم الهوية:</b> </label> </td>
<td> <p id="identity"> <?php echo $row['identity'] ?> </p> </td>
.........
<script type="text/JavaScript">
function openprint(){
var popupwin = window.open("","طباعة البيانات","width=700, height=400");
popupwin.document.writeln('<html><head><title>طباعة البيانات</title></head> <body> <center> <p id="printname"></p> </center> </body> </html>');
popupwin.document.close();
popupname = popupwin.document.getElementById('printname').innerHTML;
name = document.getElementById('name').innerHTML;
popupname = name;
}
<?php } ?>
Actually something is showing up on your popup page, you just haven't passed any actual visible content there. Try this:
popupwin.document.getElementById('printname').innerHTML = 'put your date or content here';
And place it just after your writeln() call.
I have a check-box for each user and the value of the check-box is the user id
What is the best suitable JavaScript Which disables the delete a.button until have clicked the correct corresponding check-box
If button is disabled I would like to be able to use bootstraps disabled="disabled"
<div class="table-responsive" id="user">
<table class="table table-bordered table-hover">
<thead>
<tr>
<td></td>
<td><b><a><?php echo $column_username;?></a></b></td>
<td><b><a><?php echo $column_status;?></a></b></td>
<td><b><a><?php echo $column_date_added;?></a></b></td>
<td class="text-right"><b><?php echo $column_action;?></b></td>
</tr>
</thead>
<tbody>
<?php if ($users) { ?>
<?php foreach ($users as $user) { ?>
<tr>
<td class="text-center">
<input type="checkbox" name="selected" value="<?php echo $user['user_id']; ?>" />
</td>
<td class="text-left"><?php echo $user['username']; ?></td>
<td class="text-left"><?php echo $user['status']; ?></td>
<td class="text-left"><?php echo $user['date_added']; ?></td>
<td class="text-right">
<i class="fa fa-pencil"></i> Edit User
<i class="fa fa-pencil"></i> Delete User
</td>
</tr>
<?php } ?>
<?php } ?>
</tbody>
</table>
First, all the delete buttons should be disabled when you first load the HTML. You can add the disabled='disabled' attribute to each delete button in your PHP code.
Then in JavaScript you look for a change event on the checkbox:
$("input[type='checkbox']").change(function() {
var thisRow = $(this).parents("tr");
if(this.checked) {
thisRow.find("a.btn-danger").attr("disabled", false);
} else {
thisRow.find("a.btn-danger").attr("disabled", "disabled");
}
});
I am trying to use innerHTML to place a dynamic image within a row of a table I have on my page. The image is showing up, however, it is floating to the top of the table for some reason. Is there any way to ensure it stays in the row that I have it in. Here are some snippits of the code:
In the javascript:
document.getElementById("myResults").innerHTML = "";
In the html:
<td class="label"><label for="description">Description:</td>
</tr>
<tr>
<td class="label">
<div class ="<?php echo form_row_class("bio") ?>" >
<div class="styled_textarea">
<textarea type="text" id="bio" onfocus="this.value=''; setbg('#f0f7f8');" onblur="setbg('white')" name="bio" value="<?php echo h($_POST['bio']); ?>" rows="10"></textarea>
</div>
</div>
</td>
</tr>
<tr>
<td class="label"><label for="image"><div class="vspace2em"></div>Image:</label></td>
</tr>
<tr>
<td class="label">
<a id="edit_image"></a>
<div class="row">
<div class="span10 offset1">
<div id="editpane">
<button class="btn btn-large btn-primary openbutton" type="button">Open an Image</button>
</div>
</div>
</div>
</td>
<td>
<?php if($current_user2): ?>
<?php if($current_user2['filepicker'] != ''): ?>
<img src="<?php echo $current_user2['filepicker'];?>">
<?php endif; ?>
<?php endif; ?>
</td>
</tr>
<tr>
<div id="myResults"></div>
</tr>
etc...
The image called with the innerHTML using floats to the top of the table for some reason, without appearing where I have it situated. Any ideas why this is happening?
It would be more clearer to read if you formatted the HTML table right. From the first glance I see that you have
<tr>
<div id="myResults"></div>
</tr>
myResults is your relevant div. The problem is you dont have any <td> elements inside the <tr>. I think your code should look like this.
<tr>
<td>
<div id="myResults"></div>
</td>
</tr>
This should give you the desired result.