Form issue[topic solved]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

SirChick
Forum Contributor
Posts: 125
Joined: Tue Jul 31, 2007 11:55 am

Form issue[topic solved]

Post by SirChick »

I have a form and im trying to get the user's choice to be added to these 2 variables which are part of a session but it will not do so...could you take a look and see why for me ? heres the code:


<form name="Form1" method="POST" action="housepurchaseprocess.php" enctype="multipart/form-data" id="Form1" onsubmit="return ValidateForm1(this)">


The above part in bold is a must as that is where the process occurs of the user's purchase but it wont get the session variables...

Code: Select all

<?php

$soldhousesquery = "SELECT Price, HouseType FROM soldhouses ORDER BY Price ASC";
$soldhousesresult = @mysql_query($soldhousesquery) or die(mysql_error());


echo '<select name="houselist" size=10>">';

while($soldhousesrow = mysql_fetch_array($soldhousesresult)) {
        echo "<option value=\"{$soldhousesrow['Price']}|{$soldhousesrow['HouseType']}\">£ {$soldhousesrow['Price']} - {$soldhousesrow['HouseType']}</option>";
}

echo '</select>';

?>

</div>
<input type="submit" id="Button2" name="Buy" value="Buy House" style="position:absolute;left:350px;top:600px;width:184px;height:24px;z-index:20">
</form> 

<?
//these 2 sessions when echo'd only house type displays but not price
$Price = $_POST['Price'];
$HouseType = $_POST['HouseType'];
$_SESSION['Price'] = $Price;
$_SESSION['HouseType'] = $HouseType;
?>
Last edited by SirChick on Mon Sep 10, 2007 3:08 pm, edited 1 time in total.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Post by califdon »

Did you initiate the session? The sessionstart() at the beginning of every script? http://php.about.com/od/phpfunctions/g/ ... _start.htm
josa
Forum Commoner
Posts: 75
Joined: Mon Jun 24, 2002 4:58 am
Location: Sweden

Post by josa »

By looking at your post (mostly at your other post concering the same issue) it looks like you are trying to pass the form values in a session to the housepurchaseprocess.php page. Is that what you are trying to do?

/josa
SirChick
Forum Contributor
Posts: 125
Joined: Tue Jul 31, 2007 11:55 am

Post by SirChick »

Yes session_start() is at the very top of the page i should of mentioned that sorry.

And you are correct with me trying to pass the session to the process page.
SirChick
Forum Contributor
Posts: 125
Joined: Tue Jul 31, 2007 11:55 am

Post by SirChick »

bump
josa
Forum Commoner
Posts: 75
Joined: Mon Jun 24, 2002 4:58 am
Location: Sweden

Post by josa »

I'm not clear on these:

Code: Select all

$Price = $_POST['Price'];
$HouseType = $_POST['HouseType'];
Where does this posted data come from? Is there another form somewhere that posts to this page? If you only post data from a form then you don't need sessions. I'm only trying to get the complete picture, because right now I'm confused. :-)

/josa
SirChick
Forum Contributor
Posts: 125
Joined: Tue Jul 31, 2007 11:55 am

Post by SirChick »

Heres the code with the form surrounding it:

Code: Select all

<form name="Form1" method="POST" action="housepurchaseprocess.php" enctype="multipart/form-data" id="Form1" onsubmit="return ValidateForm1(this)">

<?php
//generates a list box to display the rows of available houses in the soldhouses table depending on which one the user clicks on then clicks buy will be "should be" the one that goes to the session
$soldhousesquery = "SELECT Price, HouseType FROM soldhouses ORDER BY Price ASC";
$soldhousesresult = @mysql_query($soldhousesquery) or die(mysql_error());


echo '<select name="houselist" size=10>">';

while($soldhousesrow = mysql_fetch_array($soldhousesresult)) {
        echo "<option value=\"{$soldhousesrow['Price']}|{$soldhousesrow['HouseType']}\">£ {$soldhousesrow['Price']} - {$soldhousesrow['HouseType']}</option>";
}

echo '</select>';

