Reputation: 9666
I have a html file including this form with two hidden dialogues above it:
<div class="alert alert-success alert-dismissable" style="display:none;" id="success">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</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">×</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>
and the php for the form is here, which is stored on a separate server and referenced:
<?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)
?>
I need to some how change the if statement in the php to make the warning dialogue shown if there is an error code, and the success dialogue if not. I realise I'll need to use javascript .show() and .hide() but I don't know how to send the php response to the javascript/html file. I can't put the php in the html/javascript file, they need to stay separate.
Upvotes: 0
Views: 75
Reputation: 1614
)in your html file, do something like:
<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">×</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">×</button>
<strong>Oops!</strong> Something went wrong! </div>
...
...
:-)
Upvotes: 2
Reputation: 4516
Echo inline JavaScript:
<?php
if (error) {
echo "<script>$('selector').show()</script>";
} else {
echo "<script>$('selector').hide()</script>";
}
Upvotes: 0