javascript - 将结果从 php if 语句传递到 javascript/html

标签 javascript php jquery html

我有一个 html 文件,其中包含此表单,上面有两个隐藏的对话框:

      <div class="alert alert-success alert-dismissable" style="display:none;" id="success">
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
        <strong>Success!</strong> Your asset has been added! </div>
      <div class="alert alert-warning alert-dismissable" style="display:none;" id="fail">
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
        <strong>Oops!</strong> Something went wrong! </div>
      <form class="form-horizontal" action="http://localhost/php/insert.php" method="post">
        <fieldset>

          <!-- Form Name -->
          <legend>Add An Asset</legend>

          <!-- Text input-->
          <div class="form-group">
            <label class="col-md-4 control-label" for="name">Name</label>
            <div class="col-md-5">
              <input id="name" name="name" type="text" placeholder="" class="form-control input-md">
            </div>
          </div>

          <!-- Text input-->
          <div class="form-group">
            <label class="col-md-4 control-label" for="serial">Serial Number</label>
            <div class="col-md-5">
              <input id="serial" name="serial" type="text" placeholder="" class="form-control input-md">
            </div>
          </div>

          <!-- Select Basic -->
          <div class="form-group">
            <label class="col-md-4 control-label" for="location">Location</label>
            <div class="col-md-5">
              <select id="location" name="location" class="form-control">
                <option value="1">Option one</option>
                <option value="2">Option two</option>
              </select>
            </div>
          </div>
          <!-- Button -->
            <div class="form-group">
              <label class="col-md-4 control-label" for="singlebutton"></label>
              <div class="col-md-4">
                <button id="singlebutton" name="singlebutton" class="btn btn-primary" type="submit">Submit</button>
              </div>
            </div>
        </fieldset>
      </form>

表单的 php 在这里,它存储在单独的服务器上并引用:

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("dissertation", $con);

$sql="INSERT INTO asset (name, serial)
VALUES
('$_POST[name]','$_POST[serial]')";




if (!mysql_query($sql,$con)) {
    $errcode = "error_code=003";
}

$referer = $_SERVER['HTTP_REFERER'];

if ($errcode) {
    if (strpos($referer, '?') === false) {
        $referer .= "?";
    }

    header("Location: $referer&$errcode");
} else {
    header("Location: $referer");
}
exit;

mysql_close($con)
?>

我需要一些如何更改 php 中的 if 语句,以便在有错误代码时显示警告对话框,如果没有则显示成功对话框。我意识到我需要使用 javascript .show() 和 .hide() 但我不知道如何将 php 响应发送到 javascript/html 文件。我无法将 php 放入 html/javascript 文件中,它们需要保持独立。

最佳答案

)在你的 html 文件中,执行如下操作:

 <div class="alert alert-success alert-dismissable" style="<?php echo (!isset($_GET['error_code'])||empty($_GET['error_code']))?"display:none;":"";?>" id="success">
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
        <strong>Success!</strong> Your asset has been added! </div>
      <div class="alert alert-warning alert-dismissable" style="<?php echo (isset($_GET['error_code'])&&!empty($_GET['error_code']))?"display:none;":"";?>" id="fail">
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
        <strong>Oops!</strong> Something went wrong! </div>
        ...
        ...

:-)

关于javascript - 将结果从 php if 语句传递到 javascript/html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23112312/

相关文章:

javascript - 如何在切换后使用 fullPage.js 移动到节内的特定 div

javascript - 如何读取本地文本文件?

javascript - 获取已在 keydown 上悬停的元素的属性

php - Nginx 和 php 构建的 Docker 执行程序失败

php - 按国家/地区获取 Twitter 主题标签

javascript - onclick 监听器仅按特定顺序工作?

javascript - 如何在 react router dom 中将不存在的路由重定向到主页?

javascript - Angular : Add ngClick from js

php - 从另一个 IP 使用 PDO 连接到 MySQL

jquery - 如何锚定 jQuery Accordion 的第一个 <h3> 元素?