gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 17:30:30 26 4
gpt4 key购买 nike

我有一个 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com