Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    May 2010
    Posts
    211
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Trouble passing javascript value to php

    I have this code. It should display "ok" on test.php page after submit right? But it does not display it.

    test.html

    Code:
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Untitled Document</title>
    </head>
    
    <body>
    
    <script language='javascript'>
    
    document.getElementById("myValue").value="ok";
    </script>
    
    <form name="myForm" method="POST" action="test.php">
    <INPUT TYPE="hidden" name="myVal" id="myValue" />
    <INPUT TYPE="submit">
    </form>
    </body>
    </html>

    test.php

    PHP Code:
    <?php
    echo $_REQUEST['myVal'];
    echo 
    $_POST['myVal'];

    ?>

  • #2
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    you have nothing telling the javascript to execute that

    try
    PHP Code:
    <html>
    <
    head>
    <
    meta charset="UTF-8">
    <
    title>Untitled Document</title>
    </
    head>

    <
    body onload="setvalue()">

    <
    script language='javascript'>
    function 
    setvalue(){
    document.getElementById("myValue").value="ok";
    }
    </script>

    <form name="myForm" method="POST" action="test.php">
    <INPUT TYPE="hidden" name="myVal" id="myValue" />
    <INPUT TYPE="submit">
    </form>
    </body>
    </html> 
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #3
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    852
    Thanks
    173
    Thanked 94 Times in 94 Posts
    not a php problem, but anyway

    Your JS is run before your html is ready.

    put your js before closing </body>

    Code:
    <form name="myForm" method="POST" action="test.php">
    <INPUT TYPE="hidden" name="myVal" id="myValue" value="" />
    <INPUT TYPE="submit">
    </form>
    <script type="text/javascript">
    document.getElementById("myValue").value="ok";
    </script>
    "The greatest revenge is to accomplish what others say you cannot do."
    ~ Unknown

    I used to be indecisive, but now I'm not so sure.

  • #4
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,302
    Thanks
    28
    Thanked 276 Times in 270 Posts
    Quote Originally Posted by Anishgiri View Post
    I have this code. It should display "ok" on test.php page after submit right? But it does not display it.
    The input element doesn't exist at the time it's processed which results in an error. Look at your browser's error console and you'll see it. Revised code with fixes for all of your other HTML issues:

    Code:
    <!doctype html>
    <html lang="en">
    	<head>
    		<meta charset="utf-8">
    		<title>Untitled Document</title>
    		<script>
    			(function () {
    				"use strict";
    				function change_value() {
    					document.getElementById("myValue").value = "ok";
    				}
    				document.addEventListener("DOMContentLoaded", change_value);
    			})();
    		</script>
    	</head>
    	<body>
    		<form id="myForm" method="post" action="test.php">
    			<input type="hidden" id="myValue" name="myVal">
    			<button>Submit</button>
    		</form>
    	</body>
    </html>
    For every complex problem, there is an answer that is clear, simple, and wrong.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •