homepage Welcome to WebmasterWorld Guest from 54.224.202.109
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Passing form array variables to processing page
Difference in php versions broke code
mfleck




msg:4122973
 4:24 pm on Apr 27, 2010 (gmt 0)

Greetings,

The enclosed code is part of an online test which worked perfectly on an older version of php but does not work since my host updated their server. This section of code uses a loop to display test questions and possible answers and finally a submit button.

What breaks the code is that the processing page now requires $_POST, and array variable values are not coming through to the processing page. Any ideas what I can do to this code to make it work again?

Thanks,

Mike

$qcount=1; $count2=1;
#### Now The Questions
while($qcount<16)
{
echo "<tr><td class='red1b'>",$qcount,".&nbsp;</td><td class='red1b'>", $q[$qcount][0], "</td></tr>";
while ($count2<5)
{
if($q[$qcount][$count2]<>"&nbsp;")
{
$abc=$q[$qcount][$count2];
echo "<tr><td align='center'><input type='radio' name=a[$qcount] value='$count2'></td><td class='blu1'>",$q[$qcount][$count2],"</td></tr>";

}
$count2++;
}
echo "<tr><td colspan='2'>&nbsp;</td></tr>";
$qcount++;
if($count2==5) { $count2=1; }
}

#### Now do the submit button
echo "<tr><td align='center' colspan='2'><input class='red2b' type='submit' name='submit' value='Submit'></table></table></form>";
echo "<br><br><br><p align='center' class='blu0'>04/22/2005</p>";
echo "<p align='center'><a class='red1b' href='../../index.htm' target='_top'><<<
Back to Main Website >>></a></p><br><br></body>";

 

ENetArch




msg:4122990
 4:45 pm on Apr 27, 2010 (gmt 0)

Mike,

two things come to mind immediately concerning the array variables ...
1) place the input tag's name value in quotes.
2) make sure that the input tag's name value ends with [].

example
<input name="answer[]" .../>
<input name="answer[]" .../>
<input name="answer[]" .../>

When the form is submitted, the page processing the input can then check the array

for (x=0; x<5; x++)
answer[x] = $_POST ['answer[' . x . ']'];

Mike,

mfleck




msg:4123009
 5:30 pm on Apr 27, 2010 (gmt 0)

I can easily do #1. I toyed with #2, but it made each of the 15 questions into one - selecting a different answer in question 12 for example changed the answer for all questions. I'll keep trying - I must be missing something.

ENetArch




msg:4123038
 5:59 pm on Apr 27, 2010 (gmt 0)

Mike,

If you'd like, post the end result of your form page here, and also post the code snippet of your page that processes the input from the form. Maybe I will see something =)

Mike,

mfleck




msg:4123053
 6:12 pm on Apr 27, 2010 (gmt 0)

I wrote this portion just to see if the variable contains any data after it became apparent that things weren't working:

$a01=$_POST['a[1]']; echo ">",$a01,"<";

die();


The portion of the code that used to work just fine is:


#Place answers into array
$q[1][1]=$a[1];
$q[2][1]=$a[2];
$q[3][1]=$a[3];
$q[4][1]=$a[4];


... the code went on from there to fill an array of 15 answers.

I currently have the display die after showing what the first a value holds, and it shows only >< indicating an empty value.

ENetArch




msg:4123065
 6:25 pm on Apr 27, 2010 (gmt 0)

Mike,

What does the form HTML code look like?

Mike,

mfleck




msg:4123089
 6:40 pm on Apr 27, 2010 (gmt 0)

If you mean the opening statement:

echo "<form name='traffic_test' method='post' action='traffictest2.php'>";

Sorry if that wasn't what you meant. I can't post more than snippets of code because this page is inside a secure section of a police website and their data is considered proprietary and heavily protected. I know that makes it difficult to help me and I can only apologize. If that wasn't what you were looking for, can you advise what portion of the code you need to see - form page or processing page?

ENetArch




msg:4123121
 7:22 pm on Apr 27, 2010 (gmt 0)

Mike,

I just need the input tags as they are generated from your code. You can strip any other data that is extranious to the input tag, tag type, tag name.

Mike

mfleck




msg:4123128
 7:31 pm on Apr 27, 2010 (gmt 0)

The input section is what I first provided (please see the first section of code I provided). That brief section of code displays all 15 questions and their potential answers and allows the user to select the appropriate answer for each question. In each case, the type is radio.

Does that help?

ENetArch




msg:4123133
 7:39 pm on Apr 27, 2010 (gmt 0)

Mike,


eeee, NO! .. I want to see what your code produces. I'm reviewing your code, and I don't think it's generating what the $_POST command needs. so I want to see what you're actually getting.

