I have written a code and now i want to view the output of user selected radio button in the same php page?
How can i do it? Here is my code
<html>
<div>
<form>
<input type='submit' name='radiotest' value='1crn' /> </div>
<input type='submit' name='radiotest' value='1FFZ' /> </div>
</form>
</div>
<div>
<head>
<script src="jmol/Jmol.js"></script>
</head>
<script>
jmolInitialize("jmol");
</script>
<script>
jmolSetAppletColor("gray");
jmolApplet(450, "load <?php echo "/jmol/".$_REQUEST['radiotest'].".pdb"; ?>; select all; cartoon on; wireframe off; spacefill off; color chain; ");
</script>
</div>
</html>
Any suggestions?
This should not be done using PHP (javascript would be a better candidate), but to answer the question:
Make the form so it has an action and method attributes. Action will be the current page (you don't need to specify this attribute if it's the same page) and method will be GET or POST.
Something like this:
<form action="" method="GET">
Now, when you click the button, the form gets submitted and the page reloads but the URL is different:
http://site.com/page.php?radiotest=1crn
You can now check to see if the $_GET['radiotest']
is set using isset()
function. If it is set, do whatever you want it to do.
isset($_GET['radiotest'])
- tells you if radiotest=something
is in the URL
$_GET['radiotest']
- gives you the value of "something" from radiotest=something
in the URL.
Because PHP is a server-side language, all code gets executed on the server. By the time the page gets sent to the browser, it has already executed and no further computations can be made. If you must have a PHP solution, the request must be sent to the server, the page must reload.
This is the one of the way to display the output in the same page...
<html>
<head>
</head>
<body>
<form name='test' method='GET' action="">
<input type='radio' name='radiotest' value='1crn' />1crn
<input type='radio' name='radiotest' value='1FFZ' />1FFz
<input type='submit' value='submit' name='submit' />
</form>
<?php
if(isset($_GET("submit")))
{
echo $_GET["radiotest"];
}
?>
</body>
</html>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.