Jump to content

[SOLVED] javascript function problem


noobstar

Recommended Posts

Hi, im was fiddling with the prompt to try and somehow when a user clicks on a button would ask for his/her name and then display it however, it didn't work out but it did work this way without a function:

 

<script language="JavaScript">

var name = prompt('hello', '');

</script>

 

<?php
$hello = "<script language=JavaScript>document.write(name);</script>";
echo "$hello<br/>";
?>

 

 

 

My question is how could i make this work when someone clicks a button instead of it prompting the user when ever he/she loads the page?

 

I tried this but I am sure its something to do with the variable not being passed because its only used within the function and its not usable for some reason outside it. How could i go about making the variable available for any outside functions or other scripts ?

 

<script language="JavaScript">

function prompter(name)
{
name = prompt('hello', '');
}

</script>

 

<?php
$hello = "<script language=JavaScript>document.write(name);</script>";
echo "$hello<br/>";
?>

 

<input type="button" onClick="prompter();">

Link to comment
https://forums.phpfreaks.com/topic/71461-solved-javascript-function-problem/
Share on other sites

set an area to receive the name from the prompt

 

so where ever (one location) you want the name to show up put this

 

<span id="prompt_name"></span>

 

in your prompt function do

 

var name = prompt('Text', '');

document.getElementById('prompt_name').innerHTML = name;

Archived

This topic is now archived and is closed to further replies.

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