![](/img/trans.png)
[英]Populate id/text select2 with php's array data initialized in js with json_encode
[英]Json encode array from PHP to populate in specific html id
我正在以一种实际有效的形式工作,并发送电子邮件。 最后,它在html中显示已成功发送的消息,但我希望将该消息添加到html上的特定ID。 我在php中的最后一段代码是
if(!$send_mail)
{
//If mail couldn't be sent output error. Check your PHP email configuration (if it ever happens)
$output = json_encode(array('type'=>'error', 'text' => 'Problem in server php.'));
die($output);
}else{
$output = json_encode(array('type'=>'message', 'text' => 'thanks '.$user_name .' for your email'));
die($output);
}
然后,我希望将错误或成功的输出添加到特定的HTML ID。 我在jQuery中的编码是:
$.post('contact_me.php', post_data, function(response){
if(response.type == 'error'){ //load json data from server and output message
output = '<div class="error">'+response.text+'</div>';
}else{
$('#aniconf').removeClass('confcir').addClass('confcir2');
output = '<div class="success">'+response.text+'</div>';
//reset values in all input fields
$("#contact_form input[required=true], #contact_form textarea[required=true]").val('');
}
$("#contact_form #contact_results").hide().html(output).slideDown();
}, 'json');
}
});
更改此部分$("#contact_form #contact_results").hide().html(output).slideDown();
至
$('#here').html(output);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.