/    Sign up×
Community /Pin to ProfileBookmark

php to html variables ?

I’m just now getting a small grasp on PHP and mySQL.
I have already written a working program in Javascript, I would like to try to convert all of my Javascript into PHP. All of my Pricing information is currently stored in arrays in Javascript. I plan to store all of my pricing information in mySQL and retrieve it from there.
Is it possible to retrieve from the database and put the contents into an existing variable that does not have the “$”?
This would save me a lot of work going through all of my variables in the HTML part of the code.

example:

current:

<script type=”text/javascript” language=”JavaScript”>
labor_price=new Array()
labor_price[0]=.50 // layout
labor_price[1]=.30 // top track
labor_price[2]=.10 // bottom track
</script>

proposed:

(database)
code, description, price
0 layout .50
1 toptrack .30
2 bottomtrack .10

PHP script:

<?php
// … log in ect …

$result = mysql_query($sql, $conn) or die(mysql_error());
while ($newArray = mysql_fetch_array($result)) {

$price = $newArray[‘price’];
labor_price[$code]=$price;

;
}
?>

….

I know this code don’t work, but I’m sure you get the idea what I’m trying to do. Any suggestions or recommendations?

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@ExuroOct 13.2004 — All you have to do is echo out the variable values from the PHP code, like so:
[code=php]<script type="text/javascript">
<!--
<?php
// ... log in ect ...

$result = mysql_query($sql, $conn) or die(mysql_error());
$i=0;
while ($newArray = mysql_fetch_array($result)) {
$price = $newArray['price'];
echo "labor_price[$i]=$price;n";
$i++;
}
?>
//-->
</script>
[/code]
Copy linkTweet thisAlerts:
@anothenauthorOct 13.2004 — Yes, I know that will work, but that code requires Javascript.

Isn't there a way to preserve a variable contents from PHP to HTML?

Here is a small snippit of my code to give you an idea what I have:

[code=php]<HTML><HEAD>
<script type="text/javascript" language="JavaScript">

// Labor Rates: labor_price
labor_price=new Array()
labor_price[4]=.11 // rock bottoms


function CalculateTotals(){
f=document.wall_calculator;
sqft_drywall_temporary = f.wall_length.value * f.wall_height.value ;
f.total_square_foot.value = sqft_drywall_temporary ;

labor_price_temporary = f.total_square_foot.value * labor_price[4] ;
f.labor_price.value = labor_price_temporary ;

;}

</script></HEAD>

<BODY onLoad="document.wall_calculator.wall_length.focus()" bgcolor="FFF8DC" TEXT="000000" LINK="9932CC" VLINK="A52A2A">
<form NAME="wall_calculator" method="post" ACTION="wallcalc_pg2.php">

Wall length:<input NAME="wall_length" value="1" size="3" onfocus="this.select()" OnBlur="CalculateTotals()">
Stud Height:<select NAME="wall_height" onChange="CalculateTotals()"><option value="10" selected> 10' </option><option value="12"> 12' </option><option value="14"> 14' </option><option value="16"> 16' </option><option value="18"> 18' </option></select>
Square Foot:<input NAME="total_square_foot" size="7" OnFocus="document.wall_calculator.wall_length.select(); document.wall_calculator.wall_length.focus();">
Labor Price:<input NAME="labor_price" size="7" OnFocus="document.wall_calculator.wall_length.select(); document.wall_calculator.wall_length.focus();">

</form></body></html>[/code]


Now my goal is to remove the Javascript. I know I will need to make some PHP function calls, that I can do.

What I want to do is take my "Labor Rates" array and store/retrieve the contents in mySQL. I can do that also. Where I have the problem is in variables. All of my variables that are in the function "CalculateTotals" are from Javascript. If I convert to PHP all of my variables have the "$". All of my HTML variables do not.

Isn't there a way to preserve the contents of the variables for use in HTML?

What I like about my code is that it will calculate and display results imediately. If it's not do-able in PHP, then I'll just rewrite the code. It seems though, the only drawback is the variables.
Copy linkTweet thisAlerts:
@ExuroOct 13.2004 — You can't access any PHP code client-side. Everything is processed by the server, and by the time it gets to the client there's no trace left. If you want to access the info the user enters you have to submit it via a form or something...
Copy linkTweet thisAlerts:
@anothenauthorOct 13.2004 — Thanks Exuro,

I thought it might not be do-able,

Thats okay. I was wanting to spend some time learning PHP anyway. I'll keep my Javascript code and make new code in PHP as an option. This way my users can choose between the two.

I'm really liking PHP. It seems a bit more simple than I expected.
×

Success!

Help @anothen spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.21,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...