Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

JelvisChan

macrumors member
Original poster
Jan 9, 2009
77
0
Hello, all.

PHP SCRIPTING HELP, HOPEFULLY SIMPLE...
Happy New Years to everyone! I have a question though, if one of you could share your PHP scripting knowledge to help me figure this one out.

I don't know how hard this might be, but this is what I am trying to do:

So I have a php file called js.php.
I want to make it so that I call a javascript prompt and it asks me "What is your name?".

How do I make it so that when it asks me the question, I type in Jeff, and then PHP echos the results. I know how to do this in a javascript way - document.write...

But how do I call the function and display the answer in the PHP File?

This is very important right now, and this is what I have so far:

PHP:
<?php
$prompt = "<script language=\"JavaScript\">\n
 prompt(\"What is your name?\");\n
 </script>"; 

echo $prompt;


?>

This calls that prompt, but how do you display the answer?

Thanks,

Jeff
 
If you don't mind some being pretty blunt (harsh, even?), read this:

http://www.eggheadcafe.com/PrintSearchContent.asp?LINKID=569

Basically, you are talking about mixing client-side (JS) and server-side (aka, back-end) PHP code.

That being said, there are multiple ways to handle this, but unless you use AJAX, it will require a refresh.

If you explain what you are looking to accomplish I would be happy to provide more info/suggestions. Have fun!
 
PHP is processed before the page is displayed, not after the page has loaded. It's the fundamental difference between a server side language and a client side language. If you want to store something like a name for a later page, you can store information in a cookie, which and be created and read from both PHP and JavaScript.
 
Ok, great!
This is what I am trying to do:

I have a message board, and I am trying to make it so that when you are creating your topic, you can choose a color to make your text by clicking the font color link.
So as you are writing your topic and you want to make something red, you just click on the font color link and a javascript PROMPT BOX comes down and asks you what color you want it to be. If you say red, then php will put red as the font color and it will be shown as red.

I got the more simple tags to work ( Italics, Bold, and Underline ), and I am able to put in a direct color code with being prompted, but I do not know how to have the user manually type in a desired color.

This function is called styled text in this script.
If you don't understand, here is part of the code to make it happen:

PHP:
function styledText($strText)
{
$strText = preg_replace("/\[B\](.*?)\[\/B\]/i","<B>$1</B>",$strText);
$strText = preg_replace("/\[I\](.*?)\[\/I\]/i","<I>$1</I>",$strText);
$strText = preg_replace("/\[U\](.*?)\[\/U\]/i","<U>$1</U>",$strText);
[COLOR="Red"]$strText = preg_replace("/\[FONT\](.*?)\[\/FONT\]/i","<script language=\"JavaScript\">\n
 var name=prompt(\"Color:\");\n
document.write(name);>$1</Script>",$strText);[/COLOR]
return($strText);
}

The red part is confusing me.

Also, here is the javascript code behind it all:

Code:
function insertspecial(tag) {
var space=" ";
var text=prompt("Type text you wish to enter:","");
if (text != null)
        {
        var text_to_insert = space+'['+tag+']'+text+'[/'+tag+']'+space;
        insertAtCursor(document.form.message, text_to_insert);
        }
document.form.message.focus();
}

So I am trying to make php define the color by the user's request.

Maybe something like:
<font color=\"<?php echo $prompt; ?>\"></font>

But how would I go about doing that?

Sorry if this is confusing...
Please do what you can to help me.

Jeff
 
So you're essentially wanting to have a more graphical textarea for the user? There's a few options that you can implement rather than trying to create something on your own that won't work as well. PHP isn't the right tool for what you're trying to do.

Examples:
 
hmm. Ever thought about using a javascript library like JQuery? Might make your life easier.

You could essentially do this:

response.php:

PHP:
function styledText($strText) 
{ 
$strText = preg_replace("/\[B\](.*?)\[\/B\]/i","<B>$1</B>",$strText); 
$strText = preg_replace("/\[I\](.*?)\[\/I\]/i","<I>$1</I>",$strText); 
$strText = preg_replace("/\[U\](.*?)\[\/U\]/i","<U>$1</U>",$strText);
return($strText); 
}
$yourText = $_POST['format'];
styledText($yourText);

post.php:

Include AJAX code to send a POST message to response.php, and set the value of the text area to the response. You can do it every few seconds?

Just an idea. I don't think it is the most practical approach.
 
PHP:
$strText = preg_replace("/\[FONT\](.*?)\[\/FONT\]/i","<script language=\"JavaScript\">\n
 var name=prompt(\"Color:\");\n
document.write(name);>$1</Script>",$strText);[/COLOR]
return($strText);
}

I *know* that you're not actually going to use a FONT tag right? Because that tag is evil.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.