【发布时间】:2018-05-16 08:45:13
【问题描述】:
我一直在这个网站上到处搜索并尝试了许多解决方案,但没有一个对我有用!我的问题如下:
我有 2 张桌子:
TABLE INFO_STATUS (status_id, status_desc)
和TABLE INFO_MARQUES (comp_id, comp_desc, comp_status(INT))
我正在链接 comp_status 和 status_id 以便 INFO_MARQUES 中的行获得当前状态。
我想做什么? (从这里编辑)
2。获取位于comp_status的info_marques的当前状态值,当我打开更新表单时自动在下拉列表中选择
示例我想更新这家公司:
comp_id : 15
comp_desc : WEISS
comp_status : 1
假设在表 info_status 中:
status_id = 1
status_desc = FOLLOW-UP
当我打开更新表单时,我想看到这个: Check this
我的模态:(WORKING)
<select class="form-control" name="select_status">
<option value=''>Selectionner</option>
<?php
try {
$user = new USER();
$output = array();
$stmt = $user->runQuery("SELECT * FROM info_status");
$stmt->execute();
$result=$stmt->fetchAll();
foreach($result as $row) {
echo '<option value='.$row["status_id"].'>'.$row["status_desc"].'</option>';
}
}
catch(PDOException $e) {
printf('Erreur MySQL %d : %s', $e->getCode(), $e->errorInfo[2]);
exit;
}
?>
</select>
我的阿贾克斯
$(document).on('click', '.update', function(){
var user_id = $(this).attr("id");
var array = [];
$('select :selected').each(function(i,value)
{
array[i] = $(this).val();
});
$.ajax({
url:"partials/test/fetch_single.php",
method:"POST",
data:{user_id:user_id},
dataType:"json",
success:function(data)
{
$('#userModal').modal('show');
$('#comp_name').val(data.comp_name);
$('#comp_parent').val(data.comp_parent);
$.each($('.select_status option'),function(a,b){
if($(this).val()== data.comp_status){
$(this).attr('selected',true)
}
});
$('.modal-title').text("Modifier un fichier client");
$('#user_id').val(user_id);
$('#user_uploaded_image').html(data.user_image);
$('#action').val("Edit");
$('#operation').val("Edit");
}
})
});
然后是我的 PHP 页面:(WORKING)
if(isset($_POST["user_id"]))
{
$output = array();
$statement = $connection->prepare(
"SELECT *
FROM info_marques AS m
LEFT JOIN info_status AS s
ON s.status_id = m.comp_status
WHERE m.id = '".$_POST["user_id"]."'
LIMIT 1"
);
$statement->execute();
$result = $statement->fetchAll();
foreach($result as $row)
{
$output["comp_name"] = $row["comp_name"];
$output["comp_parent"] = $row["comp_parent"];
$output["comp_status"] = $row["status_desc"];
}
echo json_encode($output);
}
我在这里错过了什么?
干杯
【问题讨论】:
-
#comp_name、#comp_parent等在哪里? -
我只展示了这个问题涉及的模态部分,但这两个在我的模态中工作正常。
-
$('#select_status').val(data.comp_status);不起作用,因为select元素在其中需要option元素。您需要填充select并将selected设置为 AJAX 输出的代码。 -
感谢您的提示,所以我需要将该代码包含到我的 php 代码中并返回一个 .html 值?那么我需要编写 2 个 SQL 查询吗?
-
您需要
SELECT查询才能在 AJAX 请求之前输出到模式。在 AJAXsuccess上,循环通过options 并设置属性选择如果 comp_status 等于选项值。
标签: php jquery sql ajax modal-dialog