Jump to content

PHP prompt


mehidy1437

Recommended Posts

Hello guys,

Please see below function, so far everything is good.

But when I input Mehidy in input box,

it's echo $name;  it shows Mehidy, which is okay.

But when I echo strlen($name);  it shows 65 i/o 6

It's show the number of characters of $answer = "<script type='text/javascript'> document.write(answer); </script>";

why is it so?

How can I get the real strlen of Mehidy?

Please help.

//prompt function
    function prompt($prompt_msg){
        echo("<script type='text/javascript'> var answer = prompt('".$prompt_msg."'); </script>");

        $answer = "<script type='text/javascript'> document.write(answer); </script>";
        return($answer);
    }

    //program
    $prompt_msg = "Please type your name.";
    $name = prompt($prompt_msg);
	echo $name;
	echo strlen($name);

 

Edited by mehidy1437
Link to comment
Share on other sites

You cannot mix PHP and Javascript like this: PHP runs on the server and has finished running by the time your browser starts to execute any of your Javascript.

If you want the length of the string then you can get that in Javascript easily. What are you actually trying to do with this code?

Link to comment
Share on other sites

1 hour ago, mehidy1437 said:
  return($answer);

Thanks for your response.

$name is containing the string mehidy from the input & it's showing/printing fine.

So, i just want to count the number of character in $name.

If i can use the $name to print the string/input value then it should be possible to count the string length also, am I wrong here? 

Link to comment
Share on other sites

3 hours ago, mehidy1437 said:

$name is containing the string mehidy from the input & it's showing/printing fine.

No, $name contains the Javascript code, which when later evaluated by the browser displays the value you typed in.  Use you're browsers view-source command to see the code generated by your script.

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.