homepage Welcome to WebmasterWorld Guest from 54.205.144.54
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

    
Form Data Not Behaving As Expected.
Forms not updating correctly - missing data
mikesz




msg:3602473
 2:11 am on Mar 17, 2008 (gmt 0)

Hello,

I have been working on a form for some time and am baffled about why it is not doing what I think it is suppose to do.

By the way, it is a form that I have inherited and part of a CMS so I am limited about how much I can change it by the application.

The situation is that the form (which is using the output buffer)has a
birthday input.

Almost forgot, this is a complex login form that has an IP Address
check in it. If the "expected" IP address is different that the one
stored in the database, it loops back and adds a test for the users
birthdate which does a different database lookup. This one form is
used to manage both conditions.

The form was originally designed to use a text box and the request was
to replace it with a drop down because his users didn't follow the
instructions for formatting most of the time, even with an example
provided. The form does a lookup to see if the birthdate matches the
one that is stored in the database. In the lookup, it compares the
value in $_POST['birthdate'] with the value in the database. The text
box works fine if you enter the correct yyyy-mm-dd format.

The replacement dropdown has three $_POST values very creatively
assigned, $_POST['year'],$_POST['month'],$_POST['day']. To compensate
for the replacement of $_POST['birthdate'] and try to make the change
transparent to the rest of the script I generated it manually in a
hidden field (I actually thought this one WAS creative until I
discovered that it didn't work as I expected, lol),

<input type="hidden" value="<?=$_POST['year'] . "-" . $_POST['month'] . "-" . $_POST['day'];?>" name="birthdate">

The problem, as you may have already guessed is that
$_POST['birthdate'] gets no date values until AFTER the submit button
and that is too late for it to do its database compare so it fails. If
you rerun the form entry a second time the script works, no problem
but the first time through nada.

I have placed several print_r statements to see what is going on and
the very last I have just after the submit and just after the
ob_get_content and just before the return and $_POST['birthdate'] has
the correct value in it but apparently the script compare function
isn't see it for some reason. If, like I say, I run the input again,
it works fine?

I have tried replacing the $_POST['birthdate'] with a variable
$birthdate that I generate manually with the correct data and pass it
to the diff function but that breaks the script so that it doesn't work
at all no matter how many times you run it. I would really appreciate
someone who might have done something like this and solved it to give
me an idea of where I need to go with it.

Many thanks for your attention,

 

MarkFilipak




msg:3602552
 4:15 am on Mar 17, 2008 (gmt 0)

If I understand your problem correctly...
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
</head>
<body>
<input onchange='document.getElementById("year").value=this.value' ...>
<input onchange='document.getElementById("month").value=this.value' ...>
<input onchange='document.getElementById("day").value=this.value' ...>
<form onsubmit='this.birthdate.value=this.year.value+"-"+this.month.value+"-"+this.day.value'
action="...your-url-here..."
method="POST">
<input id="year" type="hidden" value=''>
<input id="month" type="hidden" value=''>
<input id="day" type="hidden" value=''>
<input name="birthdate" type="submit" value="Use this birthdate">

Habtom




msg:3602553
 4:17 am on Mar 17, 2008 (gmt 0)

Having the hidden values in this instace won't do anything for you.

You have those three values:
$_POST['year']
$_POST['month']
$_POST['day'].

You want it in this format:
yyyy-mm-dd

There seems to be something to it, but how about having the values restructured in the way you wanted them after you submit the data?

birthdate = $_POST['year']."-".$_POST['month']."-".$_POST['day'];

mikesz




msg:3602561
 4:42 am on Mar 17, 2008 (gmt 0)

Hello, Thanks for the replies. The reason I put the hidden field in there is that without it, $_POST['birthdate'] doesn't exist since I removed it when I replaced the text field with the dropdown menu. Unfortunately, the original author has a hard coded test for that POST value and if I replace it with a $birthdate variable, the script breaks.

Thanks for the feedback, I appreciate it, mikesz

Habtom




msg:3602573
 4:59 am on Mar 17, 2008 (gmt 0)

$_POST['birthdate'] is not in the forms alright. But when you submit the form, you reconstruct the data into the format you want.

When you submit the form it does go to some php file, doesn't it?

<form name="mydata" action="submitted.php">

Now if you see my point on submitted.php, you can do the following.
$birthdate = $_POST['year']."-".$_POST['month']."-".$_POST['day'];

MarkFilipak's solution is also a good one, but I do think you should do it from the server side like shown above if you don't have a different reason not to.

mikesz




msg:3602586
 5:25 am on Mar 17, 2008 (gmt 0)

Thanks Habtom,

Sorry for not being as clear, I didn't get the information across as well as I should have. Using the hidden field, $_POST['birthdate'] does exist but on the first submit it is contains only '--'. If you submit the form a second time it works with the correct date, for example, 1933-01-01 but the first time through it does not get the data from the dropdown.

I think Mark's code is a possible solution but I too would like it to be server side if possible.

The solution you have provided is the very first one I tried to work around this issue but it breaks the code so the "test" on the values from the submit fails. For some reason that I haven't found, the script is testing the value in $_POST['birthdate'] which is what the original code supplied but when I change it to a $birthdate which is logical to me, the data validation fails even if you hardcode the correct date expected.

pretty weird stuff...

thanks again, mikesz

Habtom




msg:3602592
 5:31 am on Mar 17, 2008 (gmt 0)

I got a little more confused ;)

What I did understand so far is that you do have three dropdowns (year, month, day), and the script expects an input in the format: yyyy-mm-dd.

If that is the case let's do the following:

1. Remove the hidden value ($_POST['birthdate']), we won't need it.
2. In the code that follows, if $_POST['birthdate'] has been used anywhere on the related code, replace it with $birthdate.
3. When submitted place this code somewhere on the top: $birthdate = $_POST['year']."-".$_POST['month']."-".$_POST['day'];

MarkFilipak




msg:3602603
 5:48 am on Mar 17, 2008 (gmt 0)

Habtom. Your line 3 is a no brainer -- I agree. I believe that the problem is that mikesz can't modify the code to add your line 3. I don't know why not, but nevertheless, that appears to be the situation.

mikesz




msg:3602803
 11:44 am on Mar 17, 2008 (gmt 0)

Thank you both for your help. That code actually does work when it is placed inside the validation function. This stuff is really spaghetti and I misunderstood the verification step. The form I modified is in a function that calls another function to verify that the input is a match for the value in the database. The verfication function has a switch statement that depends on a counter or is driven by the counter that is passed to it. For some reason the counter passes fine but the input variable $birthdate does not. When I moved the $birthdate definition inside the verify function everything worked. Go figure!

What made me think about the verify function was a debug statement I put in the first function just after the submit and before the output buffer clean and I put a similar debug statement in the verify function. The first one displayed the correct value but when it got to the verify function it was empty so I started thinking that clearing out the output buffer was shooting me in the foot and apparently it was because moving the definition to the verify function seems to have solved the problem.

I tried a few different things to try to make the $birthdate global thinking that was the problem but none of them worked.

I am not sure I like the resolution meaning that I think that "passing" that variable should have worked but didn't.

Thanks again for the insights I was pretty much banging my head and the feedback helped.

kindest regards, mikesz

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