Select-option breaks the mark-up and doesn't post - php

I'm trying to implement live-table edit, but I'm having some trouble with my select options. The input type="text" is fully functional though.
Ever since I tried to add select, it seemed to brake my entire lay-out. First there were multiple rows and columns displayed on the page. But now there's only one and the columns show up empty after the one that contains select.
And the second problem is that ajax doesn't post the table edit since I added select to the mark-up. I'm thinking that this is because the mark-up breaks, but I'm not sure.
If anybody knows what to do, I'd appreciate it.
Markup
<?php
$query = ("select * from projectlist");
$resultaat = mysql_query($query) or die (mysql_error());
while($row = mysql_fetch_array($resultaat, MYSQL_ASSOC)){
$id = $row['projectid'];
?>
<tr class="predit">
<form action="" method="post">
//Working input
<td>
<span id="klant_<?php echo $id; ?>" class="text"><?php echo $row["Klant"]; ?></span>
<input type="text" class="ip" id="klant_ip_<?php echo $id; ?>" value="<?php echo $row["Klant"]; ?>">
</td>
//Same approach, but with select instead of input
<td>
<span id="project_<?php echo $id; ?>" class="text"><?php echo $row["Project"]; ?></span>
<select id="project_ip_<?php echo $id; ?>" class="ip">
<?php
//Fetch select options from another table
$query = ("select * from projecten");
$resultaat = mysql_query($query) or die (mysql_error());
while($row = mysql_fetch_array($resultaat, MYSQL_ASSOC)){
$listid = $row["projectcode"];
$projectnaam = $row["projectnaam"];
?>
<option value="<?php echo $projectnaam; ?>" id="<?php echo $listid; ?>"><?php echo $projectnaam; ?></option>
<?php
}
?>
</select>
</td>
</tr>
</form>
<?php
}
?>
JQuery Ajax
$(document).ready(function(){
//Projeclist
$(".predit").click(function(){
var ID=$(this).attr('id');
$("#klant_"+ID).hide();
$("#project_"+ID).hide();
$("#klant_ip_"+ID).show();
$("#project_ip_"+ID).show();
}).change(function(){
var ID=$(this).attr('id');
var klant=$("#klant_ip_"+ID).val();
var project=$("#project_ip_"+ID).val();
var dataString = 'id='+ ID
+'&Klant='+klant
+'&Project='+project;
//alert(dataString);
var project_txt = $("#project_ip_"+ID+" option:selected").text();
$.ajax({
type: "POST",
url: "post_table.php",
data: dataString,
cache: false,
success: function(html){
$("#project_"+ID).html(project_txt);
$("#klant_"+ID).html(klant);
},
error: function (request, error) {
console.log(arguments);
alert(" Can't do because: " + error);
},
});
});
$(".ip").mouseup(function() {
return false
});
$(document).mouseup(function(){
$(".ip").hide();
$(".text").show();
});
});
post_table.php
<?php
include('config.php');
$klant = $_POST['Klant'];
$project = $_POST['Project'];
$id = $_POST['id'];
$query = "update projectlist
set Klant='$klant',
Project='$project'
where projectid='$id'";
mysql_query($query, $con);
?>

Related

get array id value from checkbox to jquery function

