473,396 Members | 1,938 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

PHP to JavaScript

Ken
How do I send a variable, $validity from php and use it in JavaScript?

Seem like PHP is written first, then html and JavaScript.

Is it possible?

Ken
Jul 17 '05 #1
5 1920
On Sat, 4 Sep 2004 21:16:41 -0500, "Ken" <kk******@wi.rr.com> wrote:
How do I send a variable, $validity from php and use it in JavaScript?

Seem like PHP is written first, then html and JavaScript.

Is it possible?

Ken

They're 2 separate technologies. PHP is server-side and Javascript,
client-side. One option maybe to write the JS with PHP:
<?php
$phpFoo = 'bar';

echo '<script type="text/javascript">';
echo " alert('$phpFoo');";
echo '</script>';
?>
Haven't tried this personally, but I don't know of any other method that
might work.
HTH =)

Regards,

Ian

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/
Jul 17 '05 #2
"Ken" wrote:
How do I send a variable, $validity from php and use it in JavaScript?
Seem like PHP is written first, then html and JavaScript.

Is it possible?

Ken


Basically php is creating an html/javascript page, so you just use php
to write out the javascript code based on the logic of your php code.
Hope that answers.

--
http://www.dbForumz.com/ This article was posted by author's request
Articles individually checked for conformance to usenet standards
Topic URL: http://www.dbForumz.com/PHP-JavaScri...ict146575.html
Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=490656
Jul 17 '05 #3
"Ken" <kk******@wi.rr.com> wrote in message
news:wF******************@twister.rdc-kc.rr.com...
How do I send a variable, $validity from php and use it in JavaScript?

Seem like PHP is written first, then html and JavaScript.

Is it possible?

Ken


I guess that would depend on how you want to use it in JS. One way to send
it:

<?php
$validity = 1;
print <<<BLOCK
<html><head>
<script language="javascript">
function foo(validity) {
if (validity) {
// do something
}
}
</script></head>

<body onLoad="foo($validity)"></body></html>
BLOCK;
?>

A common way of sending a variable from JS back to PHP is through URL's. Ex:

<script language="javascript">
document.write("<img src='logstats.php?referer=" + referer + "'>");
</script>

HTH

- JP
Jul 17 '05 #4
Ken
This is what I am trying to do. There may be an easier way.

1. Check to see picture is an acceptable image.
a. Check image, picture1, for extension jpg. There are actually 10
images and 5 extensions. But I just listed 1.
b. If the extension in not jpg, change background color to yellow.
c. This works well in
JavaScript --document.getElementById('pt1').style.backgroundCol or='#FFF9B9';
..
d. To test this , try test.php with a *.gif and *.jpg

2. If the file name is acceptable, check to see if the file size is less
than FILE_SIZE_MAX. I noticed, if the file size is greater than
FILE_SIZE_MAX, the program changes the file size to 0.
a. Create an error variable
b. Transfer the error variable to JavaScript
c. Use JavaScript to change the background to yellow.
d. Rewrite the original file name in the value for the input statement.

I created the following two files to experiment with the concept.

I cannot get the file > max to turn the background to yellow.

What am I doing wrong? I am somewhat new at PHP.

Thanks for the suggestions.

Ken

test.php

<?php
//error_reporting(E_ALL);
session_start();
?>
<html><head><title></title>
<script language="javascript">
function validate_pictures() {
document.getElementById('pt1').style.backgroundCol or='FFFFFF';
var validity1 = "true";
var file_name = document.picture.picture1.value;
extension = file_name.split(".");
var ext1 = extension[1];
if (ext1 == 'jpg') {
//alert("jpg");
}
else {
document.getElementById('pt1').style.backgroundCol or='#FFF9B9';
validity1 = "false";
}
if (validity1 == "true") {//alert("submit");
picture.submit();
}
else {
//alert("correct error");
window.location.href="#top-picture";
} }

if ($error == "false") {alert("error-php");}

</script>
</head>
<body>
<a name="top-picture" </a>
<form enctype="multipart/form-data" name="picture" method="post"
action="test2.php">
<input type=file size=65 name="picture1" value="<?PHP
$_SESSION['picture_name1']; ?>" Id='pt1'>

<table align="center" cellspacing="5" cellpadding="1" border="0"
width="100">
<tr><td><input type="button" value="Review Ad"
onClick="validate_pictures()"></td></tr>
</table>
</form></body>
</html>
---------------------------------------
test2.php

<?PHP
error_reporting(E_ALL);
session_start();
//echo "start";
$_SESSION['picture_size1'] = $_FILES['picture1']['size'];
$_SESSION['picture_name1'] = $_FILES['picture1']['name'];
// echo $_SESSION['picture_name1'];
$_SESSION['valid_size1'] = "true";
if (($_SESSION['picture_size1'] == 0) && ($_SESSION['picture_name1'] !=
"")) {
$_SESSION['valid_size1'] = "false";
$error = $_SESSION['valid_size1'];
echo "File greater than max";
// header("Location: test.php");
exit();
}
?>
Jul 17 '05 #5
I noticed that Message-ID: <qx******************@twister.rdc-kc.rr.com>
from Ken contained the following:
1. Check to see picture is an acceptable image.
a. Check image, picture1, for extension jpg. There are actually 10
images and 5 extensions. But I just listed 1.
b. If the extension in not jpg, change background color to yellow.
c. This works well in
JavaScript --document.getElementById('pt1').style.backgroundCol or='#FFF9B9';


There is no need to do this.

Because the PHP is processed by the server you can simply change the
background colour using CSS. Simply use a PHP snippet to write a
background style in the body tag. No need for JS at all.

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #6

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

Similar topics

0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
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...

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.