Jump to content

HELP!!! My php script is not inserting variable values into my string???


khopson

Recommended Posts

I am at my wits end....I'm really trying to learn this stuff....but here is what's happening.

$yr = "$_POST['year']";
$month = "$_POST['month']";
$day = "$_POST['day']";

$week_str= "&weeknum=10&year=$yr&month=$month&day=$day&";

echo "$week_str"

I am using Flash client which calls this service with the year, month and day args.  I am using
LoadVars() with the POST method.  Here is what is returned to flash from my echo statement!

weeknum = 10
Year = $yr
Month = $month
Day = $day

So then I try to format it like this.....
$week_str= "&weeknum=10&year=" .$yr. "&month=" .$month. "&day=" .$day. "&";

Now the return  looks like this....
weeknum = 10
Year = " .$yr. "
Month = " .$month. "
Day = " .$day. "

I have tried everything I can think of.... Please...can anyone save my sanity here??

Thanks in advance!!!
Link to comment
Share on other sites

Are you sure you are writing

$week_str= "&weeknum=10&year=$yr&month=$month&day=$day&";

And not

$week_str= '&weeknum=10&year=$yr&month=$month&day=$day&';

Notice the different quotes.  Double quotes allow you to add variables in the string.  Single quotes treat the string literally, and do not allow variables.
Link to comment
Share on other sites

Absolutely, Positively SURE!!!

I tried doing double quotes and putting curl brackets around variables, single quotes inside of double quotes...everything I could think of to tell the interpreter that it is supposed to be a variable not a literal.....I'm out of ideas....
Link to comment
Share on other sites

OK...I'm so frustrated I'm probably not communicating very well....sorry..

I have a flash program that calls a php service file using LoadVars() it goes like this...
function getWeek(year,month,day) {
fpath = "http://localhost:8500/Services/getWeekNum.php"
var week_lv:LoadVars = new LoadVars();
week_lv.year = year;
trace("LV year = "+week_lv.year);
week_lv.month = month;
week_lv.day = day;
var reply_lv:LoadVars = new LoadVars();
reply_lv.onLoad = function(ok){
trace("OK = "+ok);
if(ok){
trace("weeknum = "+reply_lv.weeknum);
trace("Year = "+reply_lv.year);
trace("Month = "+reply_lv.month);
trace("Day = "+reply_lv.day);
}
else trace("error occurred: "+this.message);
}

week_lv.sendAndLoad(fpath,reply_lv,"POST");

Now here's my php script.....  in its entirety....Before you guys laugh too hard...I'm just a beginner so go easy on me.....

<?php


if (!isset($_POST['year'])){
  echo "Error=Cannot process without a given year&";
die();
}
if (!isset($_POST['month'])) {
    echo "Error=Cannot process without a given month&";
die();
}
if (!isset($_POST['day'])){
    echo "Error=Cannot process without a given day&";
die();
}
// Everything is here to start figuring the weeknumber

$yr = "$_POST['year']";
$month = "$_POST['month']";
$day = "$_POST['day']";

$week_str= "&weeknum=10&year=" .$yr. "&month=" .$month. "&day=" .$day. "&";

echo "$week_str"
?>


Oh...I hope you guys can tell me what I'm doing wrong...I've been pounding this out for days...I've uninstalled and reinstalled php a million times....my web server a hundred times....well...you get the picture...
Link to comment
Share on other sites

Thanks Ken....    Here's the change I made:

$yr = $_POST['year'];
$month = $_POST['month'];
$day = $_POST['day'];

$week_str= "&weeknum=12&year=$yr&month=$month&day=$day&";

Here's the output to Flash:
weeknum = 12
Year = $yr
Month = $month
Day = $day

Am I missing something in the string formatting? 


Link to comment
Share on other sites

I don't deal with flash, so I'm not familiar with debugging techniques used when interfacing with flash. If you echo something from the php script will it appear on the screen?

If so, put this line at the start of your script:
[code]<?php
echo '<pre>' . print_r($_POST,true) . '</pre>';
?>[/code]

This will dump to the screen the values the script is recieving in the $_POST arrary from the flash program. That may give us a hint as to the problem.

Ken
Link to comment
Share on other sites

I copied and pasted your php, and it's fine.. the problem must be elsewhere.  Though there is a small chance that something is odd with your php interpreter.

You could make a simple form to test with:

[code]<form action="http://localhost:8500/Services/getWeekNum.php" method=post>
<input type=text name=year value=1066>
<input type=text name=month value=1>
<input type=text name=day value=1>
<input type=submit name=submit>
</form>[/code]
Link to comment
Share on other sites

If you're just trying to find the week number, why not just use PHP's date function?

http://us3.php.net/date

$weekNum = date("W");

You can then do whatever you like with that value--echo it to the browser or send to flash.  I don't use flash either.
Link to comment
Share on other sites

Gee...thanks for the advice on finding the week number....I hadn't gotten that far, yet...but was wondering how I was going to tackle that one?!  I'm quite impressed if php has a function that will calculate that beast for me!!

Anyway...I copied the simple form program you gave me, ran it...it ran fine, after clicking submit, the page came up absolutely blank.  Nothing echoed!

In fact, it came up blank running it through Dreamweaver.  When I tried typing the address to the little form program directly into the browser like this:  http://www.localhost/test.html  I get "Cannot find Server" error message. (IE6)  So I tried qualifying it a little better with http://localhost:8500/test.html
still received same error message.....as ND(Napolean Dynamite) would say..."GOSH!..DANG!!!

I checked my services, even did a restart on the web server, but still errored when typing the address into the browser. 

I'm going from bad to worse here aren't I??? 


Link to comment
Share on other sites

OK....I'm really getting tired...need sleep....

I ran the test script in Firefox rather than IE6, and it ran ok....  I put Ken's suggestion in to echo the variables as follows:  echo '<pre>' . print_r($_POST,true) . '</pre>';  I put this line in right after the <?>php tag.

Here is what spewed out on the browser screen:

' . print_r($_POST,true) . ''; if (!isset($_POST['year'])){ echo "Cannot process without a given Year!"; die(); } if (!isset($_POST['month'])) { echo "Cannot process without a given Month!"; die(); } if (!isset($_POST['day'])){ echo "Cannot process without a given Day!"; die(); } // Everything is here to start figuring the weeknumber $yr = $_POST['year']; $month = $_POST['month']; $day = $_POST['day']; $week_str= "&weeknum=13&year=$yr&month=$month&day=$day&"; echo $week_str ?>


Is it the php interpreter that is choking?  (I have installed PHP 5).  It seems to see the echo statement and then just spew everything after it until there is no more to spew???  Doesn't seem to pay attention to the quotes, or anything???


If I installed php 4.x  Can I still get the date("W")???
Link to comment
Share on other sites

I hope you mean the <?php tag, not the <?>php tag :)

