Jump to content

Forms on the same page?


Jragon

Recommended Posts

Hey,

 

I was wondering how i could make a form that when submited it would stay on the same page but skip to the php bit#

 

Idea:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>What would you like to be said over and over agien?</title>
</head>

<body>
IF DATA ALLL READY EXITS DONT EXACUTE
  <h1>What would you like to be said over and over agien?</h1>
  <h6>By Rory Anderson!</h6>
  <form action="" method="post">
   <h2>What would you like to be said:</h2>
   <br />
   <input type="text" name="text"/>
   <br />
   <h2>How many times would you like it to be said:</h2>
   <br />
   <input type="text" name="times"/>
   <br />
   <input type="submit" value="say!"/>
  </form>
IF DATA DOSE NOT EXIST DONT EXACUT
<?php
for($i=0; $i >= $times; $i++){
    echo $text;
}
?>
</body>
</html>

 

Thanks

 

Jragon

Link to comment
https://forums.phpfreaks.com/topic/208296-forms-on-the-same-page/
Share on other sites

You might want to try setting it so that the form action is to submit the same page, which you can do with PHP_SELF:

 

<form method="post" action="<?php echo $PHP_SELF;?>">

 

Then you want to make it so that the page has a set of instructions as to what it should do IF it sees that the submit button has been pressed, which means you need an if clause a little like this...

 

if(isset($_POST['submit']))
{ whatever }
else { whatever else}

 

... with {whatever} being whatever you want to happen if the submit button HAS been pressed, and {whatever else} being whatever you want to appear if it hasn't.

 

I hope that helps you and is what you were after! :)

You might want to try setting it so that the form action is to submit the same page, which you can do with PHP_SELF:

 

<form method="post" action="<?php echo $PHP_SELF;?>">

 

Then you want to make it so that the page has a set of instructions as to what it should do IF it sees that the submit button has been pressed, which means you need an if clause a little like this...

 

if(isset($_POST['submit']))
{ whatever }
else { whatever else}

 

... with {whatever} being whatever you want to happen if the submit button HAS been pressed, and {whatever else} being whatever you want to appear if it hasn't.

 

I hope that helps you and is what you were after! :)

 

You do NOT want to use action=<?php $_SERVER['PHP_SELF'] ?> It can be exploited with XSS attacks. To submit a form to itself, either use action="" or explicitly name the script in the action= attribute.

Also, since your submit button has no name= attribute, if( isset$_POST['submit']) ) { will not work. The best way to check for form submission is to use a hidden field like

<input type="hidden" name="submitted" value="yes">

then check for it to see if the form has been submitted: if( $_POST['submitted'] == 'yes' ) { This is to pander to the shortcomings of some versions of Internet Exploder that don't handle submit buttons properly when the enter key is used to submit the form.

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.