I want to get the value of the id in the checkbox. but the variable in id always give the 1st value. how can send php array to jquery array. thanks
require "connection.php";
$sql = mysqli_query($con,"SELECT * FROM position ORDER BY position_align + 0 ASC");
$switch = array();
$pid = array();
while($row = mysqli_fetch_assoc($sql)){
$pcode = $row['position_code'];
$pdesc = $row['position_desc'];
$status = $row['status'];
?>
<tr>
<td><?php echo $pcode; ?></td>
<td><?php echo $pdesc; ?></td>
<td>
<div class="custom-control custom-switch">
<?php
if($status == 'active'){
?>
<div class="onoffswitch4">
<input type="checkbox" name="onoffswitch4" class="onoffswitch4-checkbox switch" onclick="change(this.value)" id="myonoffswitch" checked value="<?php echo $pcode; ?>">
<label class="onoffswitch4-label" for="pid[]">
<span class="onoffswitch4-inner"></span>
<span class="onoffswitch4-switch"></span>
</label>
</div>
<?php
}else{
?>
<div class="onoffswitch4">
<input type="checkbox" name="onoffswitch4" class="onoffswitch4-checkbox switch" onclick="change(this.value)" id="aaa[]" value="<?php echo $pcode; ?>">
<label class="onoffswitch4-label" for="myonoffswitch">
<span class="onoffswitch4-inner"></span>
<span class="onoffswitch4-switch"></span>
</label>
</div>
<?php
}
?>
$('.switch').on('change', function() {
if(this.checked) {
var id = this.value;
$.ajax({
url: "admin_change_status.php",
data: {
id:id
},
type: "POST",
success: function(result){
}
});
}else{
var id2 = this.value;
$.ajax({
url: "admin_change_status.php",
data: {
id2:id2
},
type: "POST",
success: function(result){
}
});
}
});
i expect to get the value of id in checkbox to jquery.
If you want to get all values, you can only get it by classname not by id. You have to loop though all classes names and get each one of them, you can do this by each function. Each function loop though all the elements.
$('.onoffswitch4-checkbox').each(function(i){
var statsValue = $(this).val();
console.log(statsValue);
});

Display Table based on dropdownlist selection in PHP

My table is not displayed when i select any project name. I guess the onchange function is not working properly but i couldn't figure out the problem.
Code is as follows:
<div class="span6">
<?php $sql = "SELECT * from login_projects WHERE login_id='".$record['login_id']."'";
$res_sql = mysql_query($sql); ?>
<label>Project Name <span class="f_req">*</span></label>
<!--<input type="text" name="city" class="span8" />-->
<select name="project_name" onchange="get_list_onnet()" id="project_name" class="span8">
<option value="">--</option>
<?php while($rec_sql = mysql_fetch_array($res_sql)){ ?>
<option value="<?php echo $rec_sql['project_id']; ?>">
<?php echo $rec_sql['project_name']; ?></option>
<?php } ?>
</select>
</div>
Function:
<script>
function get_list_onnet(){
var project_name=$("#project_name").val();
$.ajax
({
type: "POST",
url: "ajax.php",
data: {action: 'get_list_onnet',list_onnet:project_name},
success: function()
{
document.getElementById("dt_a").style="block";
$("#dt_a").html(html);
}
});
};
</script>
<script>
$(document).ready(function() {
//* show all elements & remove preloader
setTimeout('$("html").removeClass("js")',1000);
});
</script>
Ajax.Php Page:
function get_list_onnet(){
$list_onnet=$_POST['list_onnet'];
$sql_list_onnet=mysql_query("SELECT * from projects,project_wise_on_net_codes
where projects.project_id = project_wise_on_net_codes.project_id AND
project_wise_on_net_codes.project_id='$list_onnet'");
$row1 = mysql_num_rows($sql_list_onnet);
if($row1>0)
{
echo "<tr><th>id</th><th>Project Name</th><th>Country Code</th><th>On-net prefix</th>
<th>Action</th></tr>";
$k = 1; while($row_list_onnet=mysql_fetch_array($sql_list_onnet))
{
$project3 = $row_list_onnet['project_name'];
$countrycode1 = $row_list_onnet['country_code'];
$prefix1 = $row_list_onnet['on_net_prefix'];
$id_proj = $row_list_onnet['project_id'];
$on_prefix = $row_list_onnet['on_net_prefix'];
echo "<tr><td>".$k."</td><td>".$project3."</td><td>".$countrycode1."</td>
<td>".$prefix1."</td><td><a href='process/update_process_onnet.php?ID=".$id_proj."&Onnet=".$on_prefix."'>Delete</a></td>
</tr>";
$k++;
}
}
else
{
echo "<script>alert('No Record Found')</script>";
}
}
The problem is that it is always going in the else condition and nothing is displayed in the table.

how to fill options in html select based on an html field filled by user.

Here is the code snippet :
<select name="isbn" onchange=" ">
<?php
//Displaying all ISBN in drop down
$result = mysqli_query($con,"SELECT * FROM book");
while($row = mysqli_fetch_array($result)) {
?>
<option> <?php echo $row['ISBN']; ?></option>
<?php } ?>
</select>
</label></td>
</tr>
<tr>
<td>Copy Number </td>
<td><select name="copy_number">
<?php
//Now based on selected book I want to fetch number of copies from database
$result = mysqli_query($con,"SELECT number_of_copies FROM book where isbn = [ SELECTE VALUE FROM ABOVE]");
?>
How can I do that?
You can do that with jQuery ajax and simple php handler to get copies of selected book,
<select name="isbn">
.....
</select>
<div id="copies"></div>
<script>
$.ajax({
url: "getCopies.php?",
data: "isbn=" + $("select[name='isbn']").val(),
type: "POST",
dataType: "json",
success: function(response) {
$.each(response, function(i, item) {
$("#copies").append(item.name); // Sample json format {id: "213123", name:"Lord of the rings", isbn:"887799..."}
})
}
});
</script>
getCopies.php
<?php
$isbn = $_POST["isbn"];
// Some db connections
$result = mysqli_query($con,"SELECT number_of_copies FROM book where isbn = $isbn");
$resultArr = array();
while($row = mysqli_fetch_array($result)) {
$resultArr[] = $row;
}
echo json_encode($resultArr); // This will return rows in json format. You can iterate it in js side

how to insert php statement into jquery/javascript

I am new in php. When user select one of the option in dropdown menu, the list of selected item will shows in textarea form. I want to make each of every item on that list, link to another page. How do I write php/html statement in javascrip/jquery? Here is my javascript :
<script type="text/javascript">
function check(){
var select = document.getElementById('category');
var textarea = document.getElementById('model');
$.ajax({
type: 'POST',
url: 'http://localhost/system/ajax.php',
data: {txt:$('#category').val()},
dataType: 'json',
success: function(resp){
result = resp.success;
textarea.value = "";
for(i=0; i<result.length; i++){
textarea.value = result[i] += "\n" + textarea.value;
}
}
});
}
This is my html code :
<form action="" method="post">
<tr>
<td width="116">Category</td>
<td width="221">
<center>
:
<select name="category" id="category" onChange="check()">
<option>--- Choose Category ---</option>
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("inventory");
$sql = mysql_query("SELECT * FROM equipment GROUP BY equip_category ASC ");
if(mysql_num_rows($sql) != 0){
while($row = mysql_fetch_array($sql)){
echo '<option value="'.$row['equip_category'].'">'.$row['equip_category'].'</option>';
}
}
?>
</select >
</center>
</td></td></tr>
<tr>
<td><p>Model/Brand</p>
<td>
<p align="center">:<textarea name="model" id="model" rows="5" cols="25"><?php echo (''); ?></textarea>
</p>
</td></td>
</tr></form>
To insert PHP into jQuery/JS, use the following syntax:
var phpVar = <?php echo $some_php_var;?>;
console.log(phpVar); //logs $some_php_var
However, oftentimes it is best to just echo jQuery/JS in PHP:
<?php
echo 'var phpVar = '.$some_php_var.';',
'console.log(phpVar);';
?> <!--logs $some_php_var-->
In PHP use $_POST['txt'] to access the value of category that you are passing from Javascript.
Check out this tutorial for connecting PHP-Javascript-MySQL.
this is my ajax.php :-
<?php
if(!empty($_POST)) {
include "connect.php";
$txt = $_POST['txt'];
$query = "SELECT equip_name_desc FROM equipment WHERE equip_category = '$txt'";
$number = 0;
$result = mysql_query($query)
or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$time = $row['equip_name_desc'];
$testarray[$number] = $time;
$number = $number+1;
}
echo json_encode(array('success'=>$testarray));
}
?>
Below is the example of output. I want to make 'Lenovo GGG' or 'HP' is a link to another page. Question is, how to write the href link in javascript?

Chained ajax Select Boxes

I have some problems with my html/php/ajax code about dependent (or chained) select. I want to show in my menu the list of faculties after I have decided the university.
I'll show you my (italian) code. I hope you'll help me. Thanks.
javascript ajax code:
<script type="text/javascript">
$(document).ready(function()
{
$(".universita").change(function()
{
var dataString = 'id='+ $(this).val();
$.ajax
({
type: "POST",
url: "ajax_facolta.php",
data: dataString,
cache: false,
success: function(html)
{
$(".facolta").html(html);
}
});
});
});
html code about two select boxes:
<td align="right">Università: </td>
<td>
<select class="input" name="universita">
<option selected="selected">--Seleziona Università--</option>
<?php
require('config.php');
$query = mysqli_query($con, "SELECT * FROM UNIVERSITA order by id ASC");
$num_righe = mysqli_num_rows($query);
for($x=0; $x<$num_righe; $x++)
{
$rs = mysqli_fetch_row($query);
$id = $rs[0];
$nome = $rs[1];
?>
<option value="<?php echo $id;?>"> <?php echo $nome; ?></option>
<?php
}
?>
</select></td>
</tr>
<tr>
<td align="right">Facoltà: </td>
<td><select class="input" name="facolta">
<option selected="selected">--Seleziona Facoltà--</option>
</select></td>
</tr>
the file ajax_facolta.php:
<?php
require('config.php');
if($_POST['id'])
{
$id=$_POST['id'];
$sql = mysqli_query($con, "SELECT * FROM FACOLTA WHERE id_univ='$id' ");
echo '<option selected="selected">--Selziona Facoltà--</option>';
while($row=mysqli_fetch_array($sql))
{
$id=$row['id'];
$nome=$row['nome'];
echo '<option value="'.$id.'">'.$nome.'</option>';
}
}
?>
and the simple configure.php:
<?php
$con = mysqli_connect("127.6.143.130","xxxxx","xxxxx", "jeme");
if (!$con)
{
die('Errore nella connessione: ' . mysqli_connect_error());
}
?>
The database is very simple.
UNIVERSITA has (id, nome)
FACOLTA has (id, nome, id_univ).
I do not find any errors but it does not work. Thanks for the help.
In HTML you have :
<select class="input" name="universita">
and you are using
$(".universita").change(function()
$(".universita") will try to search HTML control having class name "universita"
So change few things in code. I hope it will work
1). <select class="input" name="universita" id="universita">
2). $("#universita").change(function()
3). <td><select class="input" name="facolta" id="facolta">
4). $("#facolta").html(html);
let me know is this helpfully?.

Categories