I want to fetch data from a database related to a selected option when it is clicked.
When I select another option then fetch the data for that selection.
When I fetch the new data option the old data is replaced.
I want to display each set of data in a new row so I do not want the new data to replace the old data.
<form action="" method="post">
<div class="form-group">
<label for="sel1">Select list:</label>
<select multiple name="names" class="form-control" id="sel1">
<?php
$result = "SELECT Name FROM coder";
$sql = $conn->query($result);
if ($sql->num_rows > 0)
while($row = $sql->fetch_assoc()) {?>
<option value='<?php echo $row['Name'] ?>'><?php echo $row['Name'] ?></option>
<?php }
?>
</select>
</div>
<input type="submit" name="submit" class="btn btn-info">
</form>
<table>
<thead>
</thead>
<tbody>
<?php
$result = "SELECT * FROM coder WHERE Name='$name'";
$sql = $conn->query($result);
if ($sql->num_rows > 0)
while($row = $sql->fetch_assoc()) {{?>
<tr>
<td><?php echo $row['Name']?></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
I suggest to use jquery for binding the div once the result is placed inside it.
Also Instead of using direcy Query, you also can use onChange event of jQuery for fetching the data according to your need. It will accelerate the speed of fetching of data.
Divide the div into samall child divs and store the data into them.
Your problem will be resolved in this way for sure
Related
I have 2 dropdown menus. I want to dynamically populate second dropdown based on the first one. When changing the value in first dropdown, it has to pass the selected value to an invisible php array to update it. So here is the problem: passing the js variable(strUser) to php {{ the php var }} (used {{}} only to make it more simple to see).
<form class="formatted-form" action="" method="POST">
<div class="message">
<?php ThemexInterface::renderMessages(isset($_POST['success'])?$_POST['success']:false); ?>
</div>
<table class="profile-fields">
<tbody>
<?php if(!ThemexCore::checkOption('user_location')) { ?>
<tr style="display: none">
<th><?php _e('Hidden Country', 'lovestory'); ?></th>
<td>
<div class="select-field">
<span></span>
<?php
echo ThemexInterface::renderOption(array(
'id' => 'country',
'type' => 'select',
'options' => array_merge(array('-' => '–'), ThemexCore::$components['countries']),
'value' => !empty(ThemexUser::$data['user']['profile']['country'])?{{ThemexUser::$data['user']['profile']['country']}}-this should be replaced with the strUser variable we get in js :null,
'wrap' => false,
));
?>
</div>
</td>
</tr>
<tr>
<th><?php _e('Visible_Country', 'lovestory'); ?></th>
<td>
<span>
<?php
$post_ids2 = $wpdb->get_col( "SELECT DISTINCT country FROM countries" );
echo "<SELECT id=tara_1 onChange='reload()' >";
foreach ($post_ids2 as $value){
echo "<option value=$value </option>";
echo $value;
}echo "</select>";
?>
</span>
</div>
</td>
</tr>
<script>
function reload(){
var e = document.getElementById("tara_1");
var strUser = e.options[e.selectedIndex].text;
}</script>
<tr>
<th><?php _e('City', 'lovestory'); ?></th>
<td>
<span>
<?php
global $wpdb;
$post_ids = $wpdb->get_col( "SELECT city FROM countries WHERE tara='".ThemexUser::$data['user']['profile']['country']."'" );
echo "<SELECT id=tara_1 name=cities>";
foreach ($post_ids as $value){
echo "<option value=$value </option>";
echo $value;
}echo "</select>";
?>
</span>
</div>
</td>
</tr>
<?php } ?>
<?php
ThemexForm::renderData('profile', array(
'edit' => true,
'before_title' => '<tr><th>',
'after_title' => '</th>',
'before_content' => '<td>',
'after_content' => '</td></tr>',
), ThemexUser::$data['user']['profile']);
?>
</tbody>
</table>
<?php _e('Save Changes', 'lovestory'); ?>
<input type="hidden" name="update" value="1" />
<input type="hidden" name="user_action" value="update_profile" />
<input type="hidden" name="nonce" value="<?php echo wp_create_nonce(THEMEX_PREFIX.'nonce'); ?>" />
</form>
I know php is server-side and js works on client-side, that's why i am confused on how could i dynamically refresh the second dropdown AND pass the value selected by user to PHP POST. I am new to PHP and JS so please be kind, i would appreciate any hint on how I should approch the problem. In the end i want it to behave like this:
Dropdown1 selected>Italy, Dropdown2 > {gets populated with italian cities from database}
Dropdown1 selected>France, Dropdown2 > {gets populated with frech cities from database}
Well, the real problem is passing JS to PHP- which i think is the wrong approach.
Thanks in advance.
how do I display data in the textbox automatically when I click on the edit button, I want to display data from the database using jquery chosen like this:
enter image description here
, sorry if my question is unclear, if anyone is unable to ask me, please help
This is my code
<?php
$main_controll = new App_UserManagement_Control_UserManagement();
$data_from_ctr = $main_controll->edit();
echo json_encode( $data_from_ctr);
?>
<tr class="odd">
<td width="150"><strong>Account Access</strong></td>
<td width="10">:</td>
<td colspan="2">
<select id="acctAccessed" name="acctAccessed[]" multiple="multiple" class="chosen-select" style="width:350px;" data-placeholder="Select account">
<?php foreach ($data_from_ctr['rek'] as $data) :?>
<option <?php if ($data['selected'] == "1"){ ?> selected= "" <? } ?> value="<?php echo $data['idRek'];?>"><?php echo $data['idRek'];?></option>
<?php endforeach; ?>
</select>
<input id="chkall" type="checkbox" >Select All</input>
</td>
<script type="text/javascript">
$("#acctAccessed").chosen();
</script>
but my error like this, I don't get data for account access
enter image description here
Can you post $data_from_ctr as , the above mentioned code by you is working.
I am using two drop down lists first for category and second for sub category. I want to use Javascript's onChange(); to fetch the values of sub categories but am not able to make out how to use it in the code. Plz guide.
<th>Category</th>
<td>
<select name="category" class="frm" onChange="fun()">
<?php
include("data_connect.php");
$data=mysqli_query($con,"select * from `category`");
while($row=mysqli_fetch_array($data))
{
echo "<option value=".$row['c_id'].">".$row['name']."</option>";
}
?>
</select>
</td>
</tr>
<tr>
<th>Sub-Category</th>
<td>
<select name="sub_category" class="frm">
<?php
if(isset($_POST['sub']))
{
include("data_connect.php");
$data=mysqli_query($con,"select * from `sub_category` where c_id=".$_POST['category']."");
while($row1=mysqli_fetch_array($data))
{
echo "<option value=".$row1['s_id'].">".$row1['name']."</option>";
}
}
?>
</select>
</td>
</tr>
I'm going to ignore for a moment your invalid table structure as I'm not really sure how you're wanting it to look. I'll just wrap what you have in a form for now so you at least have something to work from.
Also, your sub-category query is open to SQL injection attacks. You should use bound parameters to address this. You can continue to use mysqli to do that, but I personally suggest switching to PDO.
<form method="post" id="categoryForm">
<th>Category</th>
<td>
<select name="category" class="frm" onChange="document.getElementById('categoryForm').submit();">
<?php
include("data_connect.php");
$data=mysqli_query($con,"select * from `category`");
while($row=mysqli_fetch_array($data))
{
// check to see if we should re-select this value because it was submitted
$isSelected = (!empty($_POST['category']) && $_POST['category'] == $row['c_id'] ? " selected" : "");
echo "<option value='".$row['c_id']."'" . $isSelected . ">".$row['name']."</option>";
}
?>
</select>
</td>
</tr>
<tr>
<th>Sub-Category</th>
<td>
<select name="sub_category" class="frm">
<?php
// only if we've submitted the main category selection
if(!empty($_POST['category']))
{
// no need to include data_connect.php again, it was already included above
$data=mysqli_query($con,"select * from `sub_category` where c_id='".$_POST['category']."'");
while($row1=mysqli_fetch_array($data))
{
echo "<option value=".$row1['s_id'].">".$row1['name']."</option>";
}
}
?>
</select>
</td>
</tr>
</form>
I'm new in php how can I make this work
delete.php
<?php
include_once 'dbconfig.php';
if($_POST['del_id'])
{
$id = $_POST['del_id'];
$stmt=$db_con->prepare("UPDATE tbluser set status=1 WHERE id=:id");
$stmt->execute(array(':id'=>$id));
}
?>
I want to add an active/inactive dropdown choice in my edit form page but I dont know how to make it work I dont know how to call the delete.php so that when i choose inactive and submit the form it will not show on my datatable
edit_form.php
<?php
include_once 'dbconfig.php';
if($_GET['edit_id'])
{
$id = $_GET['edit_id'];
$stmt=$db_con->prepare("SELECT * FROM tbluser WHERE id=:id");
$stmt->execute(array(':id'=>$id));
$row=$stmt->fetch(PDO::FETCH_ASSOC);
}
?>
<div id="dis">
</div>
<form method='post' id='emp-UpdateForm' action='#'>
<table class='table table-bordered'>
<input type='hidden' name='id' value='<?php echo $row['id']; ?>' />
<tr>
<td>Status</td>
<td><select name=stats>
<option value="1">Active</option>
<option value="0">Inactive</option>
</select>
</td>
</tr>
<tr>
<td colspan="2">
<button type="submit" class="btn btn-primary" name="btn-update" id="btn-update">
<span class="glyphicon glyphicon-plus"></span> Save Updates
</button>
</td>
</tr>
</table>
everything is working it's just an additional functionality in the edit form page
Get the stats like $status= $_POST['stats']; in delete.php
change you $_GET['edit_id'] to $_GET['id']
modify you updated to set the status
$id = $_POST['id'];
$status = $_POST['stats']
$stmt=$db_con->prepare("UPDATE tbluser set status=:status WHERE id=:id");
$stmt->execute(array(':status'=>$status,':id'=>$id));
If you want to call the delete.php, you can use ajax.
<script>
$.ajax({
url:'delete.php',
type:'post',
data: $('form').serialize()
});
</script>
You can get values on the delete.php, with the help of $_POST or $_REQUEST.
And if the delete.php code is right, then on submit of the edit_page.php your datatable can get affected.
I have a table which contains the column fleet name, vehicle registration and some other info about each vehicle. 1 fleet can have many vehicles. The main aim of my application is to show vehicles within a fleet when a fleet is selected (from a dropdown), so filtering out and showing rows of the database (based on the column "fleet_name") so the results show only the cars in the selected fleet (this is already achieved, see code below). what i'm now trying to achieve is to grab, with a click of a button, ALL of the pulled out phone numbers of the vehicles within the selected fleet, comma seperated, so if the fleet had 3 vehicles, once you click the "grab numbers" button you would get 074123456789,07412347321,074292839201 in the textfield. if the 0 in the beggining could be changed to 44, that would be a huge bonus too! so i've got as far as being able to grab a row's phone number singular:
currently my code to show all the data:
<?php
$sql = "SELECT * FROM main_table";
if (isset($_POST['search'])) {
$search_term = mysql_real_escape_string($_POST['search_box']);
$sql .= " WHERE fleet_name='$search_term'";
$sql .= " OR vehicle_registration_number='$search_term'";
}
$query = mysql_query($sql) or die(mysql_error());
?>
form which filters out the data:
<form name="search_form" method="POST" action="index2.php">
<?php $sql2 = "SELECT DISTINCT fleet_name FROM main_table";
$query2 = mysql_query($sql2);
echo "<p> Select Fleet Name: <select name='search_box'><option></option>";
while($row = mysql_fetch_assoc($query2)){ echo "<option>{$row['fleet_name']}</option>"; }
echo "</select></p>"; ?>
<input type="submit" name="search" value="Search the table...">
</form>
and my table code:
<table width="70%" cellpadding="5" cellspacing="5">
<tr>
<td><strong>Fleet Name</strong></td>
<td><strong>SIM Mobile Number</strong></td>
</tr>
<?php while ($row = mysql_fetch_array($query)) { ?>
<td><?php echo $row['fleet_name']; ?></td>
<td><?php echo $row['mobile_number']; ?><input type="button" value="select" onclick="clickMe('<?php echo $row['mobile_number']; ?>')" /></td>
<tr>
my button code atm:
function clickMe(number) {
$("#textfield").val(number);
}
and my textfield code:
<input type="text" name="to" id="textfield">
Thank you so much for taking time to read this and any help given.
Try to use the
GROUP_CONCAT(COLUMN_NAME)
this will give the comma separated value of your column values.
You can use PHP implode() function.