//these 2 sessions when echo'd are blank when loaded on to a different page using the session
$Price = $_POST['Price'];
$HouseType = $_POST['HouseType'];
$_SESSION['Price'] = $Price;
$_SESSION['HouseType'] = $HouseType;

//the below 2 come from a query that gets the row and gets the 2 fields "SoldHouseID" and "NumberOfRooms", this does not work either
$_SESSION['HouseID'] = $soldhousesrow['SoldHouseID'];
$_SESSION['NumberOfRooms'] = $soldhousesrow['NumberOfRooms'];

?>
</div>
<input type="submit" id="Button2" name="Buy" value="Buy House" style="position:absolute;left:350px;top:600px;width:184px;height:24px;z-index:20">
</form>

The form method is POST so i used $_POST and then the session is because it goes to a process page see below:


method="POST" action="housepurchaseprocess.php"
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Post by Stryks »

So, you're saying you have a three stage form yes? Price and HouseType come from form1, the form you have shown is form2, and you want the values from form1 and form2 to be sent to housepurchaseprocess.php, yes?

It's kind of hard to discern, because you are only showing us the tail end of this problem. We need the root of the problem and in this case I imagine that's going to be the first form. My first thought at a quick glance is a mismatch of names or you've set the id and not the name of the form elements on that first form.

Can we see your first form please?

Also, not knowing how much you know about session variables, it might be worth noting that you MUST do a session_start() at the top of the page where you want to READ session values, not just when you want to WRITE them. In this case, housepurchaseprocess.php will need to begin with a session_start() call, as well as the form shown.
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Post by Stryks »

As troubleshooting tools go, a var_dump can save a lot of head scratching.

Whack a

Code: Select all

var_dump($_POST);
in there and take a look at what is shown.

What you would expect?
SirChick
Forum Contributor
Posts: 125
Joined: Tue Jul 31, 2007 11:55 am

Post by SirChick »

Right the response on the var dump:
array(0) { }

Notice: Undefined index: Price in C:\xampp\htdocs\housepurchaselist.php on line 67

Notice: Undefined index: HouseType in C:\xampp\htdocs\housepurchaselist.php on line 68


Stryks, this is the structure in logic form:


From1:

Query to get rows.
*Create list box using html via echos*
*Display a purchase button*
End form.

When purchase button is pressed, the selected row in the list box is then stored into a session with session_start().
Once stored into a session because From1 has:

action="housepurchaseprocess.php that means when the purchase button is pressed itll load up that php file.

I then in the housepurchaseprocess.php load up the session using session_start(); and use the user's choice to do the processing with. I couldn't do it on the same page cos i needed header location which i cannot do if ouput has already started.

Then I do the necessary processing such as subtracting the money from the user and adding to fields in the Database and then once that process is done is goes to the success page, which is just "you have succesfully bought" etc
josa
Forum Commoner
Posts: 75
Joined: Mon Jun 24, 2002 4:58 am
Location: Sweden

Post by josa »

Okay, I'm going to give this another try. If I understand correctly Form1 is the only form involved. There is no other form "before" that form like Stryks suggested? If this is the case then the session logic will never work the way you use it. The first time the file with the form is parsed, the $_POST array is going to be empty. When you click submit the form data is going to get posted directly to housepurchaseprocess.php. On this page you should use $_POST to retrieve the data - forget sessions for this.
Now for the other two variables:

Code: Select all

$_SESSION['HouseID'] = $soldhousesrow['SoldHouseID']; 
$_SESSION['NumberOfRooms'] = $soldhousesrow['NumberOfRooms'];
SoldHouseID and NumberOfRooms are not in your SQL query so that won't work. Even if they were you would only store the last fetched row in the session and not the row the user selects in the form. Consider using hidden form fields instead of sessions for the data you want to pass to the process page, but don't want the user to see.

/josa
Last edited by josa on Wed Sep 05, 2007 10:05 am, edited 1 time in total.
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Post by Stryks »

Yeah, josa is right on the money. The process you describe doesn't appear to require sessions.

A few things that might make this make more sense.

Submitting a form using method="post" will result in the page specified in action="something.php" being opened containing all of the form data in the $_POST superglobal.

