Click here to Skip to main content
15,914,444 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am using php header in javascript but it is not working. this is my code, kindly see what i did wrong

XML
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
    var n = document.getElementById('id').value;
    var p = document.getElementById('pw').value;
    var queryString = "?emailID=" + n+"&pw="+p;
  $.ajax({url:"ajax-example.php"+queryString, success:function(result){
   if(result==1)
   {
       $("div").html("correct");
       header('Location: http://www.hotmail.com');

   }
    else
        $("div").html(result);

  }});
});});
</script>
</head>

<body>
<div> </div>
Username : <input type="text" name="id" id="id">
Password: <input type="password" name="pw" id = "pw">
<button>Authenticate</button>
</body>
</html>
Posted
Updated 3-Apr-12 9:48am
v2
Comments
ZurdoDev 3-Apr-12 15:35pm    
What does "is not working" mean? Have you debugged it?
Sweety Khan 3-Apr-12 15:47pm    
php header not working, everything else is fine. when correct emailid and password enter by user, www.hotmail.com should open but it doesnt open.
Sergey Alexandrovich Kryukov 3-Apr-12 16:53pm    
Where is the "PHP header" in your code?
Anyway, the question does not seem to make sense as PHP works on server side, and JavaScript -- on client side.
--SA

No, the PHP header function does not work in Javascript (and it doesn't make a lot of sense trying to use it from Javascript).

I think what you really want to do is a redirect from Javascript. You can use something like this:
JavaScript
location = 'http://www.hotmail.com/';
 
Share this answer
 
Comments
Sweety Khan 4-Apr-12 7:20am    
Thanks for understanding what exactly i was asking. After realizing that php is serverside language and js is clientside my question really dont make sense, i should ask what can be use in js to redirect the page like header in php . Thanks a lOt :)
Please see my comment to the question. The line
PHP
header('Location: http://www.hotmail.com');
makes no sense in principle, because this is PHP, something which should generate header on the server side, before HTML or JavaScript, or any other content appears in the HTTP response on the client side. From PHP standpoint, JavaScript is merely a piece of data to be written in response to HTTP request, some content. Even if you put this line in <?php … ?>, it still wouldn't make any sense. This is not something which could be somehow "fixed"; it looks like you don't understand generally how server side and client side work.

Also, even the jQuery part of the code is bad: using document.getElementById defeats the purpose of jQuery, where you can use selectors instead; please see:
http://api.jquery.com/category/selectors/[^],
http://api.jquery.com/id-selector/[^].

It looks like you don't understand what are you writing. Do your try to write something based on some available code samples? Normally, it does not work like that; you need to understand each like you are writing.

—SA
 
Share this answer
 
v3
Comments
Sweety Khan 4-Apr-12 7:34am    
yeh u right using php header in javascript is senseless. i post the question beacuse i wanted to know something like php header that can be used in js which is now told by Graham Breach. yeh i take a sample code and edit it for the work i want to do but i know each and every part. whats wrong in using document.getElementById? i saw the links, there is clearly written "jQuery uses the JavaScript function document.getElementById(), which is extremely efficient" so i dont understand why u r saying using it is bad
Sergey Alexandrovich Kryukov 5-Apr-12 2:42am    
document.getElementById is not wrong per se, it defeats the purpose of jQuery where it is already implemented in descriptors, for your convenience. It's like using C++ without classes and virtual methods.
--SA
Sweety Khan 10-Apr-12 12:10pm    
now i understand after searching tht u mean i should code this
var n = $('#email').val();
var p = $('#pw').val();
right?
Sergey Alexandrovich Kryukov 12-Apr-12 12:41pm    
Well, this is correct if you want to get element's value:
http://api.jquery.com/val/
I don't know how is it related to your original question though...
--SA
Sweety Khan 12-Apr-12 12:52pm    
not related to my question but u told me a new thing which i dont know.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900