-2

i have a very simple ajax call, just so i can test first:

<script  src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript">
function validateLogIn()
{
  var username  = $("#username").val();     
  console.log(username,password,login,remember);
  //alert('Given data is not correct' + username + "   " + password);
  $.ajax({     
    url: 'validate.html'                         
    type: 'POST', // POST/GET
    data: { 'username' : username } //for now just pass username
    }).done(function(response){  //Attach a succes handler
    alert(response); //my complete code in validate.html
                     // displays in this alert!
    }); 
  }); 
}

 </head>
 <body>
   <form action="crud.html" method="post" name="form_submit" onsubmit="return validateLogIn()">
      <input required placeholder="Username" type="text" name="username" id="username"/>
      <input required placeholder="Password" type="password" name="password" id="password"/>
      <label for="remember">Remember Me:</label>
    <input type="checkbox" name="remember" value="yes" id="remember" />
    <br />
    <br />
    <input type="submit" name="login" value="login" id="login"/>
</form>

in validate.html: none of this gets executed

 <?php
  echo htmlspecialchars($_POST['username'], ENT_QUOTES, 'UTF-8');
  echo $username = $_POST['username']; 
  <script type='text/javascript'>alert('$username');</script>
charlie_cat
  • 1,830
  • 7
  • 44
  • 73
  • 1
    in html, you have php code ????? – Khushboo Feb 04 '15 at 12:51
  • 1
    And you haven't send the values to that html file also. – Khushboo Feb 04 '15 at 12:51
  • 2
    You have *lots* of mistakes here. The PHP is hopelessly wrong and the attempt to use Ajax is hopelessly wrong. Forget about using Ajax for the time being. Get the PHP working with a regular form submission. Then worry about making it work with Ajax later. – Quentin Feb 04 '15 at 12:53
  • yes i have php in html cos i want to test something and yes i am passing values in my form i have a post and a onsubmit which calls my JS, – charlie_cat Feb 04 '15 at 12:54

1 Answers1

1

I don't think you understand AJAX at all. I've made an example below very basic with comments.

var username  = $("#username").val();

$.ajax({     
    url: 'validate.php',  // << You can't have php in html files. 
    type: 'POST', // POST/GET
    data: { 'username' : username } //Data to be send will be in $_POST
  }).done(function(response){  //Attach a succes handler
     alert(response); //Alert the response from validate.php
  }); 
}

PHP FILE:

<?php
echo htmlspecialchars($_POST['username'], ENT_QUOTES, 'UTF-8'); //XSS PREVENTION
Daan
  • 12,099
  • 6
  • 34
  • 51
  • **Danger**: This code is [vulnerable to XSS](https://www.owasp.org/index.php/Cross-site_Scripting_(XSS)). User input needs escaping before being inserted into an HTML document!. (Or you could set the content type of the response so it isn't HTML) – Quentin Feb 04 '15 at 12:55
  • thanks that is how i have tried it but when i do the echo $_POST['username']; line gives me username is undefined, that is why i just wanted to see if i can get to validate.html first, without anything else – charlie_cat Feb 04 '15 at 12:57
  • please see my OP above i made some changes – charlie_cat Feb 04 '15 at 13:23
  • 1
    You still have PHP in a .html file that is not possible. – Daan Feb 04 '15 at 13:27