I think it's very unlikely that the problem is with the interpreter.  That output there looks like what might happen if you accidentally left php mode and returned to html mode, with a '?>' for example.

Did you include the '?>' from ken's example?  If so, take it out and try again.

Link to comment
Share on other sites

OH....I just found the # sign you told me about earlier....cool!!  (OK...I'll admit it I really am blonde!!)

Here is the exact code in the php script.  The tags look ok to me?  The interpreter sees the
beginning tag, then the word "echo" then the '<pre>' tag.  It echos alright, starting with the last single quote on the '<pre>' tag and all the way to the end of the document.  It even echos the [quote]?>[/quote]?>  tag. 

Is there a chance that it is being handled by the html and not even invoking the php interpreter? Or does the fact that it doesn't print the first[quote]<?php tag [/quote] and the first[quote]echo[/quote] mean that the interpreter was invoked but just didn't finish the job properly?


[code]<?php
echo '<pre>' . print_r($_POST,true) . '</pre>';

if (!isset($_POST['year'])){
    echo "Cannot process without a given Year!";
die();
}
if (!isset($_POST['month'])) {
    echo "Cannot process without a given Month!";
die();
}
if (!isset($_POST['day'])){
    echo "Cannot process without a given Day!";
die();
}
// Everything is here to start figuring the weeknumber

$yr = $_POST['year'];
$month = $_POST['month'];
$day = $_POST['day'];

$week_str= "&weeknum=13&year=$yr&month=$month&day=$day&";

echo $week_str
?>[/code]

Here's the output on the browser, again, I am using the form script supplied by [quote]btherl[/quote] to test.  And also using the line provided by[quote]Ken[/quote] to echo the args coming into the script.

' . print_r($_POST,true) . ''; if (!isset($_POST['year'])){ echo "Cannot process without a given Year!"; die(); } if (!isset($_POST['month'])) { echo "Cannot process without a given Month!"; die(); } if (!isset($_POST['day'])){ echo "Cannot process without a given Day!"; die(); } // Everything is here to start figuring the weeknumber $yr = $_POST['year']; $month = $_POST['month']; $day = $_POST['day']; $week_str= "&weeknum=13&year=$yr&month=$month&day=$day&"; echo $week_str ?>

HuH!!!  Coding is not for wimps!!  It takes every bit of strength to keep from throwing this computer out the window!!  (sorry...lost it for a minute).
Link to comment
Share on other sites

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.