Please,

Mike,

Matthew1980




msg:4123134
 7:39 pm on Apr 27, 2010 (gmt 0)

Hi there,

Having read the code & posts so far, and sort of understanding your issue, 1 question/thought springs to mind.

If you were using a form before and it worked fine, and now the server has 'upgraded' - requiring you to use the $_POST array, what were you doing before?

I assume as you were using registered_globals - at least that is the only thing that springs to mind..

The first thing you need to do is check that the form data is being sent & received correctly buy the processing file/script, which evidently it has done before, but seeing as this has happened since the 'upgrade' you may need to see how you can improve the 'catch' of data being sent, this may just mean attaching the $_POST array to some vars etc.

Also - forgive me for the assumption but the easiest way to check for the passed vars on the processing side would be a simple:-

print_r($_POST);
exit;

This would display all the data that is being submitted by the form, handy for checking spelling of keys & values etc.

Hopefully I have understood you correctly.

Cheers,
MRb

CyBerAliEn




msg:4123137
 7:48 pm on Apr 27, 2010 (gmt 0)

Notes on your HTML

Consider this::
<?php
//Your current code
echo "<form name='traffic_test' method='post' action='traffictest2.php'>";

//Better Code
echo "<form name=\"traffic_test\" method=\"post\" action=\"traffictest2.php\">";
?>


I imagine the single quotes are used so that PHP will interpret and parse the string without error. But you really should use double quotes on all HTML attribute values.
Good: name="myelement"
Bad: name='myelement'
Worst: name=myelement

