i want the function(data) to get 2 attribute value from the database
This is the select option that display the display the address and contact value of the selected option in an input field
$('#recipient').change(function(){
var FULL_NAME = $(this).val();
$.ajax({
url:"load_data.php",
method:"POST",
data:{FULL_NAME:FULL_NAME},
success:function(data){
$('#address').val(data);
$('#contact').val(data);
}
});
});
and this is the load_data.php
<?php
$sql = "SELECT * FROM recipient";
$result = mysqli_query($connect, $sql);
while($row = mysqli_fetch_array($result))
{
$output1 = $row["ADDRESS"];
$output2 = $row["CONTACT"];
$arr = array($output1,$output2);
}
echo $output1,$output2;
?>
How do i pass the $output1 into $('#address').val(data) and $output2 into $('#contact').val(data)
$('#recipient').change(function(){
var FULL_NAME = $(this).val();
$.ajax({
url:"load_data.php",
method:"POST",
dataType: "json",
data:{FULL_NAME:FULL_NAME},
success:function(data){
$('#address').val(data['ADDRESS']);
$('#contact').val(data['CONTACT']);
}
});
});
and the load_data.php
<?php
$result = mysqli_query($connect, $sql);
while($row = mysqli_fetch_array($result))
{
$arr["ADDRESS"] = $row["ADDRESS"];
$arr["CONTACT"] = $row["CONTACT"];
}
echo json_encode($arr);
?>
Related
My jQuery Ajax code:
$(".comment").click(function(){
var id = $(this).data("id");
var name = $("#username_"+id).val();
if(name==''){
alert("Please Fill All Fields");
}else{
$.ajax({
type: "POST",
url: "comments",
data:{username:name},
dataType: "JSON",
success: function(jsonStr){
$("#cmt_output").html(JSON.stringify(jsonStr));
}
});
}
});
My Php Code:
............
$con = mysqli_connect("localhost","root","","quotes") or die("Error".mysqli_error($con));
$name = $_POST['username'];
$user = get_userdetails();
$vw_id = $user->id;
$query = mysqli_query($con,"INSERT INTO tlb_comments(user_id,comments) values ('$vw_id','$name')");
$comments = mysqli_query($con,"SELECT * FROM tlb_comments");
$avatar = mysqli_query($con,"SELECT * FROM tlb_avatar WHERE `vw_id`='".$vw_id."'");
$avatar_select = mysqli_fetch_all($avatar);
$comment_select = mysqli_fetch_all($comments);
array_push($avatar_select,$comment_select);
echo json_encode($avatar_select);
getStudents.js
$("#submit").click(function(){
$.ajax({
url: "displayStudents.php?branchCode=1",
datatype:"JSON",
success: function(obj){
for(var i=0; i<obj.length; i++){
$("ul").append(obj[i].name)
}
}
})
});
displayStudents.php
<?php
include 'config.php';
$branchCode = $_GET['branchCode'];
$list = mysqli_query($con, "Select * from student where branchCode = '$branchCode' ORDER BY rollNo");
$result = array();
while($row = mysqli_fetch_array($list)){
$result[] = array("RollNo"=>$row['rollNo'], "Name"=>$row['name']);
}
header("Content-type: application/json");
echo json_encode($result);
?>
The php file 'displayStudents.php' is sending the json correctly but I'm not able to display the names of the students.
I have table users.
id
login
password
I want to display the data in JSON format through php mysql
page: config.php
$rep = $db->query("SELECT * FROM users");
$array_user = array();
while($data = $rep->fetch()){
$array_user = $data;
}
echo json_encode($array_user);
?>
page listUsers.php
<div id="tab"></div>
<script>
$(document).on("ready",function(){
loadData();
});
var loadData = function(){
$.ajax({
type:"POST",
url:"config.php"
}).done(function(data){
console.log(data);
var users = JSON.parse(data);
for(var i in users){
$("#tab").append(users[i].login + "<br>");
}
});
}
</script>
but it appears to me "undefined".
Use this
<?php
$rep = $db->query("SELECT * FROM users");
$array_user = array();
while($data = $rep->fetch()){
$array_user[] = $data;
}
echo json_encode($array_user);
?>
<div id="tab"></div>
<script>
$(document).on("ready",function(){
loadData();
});
var loadData = function(){
$.ajax({
type:"POST",
url:"config.php"
}).done(function(data){
console.log(data);
var users = JSON.parse(data);
for(var i in users){
$("#tab").append(users[i].login + "<br>");
}
});
}
</script>
Hello im doing some try and error. This is the code where select-option populate from database but this gives me null value
echo "<option value=\"\">"."Select"."</option>";
$qry = "select * from try where name = '".$_POST['name']."'";
$result = mysqli_query($con,$qry);
while ($row = mysqli_fetch_array($result)){
echo "<option value='".$row['trynum']."'>".$row['tryname']."</option>";
}
$.ajax({
type: "POST",
url: "json_php_sub.php",
data: {instructor:$(this).val()},
datatype: 'json',
success: function(result){
alert(result);
document.getElementById("sub").innerHTML = result;
}
});
<select id="sub" name="subb"></select>
my problem is whether i select from dropdown the content is there but no value. pls help..
PHP:
$ajaxAnswer = "<option value=\"\">"."Select"."</option>";
$instructor = mysqli_real_escape_string($conn,$_POST['instructor']);
$qry = "select * from try where name = '".$instructor."'";
$result = mysqli_query($con,$qry);
while ($row = mysqli_fetch_array($result)){
$ajaxAnswer .= "<option value='".$row['trynum']."'>".$row['tryname']."</option>";
}
echo $ajaxAnswer;
Jquery:
$.ajax({
type: "POST",
url: "json_php_sub.php",
data: {instructor:$(this).val()},
success: function(result){
$("#sub").html(result);
}
});
data: {instructor:$('#SELECT_ELEMTN_ID').val()},
Depending on scope and stuff, you may not wanna use "this".
Jquery
$(document).ready(function () {
$.ajax({
type: "GET",
url: "phpfile.php",
dataType: "json",
success: function (data) {
$.each(data, function (idx, obj) {
$('#selectdata').append('<option value="'+obj.user_id+'">'+obj.user_name+'</option>' )
});
}
});
});
</script>
</head>
<body>
<select id="selectdata">
</select>
</body>
phpfile.php
<?php
$host = "localhost";
$user = "root";
$password ="";
$database= "databasename";
$con = mysqli_connect($host , $user , $password);
$database_connect = mysqli_select_db($con, $database);
$result = mysqli_query($con, "select Id as user_id,Name as user_name from users");
$data = mysqli_fetch_all($result, MYSQLI_ASSOC);
echo json_encode($data);
?>
I'm trying to select various bits of data out of my insert.php page. Such as post id, username and users id so far.. And I will be adding other bits of selected data. Now I could get the id of the div with response, but when I added more queries and echo them out they ended up all together so I had my postidusernameuserid all in one string. How do I separate them?
Also I'm aware of the depreciated MYSQL_ I just haven't got around to updating my code yet.
AJAX
<script>
$(document).ready(function(){
$("form#myform").submit(function(event) {
event.preventDefault();
var content = $("#toid").val();
var newmsg = $("#newmsg").val();
$.ajax({
type: "POST",
url: "insert.php",
data: {toid:content, newmsg: newmsg},
success: function(response){
$("#homestatusid").prepend("<div id='divider-"+response+"'><div class='userinfo'>"+newmsg+"<a href='/profile.php?username="+response+"'><img class='stream_profileimage' style='border:none;padding:0px;display:inline;' border=\"0\" src=\"imgs/cropped"+response+".jpg\" onerror='this.src=\"img/no_profile_img.jpeg\"' width=\"40\" height=\"40\" ></a><div style='cursor:pointer;position:relative;top:0px;float:right;padding-right:5px;' onclick=\"delete_('"+response+"');\">X</div></div></div>");
}
});
});
});
</script>
INSERT.PHP
$check = "SELECT streamitem_id FROM streamdata";
$check1 = mysql_query($check);
$resultArr = mysql_fetch_array($check1);
echo $resultArr['streamitem_id'];
$check = "SELECT username,id FROM users";
$check1 = mysql_query($check);
$resultArr = mysql_fetch_array($check1);
echo $resultArr['username'];
echo $resultArr['id'];
Is it a case of success: function(response,responsetwo){ and so on.
In PHP try returning the data in JSON format.
echo json_encode(array);
The response variable in JavaScript will be an object you can get the data from.
console.log(response['username']);
JavaScript:
$(document).ready(function(){
$("form#myform").submit(function(event) {
event.preventDefault();
var content = $("#toid").val();
var newmsg = $("#newmsg").val();
$.ajax({
type: "POST",
url: "insert.php",
dataType: "json",
data: { toid: content, newmsg: newmsg },
success: function(response){
$("#homestatusid").prepend("<div id='divider-"+response['streamitem_id']+"'><div class='userinfo'>"+newmsg+"<a href='/profile.php?username="+response['username']+"'><img class='stream_profileimage' style='border:none;padding:0px;display:inline;' border=\"0\" src=\"imgs/cropped"+response['id']+".jpg\" onerror='this.src=\"img/no_profile_img.jpeg\"' width=\"40\" height=\"40\" ></a><div style='cursor:pointer;position:relative;top:0px;float:right;padding-right:5px;' onclick=\"delete_('"+response['id']+"');\">X</div></div></div>");
}
});
});
});
PHP:
$json = array();
$check = "SELECT `streamitem_id` FROM `streamdata`";
$check1 = mysql_query($check);
$resultArr = mysql_fetch_array($check1);
$json['streamitem_id'] = $resultArr['streamitem_id'];
mysql_free_result($check1);
$check = "SELECT `username`, `id` FROM `users`";
$check1 = mysql_query($check);
$resultArr = mysql_fetch_array($check1);
$json['username'] = $resultArr['username'];
$json['id'] = $resultArr['id'];
mysql_free_result($check1);
echo json_encode($json);
I took the liberty of guessing where you wanted to use 'username', 'id', and 'streamitem_id'.