Test this out. Just create a simple form in a new page, with just a text box and a submit button. Then create another new file and just put a var_dump($_POST) in. Make sure the first page uses the second as the action, open the first form and see what you get. By rights, it should echo all of the forms contents and their values.

Also, where the form gets sent, as specified in "action", doesn't really have any practical effect on how either sessions or $_POST handling works. You'll get the same result either way. The benefit of posting it back to the same page usually comes from either the form being cyclic (ie. the form is shown along with the data you just submitted) or because validation is being done on the input. If it fails validation, you're already loading the form they'll need to correct.

Also, if you process the submitted data on the same page and then do a header redirect to the next step, you can prevent people "backing" into forms, and the problems this may cause. Still, nothing you need to contemplate too hard until you've had the problem.

Anyhow, play with this a bit and get a feel for how it works. Once you've done that you'll see how you'll apply it to your page.

Let us know how you go. 8)
SirChick
Forum Contributor
Posts: 125
Joined: Tue Jul 31, 2007 11:55 am

Post by SirChick »

Ok thankyou guys! Some good advice! Will give it a try!

May i ask what you mean by hidden values?
SirChick
Forum Contributor
Posts: 125
Joined: Tue Jul 31, 2007 11:55 am

Post by SirChick »

Ok guys,


I have had a fiddle but it still will not work.... not sure if i have done something wrong how ever.... this is the code at the moment:

I put a var dump on the process page and got this:
array(2) { ["houselist"]=> string(0) "" ["Buy"]=> string(9) "Buy House" }
Form:

Code: Select all

<form name="Form1" method="POST" action="housepurchaseprocess.php" enctype="multipart/form-data" id="Form1" onsubmit="return ValidateForm1(this)">

<?php
//generates a list box to display the rows of available houses in the soldhouses table depending on which one the user clicks on then clicks buy "should be" the one that goes to the process page
$soldhousesquery = "SELECT Price, HouseType FROM soldhouses ORDER BY Price ASC";
$soldhousesresult = @mysql_query($soldhousesquery) or die(mysql_error());


echo '<select name="houselist" size=10>">';

while($soldhousesrow = mysql_fetch_array($soldhousesresult)) {
        echo "<option value=\"{$soldhousesrow['SoldHouseID']}\">£ {$soldhousesrow['Price']} - {$soldhousesrow['HouseType']}</option>";
		}

echo '</select>';


?>
</div>
<input type="submit" id="Button2" name="Buy" value="Buy House" style="position:absolute;left:350px;top:600px;width:184px;height:24px;z-index:20">
</form>

Process Page:

Code: Select all

$CityID = $row['CityID'];
$HousePrice = $_POST{$soldhousesrow['Price']};
$HouseType = $_POST{$soldhousesrow['HouseType']};
$HouseID = $_POST{$soldhousesrow['SoldHouseID']};
Echo $HouseID;
Echo $HousePrice;
Echo $HouseType;
All 3 were blank when echo'd ! Just so you know with the form i changed value of option to house ID otherwise i couldn't process the info without some how obtaining the house ID for that particular row.
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Post by Stryks »

Ok, where to begin.
array(2) { ["houselist"]=> string(0) "" ["Buy"]=> string(9) "Buy House" }
Not sure if you're understanding the output, but it is saying that the POSTed form contains one field named "houselist" containing "" (blank in other words) and another named "Buy" containing the value "Buy House". That last is obviously the button used on the form.

You can access the values in the process page with $_POST['houselist'] and $_POST['Buy']. ie.

Code: Select all

echo $_POST['houselist'];
Now, as for when you're populating the select.

In your SQL, you specify that columns Price, and HouseType are returned from the database. They are, and so what you SEE on the form is correct. However, you're referencing "soldHouseId" to set the value(which you cant see unless you go to code view in your browser), and as it's not in the query, it's not going to be in the results.

Edit your SQL to include the data you need to build the select, and then you'll be able to reference it the way you are.

Also, database query results are like all other variables (apart from sessions) in that they are not persistent between pages. You can not access $soldhousesrow from the process page without doing another db query for it from that page.

Does this make sense?
Post Reply