In code like this:
echo "<p class=\"greeting\">Hello World</p>";
The double quotes for 'class' will create an error (if you use just the quotes); because PHP will interpret the first quote after 'class=' as "this is the end of the statement". To tell PHP to ignore the double quote (ie: don't parse/process it), you escape it. You do this by adding the '\' character before the quote. Changing class="greeting" to class=\"greeting\" inside a PHP statement will tell PHP to treat the internal (escaped quotes) as simply characters to display.

Just my opinion; proper/good coders should use double quotes; sloppy coders tend to leave out the quotes all together; and I've never really encountered anyone using single quotes.



BAD:
<a class='red1b' href='../../index.htm' target='_top'><<< Back to Main Website >>></a>

GOOD:
<a class="red1b" href="../../index.htm" target="_top">&lt;&lt;&lt; Back to Main Website &gt;&gt;&gt;</a>


This above code is in your PHP statement (being echo'ed). You'll need to escape the double quotes to get it to echo this "good" version. My emphasis is: (a) you should use double quotes; (b) you use '>' and '<' as text, but these characters have special meaning to HTML. When you want to display '>' or '<', you should use HTML entities. Essentially...
(symbol/character) = (meaning) = (HTML entity)
> = greater than = &gt;
< = lesser than =&lt;



Notes on the PHP
I don't really see any glaring error in your PHP code. What exactly is the problem (specifically)? Is that this form is being submitted and no values are showing up on the other end?

Try this as your code:
<?php
//### Start Form (reference) ###
echo "<form name=\"traffic_test\" method=\"post\" action=\"traffictest2.php\">";

//### Incrementers ###
$qcount=1;
$count2=1;
//### Now The Questions ###
while($qcount<16)
{
echo "<tr><td class=\"red1b\">{$qcount}&nbsp;</td><td class=\"red1b\">{$q[$qcount][0]}</td></tr>";
while ($count2<5)
{
if($q[$qcount][$count2]!="&nbsp;")
{
$abc=$q[$qcount][$count2];
echo "<tr><td align=\"center\"><input type=\"radio\" name=\"a[{$qcount}]\" value=\"{$count2}\"></td><td class=\"blu1\">{$q[$qcount][$count2]}</td></tr>";
}
$count2++;
}
echo "<tr><td colspan=\"2\">&nbsp;</td></tr>";
$qcount++;
if($count2==5) { $count2=1; }
}

//### Now do the submit button ###
echo "<tr><td align=\"center\" colspan=\"2\"><input class=\"red2b\" type=\"submit' name=\"submit\" value=\"Submit\"></table></table></form>";
echo "<br><br><br><p align=\"center\" class=\"blu0\">04/22/2005</p>";
echo "<p align=\"center\"><a class=\"red1b\" href=\"../../index.htm\" target=\"_top\">&lt;&lt;&lt; Back to Main Website &gt;&gt;&gt;</a></p><br><br></body>";
?>



Specific notes...

(1) You use "<>" in your condition. This is not PHP notation I recall (though I do recall it from VBA/etc as meaning "not equal to"). In PHP, "not equal to" is represented as !=.

(2) You regularly concatanate (combine) strings using "," (the comma). In PHP, you should be using the "." (period).

This:
"Hello","World"

Should be:
"Hello"."World"

(3) Your form data is being sent to traffictest2.php. On this script/page, add the following to the top of the script (before any of your own code):

<?php
echo "<p>Testing Form Values!</p>";
echo "<pre>";
print_r($_POST);
echo "</pre>";
echo "<p><i>Terminating Script!</i></p>";
exit();
?>


This code will output ALL of the data stored in "_POST" in a human read-able form. You should be able to look at this output to see how data is being sent to 'traffictest2.php' (its form/structure and what actual data/content).

mfleck




msg:4123612
 2:04 pm on Apr 28, 2010 (gmt 0)

First, a big thank you to all of you who helped me to a successful repair of my code. By printing the POST values on the processing page (as you suggested) I determined that indeed the data was coming through from the form page. So then it was a matter of figuring out how to capture the data and make it usable. Here is the code that I ended up with:

# This processes the array from the form
foreach (array_keys($_POST) as $key) {
$$key = $_POST[$key]; }

# This sets answers into proper array
foreach($a as $key => $val) {$q[$key][1]=$val; }


The test is now working perfectly and I know it would have taken me a lot longer to get here without your help.

As for the comments about my coding, I accept some of the criticism and I always strive to improve. As for the <> comparator for not equal to, you will find that the method I use is valid (http://php.net/manual/en/language.operators.comparison.php). Perhaps my adoption of the <> is based on my math background, perhaps it was used by wherever it was I learned about PHP comparators.

As for my use of single quotes instead of double quotes, again, you'll find the use of single quotes aptly described at [php.net...] where they discuss 4 different methods of specifying strings. The reason that I began using the single quote is that for me the script is much cleaner and more readable. There are from time to time advantages to using the double quotes, at which time I do.

Lastly, as for me using a comma instead of a period when building strings, if you examine the code a little more closely you'll see that I was building an echo statement, not a string. The comma is appropriate for building the echo statement and I agree that anything less than a period for building a string will generate errors. I also note that my use of echo instead of print is somewhat unusual but it is the result of an article I read that proved that in most cases, the use of echo instead of print consumes less memory and results in faster loading pages. I can neither find the article nor prove it, but that is how I wound up coding that way.

Again, thanks to all who posted their help - I was convinced that the form was not passing the array data so I was a long way from a solution.

Matthew1980




msg:4123621
 2:19 pm on Apr 28, 2010 (gmt 0)

Hi there Mfleck,

Glad you are sorted out now!

Lastly, as for me using a comma instead of a period when building strings, if you examine the code a little more closely you'll see that I was building an echo statement, not a string. The comma is appropriate for building the echo statement and I agree that anything less than a period for building a string will generate errors. I also note that my use of echo instead of print is somewhat unusual but it is the result of an article I read that proved that in most cases, the use of echo instead of print consumes less memory and results in faster loading pages. I can neither find the article nor prove it, but that is how I wound up coding that way.


I never knew about the comma's so I learned something there too :) But, I always try to avoid using echo's or print (except debug) as this is more strain on the parser. I personally prefer to 'dip-in-dip-out' of php and echo the var if I can help it, makes the code easier to read too. Again this may be down to personal preference, but I think it is the better way to go:-

<p>Hi there <?php echo $DataAssignedToVar;?> glad as you came by to visit!</p>
<p>Todays date is:<?php echo date("someDate");?> yor last visit was on: <?php echo date("lastDate");?>!</p>

You see what I am aiming to get across there. Functions are the same too:-

function myFunction(){
$StringToEcho = "Hi there people";
return $StringToEcho;
}

But I am sure you already knew that ;-p

Cheers & good luck with the rest of the project,
MRb

CyBerAliEn




msg:4123705
 4:24 pm on Apr 28, 2010 (gmt 0)

The comma is appropriate for building the echo statement and I agree that anything less than a period for building a string will generate errors.

Interesting; I did not know you could use a comma in this manner in PHP. Even in building an echo, I always parse together pieces using a period. Though my personal preference is to do something like:

//My personal preference:
echo "<p>Hello {$name}! Welcome to my site!</p>";

//What a lot of people do:
echo "<p>Hello ".$name."! Welcome to my site!";


I use to do the period concatanation style, but when I learned I could use curly braces instead, it really made me happy. Note, that I know you could just put "$name" without the curly braces and it would work. But I am in the habit of always doing it in this manner now because it will allow you insert array values directly into a string with braces, a la: {$array['key1']['key2']}


I also note that my use of echo instead of print

I don't disagree there. I always use ECHO to output. I think a lot of people who use PRINT use it because they're use to it (ie: print is a function/command in a lot of other languages).


Also, I did not know "<>" was valid notation in PHP. I have never seen it used in PHP (though have in other languages). Learn something new everyday; indeed.


However, my notes regarding single quotes has nothing to do really with PHP; I am well aware they are valid for PHP. My point was that using single quotes around attribute values of HTML is not good practice. Certainly, you can get PHP to output class='mybigbox' without error; but this isn't good practice HTML; it should be specified in the HTML as class="mybigbox". This is why I note using double quotes (with escaping) within the echo's. Though I do agree, it makes it uglier having the escapes, yuo should be sure the HTML your PHP generates is in good practice. Though using single quotes won't break anything as far as a reader/viewer is concerned, it could be misinterpreted by various different browser and browser versions. Of course, it is your choice. HTML is very fluid. People say don't use <FONT> for formatting and don't use <TABLE> for layout; but some people still do it, and if you do, it certainly won't break anything; but it isn't good practice. ;)



But we're all glad you solved your problem! :)

Matthew1980




msg:4123843
 6:33 pm on Apr 28, 2010 (gmt 0)

Hi there cyberalien,

People say don't use <FONT> for formatting and don't use <TABLE> for layout; but some people still do it, and if you do, it certainly won't break anything; but it isn't good practice. ;)


I completely agree with that, CSS is now the way forward though I think, great tool, and changing from table layout to div layout is worth the learning curve, I personally haven't used a table in over a year and a half :) all good.

mfleck,

Enjoy the rest of the coding!

Cheers,
MRb

mfleck




msg:4123890
 7:28 pm on Apr 28, 2010 (gmt 0)

Of all the things I have learned on this issue (and there have been several) the one I like the best and will test and more than likely adopt, is the use of the braces. It is much cleaner looking and faster to code to change my
echo ">",$a01,"<"; to your echo ">{$a01}<";

I am not convinced on the single/double quote issue, but I tend to write the entire page in php and not jump in and out of html. Perhaps that is why it doesn't become an issue.

As I reread some of what I typed earlier, I sound more defensive than grateful, which is certainly not the case. I know that there is always room for personal preference and I also don't stay as current with some of the more modern features of php as I should. It is always good to get other perspectives and the advice of others.

So whether I should be 'grateful' or "grateful", rest assured that I am :)

Mike

Matthew1980




msg:4123907
 7:47 pm on Apr 28, 2010 (gmt 0)

Hi there mfleck,

...but I tend to write the entire page in php and not jump in and out of html. Perhaps that is why it doesn't become an issue.


I will have to disagree with that statement, reason being is that the bigger the echo the more strain on the CPU/server & the parser. IMHO it's much, much better & easier to code dipping in and out of php, thus making the html editing a lot simpler, not necessarily tidier but definitely less of a strain on the server.

I started out years back echoing entire pages, but then realised that it was by far easier to edit the html outside the php, and found that the php became easier as a result.

Again, just an opinion.

But seriously, if you are going to echo small pieces of data, that's fine, we all do that. Just not entire pages of html tags echoed!

Anyway, enjoy the coding & learning curves :)

