-2

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 class="error"><h1>Error! Please try again or contact administrator.</h1></a></div>';
        }

HTML code:

<div class="settingCont">
            <form class="Usertable" method="POST">
                <input class="routeSettings" id="routeName" type="text" name="routeName" class="validate" placeholder="Enter a bus route">
                <button class="btn waves-effect waves-light routeSettings" 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

Zach Jensz
  • 3,650
  • 5
  • 15
  • 30
  • What div do you want to add to, `settingCont`? The HTML being outputted is a bit strange, there is no link but you are closing an `a`. – user3783243 May 19 '22 at 01:26
  • PHP is processed on the server. If you wish to update the DIV without reloading the page in the browser you'll need to use Javascript. You can use [XHR](https://javascript.info/xmlhttprequest) or [Fetch](https://javascript.info/fetch) to access a PHP script on the server to get information if necessary. But the PHP script will return information through the XHR or Fetch channel and then you'll still need to use Javascript to update the DIV. – bloodyKnuckles May 19 '22 at 01:26
  • I've answered a couple other questions with more details about this. [How to make the value stored in the PHP session update with the Ajax submission](https://stackoverflow.com/questions/72022099/how-to-make-the-value-stored-in-the-php-session-update-with-the-ajax-submission/72023066#72023066) and [jQuery AJAX file upload PHP](https://stackoverflow.com/questions/23980733/jquery-ajax-file-upload-php/23981045#23981045) – bloodyKnuckles May 19 '22 at 01:37
  • PHP is only processed on page load, this will not work. – ethry May 19 '22 at 04:38

2 Answers2

0

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.

DharmanBot
  • 1,066
  • 2
  • 6
  • 10
Yogesh
  • 51
  • 1
  • 5
0

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.

<?php
$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 class="settingCont">
    <?php
    if ($showNotice) :
    ?>
    <div class="notification <?=$class?>"><span><?=$message?></span></div>
    <?php
    endif;
    ?>
    <form class="Usertable" method="POST">
        <input class="routeSettings" id="routeName" type="text" name="routeName" class="validate" placeholder="Enter a bus route">
        <button class="btn waves-effect waves-light routeSettings" type="submit" name="action">Enter</button>
    </form>
</div>
Tyrsson
  • 1
  • 1
  • 3