473,748 Members | 2,361 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

JavaScript and PHP var problem

Hi

I'm trying to get a small script working I need to get the user to
input some info from a prompt and then use that info in
a PHP script, but I can't seem to assign the JavaScript var
to a PHP var

<?PHP

if ($do == 'ban') {
print "<SCRIPT language=JavaSc ript>";
print "var reason; ";
print 'reason=prompt( "Please enter the reason for banning this
user:","");';
print "document.write (\"Banning ID $id - \"+reason+\"<br >\");";

/// problem here none of these work

print "document.write (\"$banreason =\"+reason+\"\" );";
print "$banreason = reason";
////
print "</SCRIPT>";
print "outside javascrpt the reason = $banreason<br>" ;
// do PHP SQL here
}

?>

Jul 23 '05 #1
3 2347
Brian wrote:
Hi

I'm trying to get a small script working I need to get the user to
input some info from a prompt and then use that info in
a PHP script, but I can't seem to assign the JavaScript var
to a PHP var
Thats because by the time the prompt has appeared, the PHP script on the
server is finished processing. If you need to get info back to the
server, submit a form, let PHP handle the form. JS and PHP can't
communicate the way you are trying to do it.
<?PHP

if ($do == 'ban') {
print "<SCRIPT language=JavaSc ript>";
print "var reason; ";
print 'reason=prompt( "Please enter the reason for banning this
user:","");';
print "document.write (\"Banning ID $id - \"+reason+\"<br >\");";

/// problem here none of these work

print "document.write (\"$banreason =\"+reason+\"\" );";
print "$banreason = reason";
////
print "</SCRIPT>";
print "outside javascrpt the reason = $banreason<br>" ;
// do PHP SQL here
}

?>

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq
Jul 23 '05 #2
Brian wrote:
Hi

I'm trying to get a small script working I need to get the user to
input some info from a prompt and then use that info in
a PHP script, but I can't seem to assign the JavaScript var
to a PHP var

<?PHP

if ($do == 'ban') {
print "<SCRIPT language=JavaSc ript>";
print "var reason; ";
print 'reason=prompt( "Please enter the reason for banning this
user:","");';
print "document.write (\"Banning ID $id - \"+reason+\"<br >\");";

/// problem here none of these work

print "document.write (\"$banreason =\"+reason+\"\" );";
print "$banreason = reason";
You can not set a server-side variable from client-side JavaScript[1].
PHP reads and parses the code above and generates HTML (which may or
may not include client-side JavaScript). That HTML is sent to the
client to be rendered, ignored, saved to disk, whatever. It does not
matter, by the time the client sees _ANY_ of the client-side
JavaScript you have written, the server has already forgotten about
that request and moved on to other tasks. To say it another way, PHP
is _not_ processed a line at a time with the client and server
interacting in any way. PHP parses the entire file and sends the
result to the client.
////
print "</SCRIPT>";
print "outside javascrpt the reason = $banreason<br>" ;
// do PHP SQL here
}

?>


[1] this is not completely true, there are ways of getting client-side
values to the server, but not the way you are attempting to do, and
not while the page is being parsed by PHP. For example, you could have
something like:

<script type="text/javascript">
function saveInDatabase( someVariable) {
var phpDispatcher = new Image();
phpDispatcher.s rc =
'http://someserver/saveInDatabase. php?theValue=' + someVariable;
}
</script>
<a href="#" onclick="saveIn Database(prompt ('Enter a value to save in
the database'));ret urn false;">Click here</a>

but this concept can _not_ be used to return a value to the server for
use in the same page it is currently parsing.

--
Grant Wagner <gw*****@agrico reunited.com>
comp.lang.javas cript FAQ - http://jibbering.com/faq

Jul 23 '05 #3
Thanks Guys

Time for a re-think :) I feel some cookies coming on :)

Brian
Hi

I'm trying to get a small script working I need to get the user to
input some info from a prompt and then use that info in
a PHP script, but I can't seem to assign the JavaScript var
to a PHP var

<?PHP

if ($do == 'ban') {
print "<SCRIPT language=JavaSc ript>";
print "var reason; ";
print 'reason=prompt( "Please enter the reason for banning this
user:","");';
print "document.write (\"Banning ID $id - \"+reason+\"<br >\");";

/// problem here none of these work

print "document.write (\"$banreason =\"+reason+\"\" );";
print "$banreason = reason";
////
print "</SCRIPT>";
print "outside javascrpt the reason = $banreason<br>" ;
// do PHP SQL here
}

?>

Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
9457
by: Kai Grossjohann | last post by:
It seems that Ctrl-N in Mozilla opens a new empty browser window. That's fine, I don't need to do anything about it. But Ctrl-N in IE appears to clone the current window. Is there a way to intercept the key so that I can do stuff on the server side to make the new window behave correctly? (We have a JSP-based webapp which stores state in the session. Now if two windows access (and modify!) the same session, then madness will result....
53
5737
by: Cardman | last post by:
Greetings, I am trying to solve a problem that has been inflicting my self created Order Forms for a long time, where the problem is that as I cannot reproduce this error myself, then it is difficult to know what is going on. One of these Order Forms you can see here... http://www.cardman.co.uk/orderform.php3
6
2280
by: Alex Rast | last post by:
First of all, this is not a programming question. I'm a user, not programming in JavaScript. I'm not, however, a novice user or even a power user - I certainly know programming intimately as well as computer hardware, configuration, etc... down to the lowest level (e.g. BIOS, assembly language, etc). Anyway, I've obviously got something wrong with JavaScript configuration, files, etc. in Windows 2000, Service Pack 3, because on sites...
136
9434
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their code was littered with document.all and eval, for example, and I wanted to create a practical list of best practices that they could easily put to use. The above URL is version 1.0 (draft) that resulted. IMO, it is not a replacement for the FAQ,...
0
8984
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8823
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9530
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9363
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9312
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8237
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
3300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.