Cheers,
MRb

CyBerAliEn




msg:4124125
 2:45 am on Apr 29, 2010 (gmt 0)

Something interesting you may not know about as well...

Suppose you had code like:
<?php
echo "<div class=\"content\">";
echo "<h1>Welcome ".$name."!</h1>";
echo "<p>Websites are cool. OK?</p>";
echo "</div>";
?>


The idea: you have a bunch of echo's outputting HTML. It is usually pretty obvious why this is done: you usually have PHP doing some sort of processing or manipulation and then outputting the proper HTML. But something you can do in PHP is to change code like the above to this:

<?php
echo <<<END
<div class="content">
<h1>Welcome $name</h1>
<p>Websites are cool. OK?</p>
</div>
END;
?>


The above will echo everything from "<<<END" until it finds "END;". I've always used this notation, but I believe you could change it (ie: "<<<ENDOUTPUT" and "ENDOUTPUT;"). It is pretty useful in that it will run echo and dump the output in swoosh, and it will also parse string-compatible PHP (ie: if you include a variable, it will parse it). I find this to be pretty useful when I want to dump a "chunk" of PHP. I personally don't like seeing code where there is a bunch of PHP mixed with HTML and the file "jumps in" and "jumps out" of PHP to do the outputting; always felt ad hoc to me.

But I generally agree with Matthew... PHP shouldn't be outputting 100% of the HTML anyway. It really should serve as a "processor" of sorts where the bulk of your content is not directly output by PHP.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved