Jump to content

Use AJAX or standard from posting?


rv20

Recommended Posts

So if you post a form you can do it the regular way or can use AJAX, i would rather to it the regular way but this results in having to refresh(or redirect depending on how you call it) the page twice, twice because you firstly have to actually post the form, (btw i have php and html on the same page) so posting the form,

 

<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" >

 

Then if there are validation errors like blank fields, invalid email etc..you have to refresh again to clear the $_POST[] variables so something like,

 

if ($_POST['user'] == ""){

<?php echo htmlentities($_SERVER['PHP_SELF']); ?>

}

 

Ok this above exmaple might result in a permanemt loop but with a few tweaks ..........

 

i think PHP_SELF clears the $_POST vars otherwise if you reload the page the $_POST vars are reposted which can result in a variety of problems.

 

Big deal you might say, reposting twice, but, the more you get involved in your own project the more you want it to be as user friendly as possible. I just sense that AJAX is OTT though.

Link to comment
Share on other sites

Ok here is some cut down code, it works ok the only three things are,

 

1) How can hide the ?login=invalidusername  in the url

2) Is this secure

3) If the user doesn't enter a password and gets the ?login=invalidusername url and then refreshes the page how do i remove the ?login=invalidusername and go back to the http://login.php

 

How else would you suggest returning the reponse form the php script if not appending the ?login=invalidusername

 

<?php
  
  if (($_SERVER['REQUEST_METHOD'] == "POST") && (parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST) == "127.0.0.1")) {
     
      $username = $_POST['user'];
      
      if ($username == "") {
          $url_self = htmlentities($_SERVER['PHP_SELF']) . "?login=invalidusername";
          header("location: $url_self");
      }
  }

?>



  <html>
    <head>
    </head>
    <body>
      <form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" >
        <input type="text" name="user"/>
        <input type="submit">
        <input type="button" value="button">
      </form>
      <div id="val_errors" style="width:200px;height:200px;border:solid;">
       <?php
       if ($_GET['login'] == "invalidusername") {
           echo "Please enter a username";
       }       
       ?>
      </div>
    </body>
  </html>

Link to comment
Share on other sites

Try

<?php

if(isset($_POST['submit'])) {
$username = $_POST['user'];
if(!$username || $username == "") {
echo 'invalid username';
}
else
{
//whatever else your script does
}
}
else {?>
<html>
    <head>
    </head>
    <body>
      <form action="" method="post" >
        <input type="text" name="user"/>
        <input type="submit" name="submit" id="submit">
        <input type="button" value="button">
      </form>
<?php }  ?>

 

Don't forget to sanitize your posts. As of now, no, it's not secure.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.