0

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">&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>

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.

Carla Dessi
  • 9,086
  • 9
  • 39
  • 53

2 Answers2

2

)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">&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>
        ...
        ...

:-)

Jean-philippe Emond
  • 1,619
  • 2
  • 17
  • 29
  • I can't use php in the html file, my php has to be hosted on a separate server. The file with the form in needs to be purely javascript/html – Carla Dessi Apr 16 '14 at 14:31
  • use this [concept](http://stackoverflow.com/questions/979975/how-to-get-the-value-from-url-parameter) and use the $(document).ready(function (){ }); to verify witch div you need to ".show()" – Jean-philippe Emond Apr 16 '14 at 14:35
0

Echo inline JavaScript:

<?php

if (error) {
  echo "<script>$('selector').show()</script>";
} else {
  echo "<script>$('selector').hide()</script>";
}
Szymon Toda
  • 4,454
  • 11
  • 43
  • 62
  • this doesn't work, possibly because the php is in a different file on a different server? – Carla Dessi Apr 16 '14 at 14:34
  • You need to alter that code, it's just a bootstrap. `.show() .hide()` methods are from jQuery framework so naturally I used `$(selector)` to indicate that but it's up to you to select right element. Basicly you want to echo different inline JS code based on your PHP statement. If it works on local console than that soultion should too. – Szymon Toda Apr 16 '14 at 15:27