php - 如何使用php返回ajax
问题描述
我有一个关于如何php
使用 ajax 在 jquery 中返回的问题。
我有 html 字段:
<input type='text' id='name'>
<input type='text' id='color'>
<input type='text' id='material'>
我将它的值发送到 php 以检查它是否存在
在php中我有如果将错误号写入数组$errors:
if($_POST['name'] == "") { $errors[] = "1"; }
if($_POST['name'] == "Name") { $errors[] = "2"; }
最后 php 返回带有错误的数组:
foreach($errors AS $error) {
echo $error";
}
现在,当我收到错误时,我想对 css 进行更改。像这样的东西
success: function(html){
if(html.$errors.1) {do this} //this is not correct js I know, but please understand me, I am just learning
if(html.$errors.2) {do this}
}
问题是我不知道如何访问错误;html 返回类似这样的内容:123 如果例如第二个字段没有错误,则返回:13
你们能帮我解决这种情况吗?
先感谢您!
解决方案
尝试这样的事情
<input type='text' id='name'>
<span id="name_err" style="display:none;">Please enter name</span>
<input type='text' id='color'>
<span id="color_err" style="display:none;">Please enter color</span>
<input type='text' id='material'>
<span id="material_err" style="display:none;">Please enter material</span>
在Javascript中,这样写
if(html.$errors.2)
{
document.getElementById("color").style.display = "block";
}
if(html.$errors.1)
{
document.getElementById("name").style.display = "block";
}
if(html.$errors.3)
{
document.getElementById("material").style.display = "block";
}
推荐阅读
- python - 使用 sqlite3 和 python 验证用户详细信息
- javascript - 如何通过提供来自另一个进程的参数来执行文件
- xml - xml 响应中的错误代码 2
- python - Airflow PostgresOperator 仅在与 DAG 相同的文件夹中查找 SQL 文件
- c# - 如何在 Android.Media.Mediaplayer 中播放 webm?(错误(1,-2147483648))
- flowground - 流场原木的保存期限
- excel - Excel VBA 在共享文件中打开时运行代码
- sql - SQL Server:内部联接运行总计
- postgresql - 通过创建数据源与 PostgreSQL 12 连接时出错
- list - 在 ocaml 中打印 int -> int list