How to Change a PHP Variable With JavaScript

PHP is a server-side language used to render web pages. To edit a variable with JavaScript, you must create a form field element. The form element you use is then used by PHP to dynamically create content when the user clicks "Submit" and moves to the next PHP page. To create the form element, write the element tag and value to the page using the JavaScript language.

Instructions

    • 1

      Right-click the PHP file you want to edit and select "Open With." If you have a PHP editing program you use to edit the code, double-click it. If not, Notepad is an alternative to a PHP editor and is included with Windows.

    • 2

      Type the PHP code to write the form element to the page. You can set the element as hidden, so it does not show up as text in the reader's browser. Type the following code to write an element to the PHP page:

      echo "<input type=hidden name=PHPvalue>";

      This code writes the form element to the page that you use to edit a PHP value in JavaScript.

    • 3

      Write the JavaScript code to edit the form value. Type the following code within the "<script>" and "</script>" tags:

      document.all("PHPValue").value = 'myvalue';

      This JavaScript code sets the value for the hidden form element. You use this value to generate code in PHP. Replace "myvalue" with your own value.

    • 4

      Press "Ctrl+S" to save the changes. Use the PHP editor to test the JavaScript or run the file in your web browser for testing.

Related Searches:

References

Comments

You May Also Like

Related Ads

Featured