Home > OS >  How to place a PHP echo statement inside of an already existing HTML div?
How to place a PHP echo statement inside of an already existing HTML div?

Time:05-19

I have a a page of some HTML with a div, when a button is clicked, PHP echos some HTML text. I would like to be place this echoed text inside of the div but am not sure how to do this.

Code for the php:

if ($result != False)
        {            
            echo '<div id="addBus"><h1>The bus exists!</h1></a></div>';
        } else {
            echo '<div ><h1>Error! Please try again or contact administrator.</h1></a></div>';
        }

HTML code:

<div >
            <form  method="POST">
                <input  id="routeName" type="text" name="routeName"  placeholder="Enter a bus route">
                <button  type="submit" name="action">Enter</button>
            </form>
        </div>

And below is a photo of what the code is currently doing:

A screenshot of the browser page

CodePudding user response:

I think you want a responce to show in html.

You can do like this :

<?php 
$isSuccess = false;  // Creating a varible to later use

if($result != False){  // $result is your varible that you defined.
   $isSuccess = true;
}
?>

Now in your HTML you can do like this :

// After your .settingCont Div your Result Div

<div>
<?php 
if($isSuccess == true){
   echo "<div>Success</div>"
}
?>

</div>

This example Shows you how to use php in middle of HTML.

CodePudding user response:

Im assuming that the OP is trying to display a notification based on the form action and they want it inline. I understand this is pretty basic, and kind of mixes coding styles but Im trying to give an example that is easy to follow, and shows the different options of the syntax that can be used to accomplish what they are trying to do.

$showNotice = $_SERVER['REQUEST_METHOD'] === 'POST' ? true : false;
$message    = '';
$class      = '';
if ($result) {
    $class   .= 'success';
    $message .= 'The bus exists!';
} else {
    $class   .= 'error';
    $message .= 'Error! Please try again or contact administrator.';
}
?>

<div >
    <?php
    if ($showNotice) :
    ?>
    <div ><span><?=$message?></span></div>
    <?php
    endif;
    ?>
    <form  method="POST">
        <input  id="routeName" type="text" name="routeName"  placeholder="Enter a bus route">
        <button  type="submit" name="action">Enter</button>
    </form>
</div>
  • Related