When does PHP variable and HTML sync?

You will see in the code that first_name is populated by two different methods. 1) JQuery will populate html first_name with barney and 2) Php will populate the $first_name with fred.

barney displays on the rendered html. When I inspect the field with Developer Tools, I see the value as fred.

At what point does the PHP field match the html? When the form posts?

[php]


<SCRIPT LANGUAGE="javascript" >
$(window).load(function(){    

    $("#first_name").val("barney");

});    

</SCRIPT>
    </head>
    <body>
        <?php
        $first_name = "fred";
        ?>
      <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
     	Name: <input type="text" id="first_name" name="first_name" value="<?php echo $first_name;?>">  
   	<input type="submit" name="submit" value="Submit"> 
      </form>
    </body>
</html>

[/php]

PHP is a server-side language.

  1. PHP parses the page and serves it to the client.
  2. (In your case) The HTML is loaded and when the page loads everything needed, the Jaascript replaces the value.

You can test this by have the PHP output something like
echo “”;

and then the same type of alert in the onLoad function in the js.

Sponsor our Newsletter | Privacy Policy | Terms of Service