Jump to content

Recommended Posts

Hello! This bit of code actually does work, I just want to check that this is the best/proper/most efficiant way of doing it. Basically i'm calling a PHP function that uses the javascripts prompt function, then returning the value that the user types in back to PHP. Most probably easier to show you.

 

<?php

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

$output_msg = "Hello there ".$name."!";
echo($output_msg);

?>

 

That is the entire code, I just want to know if this is the proper way to do something like this (using javascript and PHP together) thanks guys!

 

Does that script really prompt you to enter your name (in javascript), and then display "Hello there <name>!", where <name> is the name you typed in?  I don't understand how it could do that.  It definitely doesn't look like the proper way to do it.

Use template system (like Smarty) to do that. From PHP you can assing variable to your template. In your template you can use html, js, css and your application code will be separated from your presentation code.

 

PHP already is a template engine.  Look at any decent MVC framework and how they handle their views.  All Smarty provides is additional overhead and an extra language you have to learn.  You're not gaining anything that you couldn't get with a well-planned PHP-only app.

 

@btherl - It works because prompt() generates both the initial script, which it outputs directly, and the answer script, which is returned to the main code and then output immediately.  In the browser, the output is simply:

 

<script type='text/javascript'> var answer = prompt('Please enter your name'); </script>Hello there<script type='text/javascript'> document.write(answer); </script>

 

@Zephni - no, that's actually a really, really bad way of doing it.  Like bh said, it's ideal to keep the different components as separate as possible.  Your prompt() function is badly thought out as it both echoes directly to the screen as a side effect and returns a value.  And, really, why have the back end create AND process the front end code?  JavaScript is designed to work on the front end.  Keep it there.  If you need data from JavaScript, there are better ways to obtain it.

PHP already is a template engine.  Look at any decent MVC framework and how they handle their views.  All Smarty provides is additional overhead and an extra language you have to learn.  You're not gaining anything that you couldn't get with a well-planned PHP-only app.

 

Yep, youre right. And popular frameworks has template system, yes. Imho its ugly cuz theres <?php ?> tags in your template. Extra language to learn at elementary level is almost nothing. Overhead at now with Smarty 3 (based on PHP 5 - 100% rewrite) also a bit (if you use eg APC its almost nothing to).

But if you use smarty (and you choose the tiny overhead) youve got some extra feature which smarty add to you (block, functions, modifiers...).

 

Ok point taken, PHP should stay back end and not be fiddling around with front end scripting like JavaScript.  I should really search google and i most probably will after I have submitted this but what is the code to prompt with PHP or isn't there. Sorry for the noobishness, I am new to PHP (as you can see). Thanks for your replies guys.

Sorry for double posting but

 

@ph are you saying i should have a seperate file that has the js in that has the prompt code in, i'm getting confused.

 

If you have a small code its worthless. Rather enterprise level worth separate app and business logic. Your application with template system enhance readability, servicing... you can simply change templates (eg. other color schemes...), designers work easily on templates

You should always attempt to keep your components as separate as possible.  There's actually a term for this - separation of concerns.  Your PHP shouldn't be concerned with your JavaScript, and vice versa.  The same goes for JavaScript and HTML - your HTML should be free from inline JavaScript (look up 'unobtrusive JavaScript').

 

It's impossible to keep things 100% separate as the various layers of your app have to communicate with one another (JavaScript may need to manipulate HTML elements, you may need to send some JavaScript data to your PHP script, etc.), but the goal is to do what you can to maximize separation and when you do need to have cross-layer communication, to only create the minimum necessary amount of clear, concise lanes of communication you need.

 

There are many benefits to this.  Your components are centralized, so if there's an issue, you don't need to dive through a bunch of unrelated code to fix it.  If written well, you can swap out entire portions of your app for something else.  It allows you to understand that, in essence, your HTML/CSS/JavaScript is really just a skin laying on top of the actual app.

 

Trying to code with separation of concerns in mind from the beginning is a great habit to get into.  It's how the pros do it, and if you start early with that mentality, you'll be ahead of the game.

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.