Jump to content

How would you use the php in the same page as the form.


k21chrono

Recommended Posts

Like i want it to have a set of text boxes that the output values would appear in, for instance the submit button would call to a function or something that would generate a random number for each text box, i would love for it to do this without redirecting or refreshing the page if possible, even the littlest suggestions would help, thank you ahead of time.

Like i want it to have a set of text boxes that the output values would appear in, for instance the submit button would call to a function or something that would generate a random number for each text box, i would love for it to do this without redirecting or refreshing the page if possible, even the littlest suggestions would help, thank you ahead of time.

 

Well, there are two things in play - a sticky form to display the numbers on the same page as the form, and attempting to process the form without a refresh.

 

A sticky form would look something like:

 

<?php
   if(isset($_POST['submit']))
   {
      //invoke random number functions
   }
?>

<!DOCTYPE html>
<html>
   <head>
      <title>Random number generator</title>
   </head>

   <body>
      <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
         <input type="text" name="text1" value="<?php if(isset($result1)) { echo $result1; } ?>" />
         <br />
         <input type="text" name="text2" value="<?php if(isset($result2)) { echo $result2; } ?>" />
         <br />
         <br />
         <input type="submit" name="submit" value="Click to calculate random numbers" />
      </form>
   </body>

</html>

 

This will cause the page to refresh itself when the submit button is clicked.  If you want the values to appear without any refresh at all, you'd need to use AJAX.

 

Another option is to forgo PHP completely and use JavaScript only.  But, since you asked this in the PHP Coding section, I figured you wanted to see your PHP options.

 

This will cause the page to refresh itself when the submit button is clicked.  If you want the values to appear without any refresh at all, you'd need to use AJAX.

 

 

Well I am kind of new to this whole thing so I'll have to look up into the whole AJAX thing but if you want you can tell me how, otherwise if i run into trouble I'll post a follow up question, thanks for all the help.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.