2

I want when a user enter 'me' from the prompt text box it will be direct to config.php else print "you don't have access". Below is the code i have so far but is not calling the prompt. Need help on how on overcome the issue. thanks in advance.

<?php 
if(isset($insert)){ 
  echo "<script>"; 
  echo $ms="prompt('Please enter your name', 'Enter name here');"; 
    if($ms=="me"){ header("Location: config.php"); 
    } else { 
  echo "you dont have access"; } 
  echo "</script>"; }
?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
dyahmed
  • 43
  • 7
  • 1
    *"Below is the code i have so far"* - there is no code. – Funk Forty Niner May 22 '15 at 03:11
  • `"; echo $ms="prompt('Please enter your name', 'Enter name here');"; if($ms=="me"){ header("Location: config.php"); } else { echo "you dont have access"; } echo ""; }?> ` – dyahmed May 22 '15 at 03:11
  • please put that in your question and not in comments, then delete it from the comments area, thank you – Funk Forty Niner May 22 '15 at 03:12
  • 1
    This will never work -- PHP cannot access JavaScript variables. They exist in two different environments: PHP on server, and JavaScript on the browser. You can however use an AJAX call to a web service running a PHP script that evaluates user input and returns some kind of data to the browser that triggers events. – SeanOlson May 22 '15 at 03:16
  • PHP runs on the server BEFORE the web page is sent to the browser. You cannot run embedded PHP from a user action in the browser. You can use Javascript to insert DOM elements in the page. Login will need to do an ajax call or a form submit to the server to verify the credentials. – jfriend00 May 22 '15 at 03:17
  • here, see this answer http://stackoverflow.com/a/2618884/ base yourself on that. – Funk Forty Niner May 22 '15 at 03:20
  • thanks. i understand that php cannot access javascript variable so what is the way out to solve the problem. – dyahmed May 22 '15 at 03:22
  • ok Fred -ii- i will check on the link. thanks – dyahmed May 22 '15 at 03:24
  • also see this Q&A with a few answers http://stackoverflow.com/q/26209967/ – Funk Forty Niner May 22 '15 at 03:30

2 Answers2

0

You can't mix PHP and JavaScript like that. The PHP code is processed first, and then the HTML and JavaScript is sent to the browser. Once this has happened, there is no way to go back to the PHP script until the next full round trip to the server.

You would have to create a login form on your page, for example like this:

<form method="POST">
    <input type="text" name="input" />
    <input type="submit" />
</form>

Once the user has filled it in and sent it off, you can work with the user's input:

<?
if($_POST['input'] == "me") {
    // user is authenticated
} else {
    // or not
}
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
TimoStaudinger
  • 41,396
  • 16
  • 88
  • 94
  • sure you can http://stackoverflow.com/a/2618884/ - well, just not like "that" ;-) – Funk Forty Niner May 22 '15 at 03:21
  • @Fred-ii- Yes, sure you can generate an alert depending on a condition in your PHP script, but you cannot access the value from a `prompt()` as OP suggested -- or am I missing something here? – TimoStaudinger May 22 '15 at 03:26
  • @Fred-ii-, what OP is trying to do is not possible, he is trying to assign javascript prompt variable to PHP variable :) – Sourabh Kumar Sharma May 22 '15 at 03:26
  • 1
    no, you're right, that's why I said they can't do it the way they're doing it now. The link I gave them is a starting point. Serverside is better though. – Funk Forty Niner May 22 '15 at 03:26
  • There is different between the two php perform its function first from the server while javascript is send to the browser after php function. – dyahmed Oct 19 '15 at 10:14
0

What you are doing is wrong you cannot assign a javascript prompt value to a php variable. You need collect the value from the prompt then use form or ajax to send the value to server side and then show the message to the user.

Sourabh Kumar Sharma
  • 2,864
  • 3
  • 25
  • 33