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
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;

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.