PDA

View Full Version : Error


smitho
12-12-2007, 09:07 PM
I also seem to be having problems with this tutorial. I had a feeling I was not connecting to the database properly so I followed irock's example and it works.

I'm still getting an error before I make a selection.


Notice: Undefined index: make in <----- which is $make = $_POST['make'];

Should this have a isset()?

Just a suggestion it may be worth while posting the "Connections/connection.php" with the other file so that we can see how it's been set up.

Thanks for the tutorial it's been a great help.

Cheers

I thought I'd try and suppress the error by doing this @$make = $_POST['make']; which got rid of the error but then the second drop menu had:

Notice: Undefined variable: rowModel in <----- which is <option value="<?php echo $rowModel['car_model_id']; ?>"><?php echo $rowModel['car_model']; ?></option> If I apply the @ to $rowModel the second error disappear as well.

irock
12-12-2007, 09:32 PM
Just a suggestion it may be worth while posting the "Connections/connection.php" with the other file so that we can see how it's been set up.


Here is exactly what I have in my Connections/connection.php file (I copied and pasted right in here:

<?php
//////////////////////////////////////////////
$database = "dwc";
$username = "shane";
$password = "password";
/////////////////////////////////////////////


$link = @mysql_connect('localhost', $username, $password);
$db = @mysql_select_db($database, $link);

?>

Of course the variables are the values that I used when setting up the database.

I will post my dwc.php file if you want.
However I am working on Part 9 at the moment so it would have a few variations in it.

Loving these tutorials so far - I really hope you do more of them in the future David. I've wanted to learn PHP for a while but just didn't know where to start. Any chance you could recommend a book? - something that would help me learn by doing?

Cheers,

iRock :-D

davidj
12-13-2007, 05:53 AM
are you still getting errors?

remember that adding an @ only turns the warning off.

it does not fix the error

post the code please

smitho
12-13-2007, 09:38 AM
Hi davidj,

Using irock's example of the Connections/connection.php the menus are working but I'm still getting errors.

I am still getting this error when I first click on the page:

Notice: Undefined index: make in C:\wamp\www\SQL\dropdown.php on line 4 (this is line 4 --->$make = $_POST['make'];)

And before selecting a make the model menu has this error in it:

Notice: Undefined variable: rowModel in <----- which is <option value="<?php echo $rowModel['car_model_id']; ?>"><?php echo $rowModel['car_model']; ?></option>

davidj
12-13-2007, 09:59 AM
need to see your code

post your code please

smitho
12-13-2007, 10:45 AM
I'm just using the dropdown.php that is supplied with the zip file.

Here it is:

<?php
require_once("Connections/connection.php"); // database connection

$make = $_POST['make'];


if ($make){
/////////////////////////////////////////////////
$query = sprintf("SELECT * FROM car_model where car_model_id='$make'");
$result = @mysql_query($query);
$rowModel = mysql_fetch_array($result);
/////////////////////////////////////////////////
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>

<form id="form1" name="form1" method="post" action="dropdown.php" >

<select name="make" onChange="document.forms[0].submit()">
<option value="">Select Make</option>
<option value="1" <?php if(!(strcmp(1, $make))){echo "selected";}?>>Alfa Romeo</option>
<option value="2" <?php if(!(strcmp(2, $make))){echo "selected";}?>>Audi</option>
<option value="3" <?php if(!(strcmp(3, $make))){echo "selected";}?>>BMW</option>
<option value="4" <?php if(!(strcmp(4, $make))){echo "selected";}?>>Citroen</option>
</select>


<select name="model">
<option value="">Select Model</option>
<?php do { ?>
<option value="<?php echo $rowModel['car_model_id']; ?>"><?php echo $rowModel['car_model']; ?></option>
<?php }while ($rowModel = mysql_fetch_array($result)); ?>
</select>

</form>

</body>
</html>

davidj
12-13-2007, 10:52 AM
did you set up your database correctly and all the fields are correct

smitho
12-13-2007, 08:48 PM
OK the errors I was getting were on the PC last night using WAMP. I just tried the same setup on my Mac this morning and I got no errors which is strange. I imported the database with no error using PHPmyAdmin on the mac I did not have the same luck on the PC and did it via the cmd window manually (cut and paste approach). I must have missed something. I'll start from scratch on the PC see how that goes.

smitho
12-13-2007, 10:51 PM
Sorry davidj

I just double checked the Mac code and had the @ infornt of the errors i was getting yesterday and that's why it worked. As soon as I removed them the errors came back. Once I have the first selection the errors go away.

smitho
01-10-2008, 03:49 AM
This may be a silly question but here goes.

I wanted to take this tutorial one more step by sending the results of what was selected to a results.php page.

I've added session_start to capture the results but I'm finding that as soon as I add a submit button the second drop menu stops working. I'm would say yo can not have submit() onchange and submit as a button.

Here is the code of the dropdown with the changes I've made in red.

dropdown.php

<?php
session_start();
//require_once("Connections/connection.php"); // database connection
$db_connection = mysql_pconnect('localhost', 'NAMEHERE', 'PASSWORDHERE');
$db_status = mysql_select_db('dwc', $db_connection);
if (!$db_status){ echo "Data_base_ Connection Failed";
exit;}

@$make = $_POST['make'];


if ($make){
/////////////////////////////////////////////////
$query = sprintf("SELECT * FROM car_model where car_model_id='$make'");
$result = @mysql_query($query);
$rowModel = mysql_fetch_array($result);
/////////////////////////////////////////////////
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>

<form id="form1" name="form1" method="post" action="results.php" >

<select name="make" onChange="document.forms[0].submit()">
<option value="">Select Make</option>
<option value="1" <?php if(!(strcmp(1, $make))){echo "selected";}?>>Alfa Romeo</option>
<option value="2" <?php if(!(strcmp(2, $make))){echo "selected";}?>>Audi</option>
<option value="3" <?php if(!(strcmp(3, $make))){echo "selected";}?>>BMW</option>
<option value="4" <?php if(!(strcmp(4, $make))){echo "selected";}?>>Citroen</option>
</select>


<select name="model">
<option value="">Select Model</option>
<?php do { ?>
<option value="<?php echo $rowModel['car_model_id']; ?>"><?php echo $rowModel['car_model']; ?></option>
<?php }while ($rowModel = mysql_fetch_array($result)); ?>
</select>
<input type="submit" name="submit" id="submit" value="Submit" />
</form>
<?php
$_SESSION['make']=$_POST['make'];
$_SESSION['model']=$_POST['model'];
?>
</body>
</html>


results.php

<?php
session_start();
echo "This is a session ".$_SESSION['make'];
echo "<br />";
echo "This is a session ".$_SESSION['model'];
echo "<br />";
echo "This is a post result ".$_POST['make'];
echo "<br />";
echo "This is a post result ".$_POST['model'];

?>

davidj
01-10-2008, 08:24 AM
the best way would be to use javascript and append the results to be passed to the url


<script language="javascript" type="text/javascript" >

function submitFunc(){

var make = document.getElementById('make').value;
var model = document.getElementById('model').value;

document.location.href="form3.php?make"+make+"&model="+model;

}
</script>


once you select the last option then click the button


<input type="button" name="submit" id="submit" value="Submit" onclick="submitFunc()"/>

smitho
01-10-2008, 10:35 AM
Thanks davidj,

tried your suggestion but found that the second dropdown stopped working as soon as I put the submit button in.

<?php
//require_once("Connections/connection.php"); // database connection
$db_connection = mysql_pconnect('localhost', 'name', 'password');
$db_status = mysql_select_db('dwc', $db_connection);
if (!$db_status){ echo "Data_base_ Connection Failed";
exit;}

$make = $_POST['make'];


if ($make){
/////////////////////////////////////////////////
$query = sprintf("SELECT * FROM car_model where car_model_id='$make'");
$result = @mysql_query($query);
$rowModel = mysql_fetch_array($result);
/////////////////////////////////////////////////
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script language="javascript" type="text/javascript" >

function submitFunc(){

var make = document.getElementById('make').value;
var model = document.getElementById('model').value;

document.location.href="form3.php?make"+make+"&model="+model;

}
</script>
</head>

<body>

<form id="form1" name="form1" method="post" action="dropdown.php" >

<select name="make" onChange="document.forms[0].submit()">
<option value="">Select Make</option>
<option value="1" <?php if(!(strcmp(1, $make))){echo "selected";}?>>Alfa Romeo</option>
<option value="2" <?php if(!(strcmp(2, $make))){echo "selected";}?>>Audi</option>
<option value="3" <?php if(!(strcmp(3, $make))){echo "selected";}?>>BMW</option>
<option value="4" <?php if(!(strcmp(4, $make))){echo "selected";}?>>Citroen</option>
</select>


<select name="model">
<option value="">Select Model</option>
<?php do { ?>
<option value="<?php echo $rowModel['car_model_id']; ?>"><?php echo $rowModel['car_model']; ?></option>
<?php }while ($rowModel = mysql_fetch_array($result)); ?>
</select>
<input type="button" name="submit" id="submit" value="Submit" onclick="submitFunc()"/>

</form>

</body>
</html>

davidj
01-10-2008, 12:45 PM
take this...


<input type="button" name="submit" id="submit" value="Submit" onclick="submitFunc()"/>


and move it out of the form tag

smitho
01-10-2008, 08:33 PM
Thanks davidj, that works like a charm.

smitho
01-10-2008, 11:21 PM
Hi davidj,

thought I'd have a go at changing the first drop menu to be linked to the database.

Added this for the first dropmenu
/////////////////////////////////////////////////
$querya = sprintf("SELECT * FROM car_make");
$resulta = @mysql_query($querya);
$rowMake = mysql_fetch_array($resulta);
/////////////////////////////////////////////////In the first dropmenu where the options where coded into the page I added this

<?php
do {
?>
<option value="<?php echo $rowMake['make_id']?>"><?php echo $rowMake['make']?></option>
<?php
} while ($rowMake = mysql_fetch_assoc($resulta));
?>This works in that once you make a selection the second dropmenu it populated. The problem is that the option you selected in the first menu is gone as the page is refreshed.

To try and have the first option stay once selected I tried this

<?php
if($rowMake['make_id'] == @$make){
?>
<option selected value="<?php $rowMake['make_id']; ?>"><?php $rowMake['make'];}else{?></option>"
<?php
do {
?>
<option value="<?php echo $rowMake['make_id']?>"><?php echo $rowMake['make']?></option>
<?php
} while ($rowMake = mysql_fetch_assoc($resulta));
}?>The first option is still refreshing to Select Make.

smitho
01-11-2008, 01:27 AM
Worked it out.

To stop the first drop down option refreshing to Select Model after you make a selection you can use this code. If I have it correct, hopefully davidj can confirm.

<?php
do {
?>
<option value="<?php echo $rowMake['make_id']?>"<?php if (!(strcmp($rowMake['make_id'], $make))) {echo "selected=\"selected\"";} ?>><?php echo $rowMake['make']?></option>
<?php
} while ($rowMake = mysql_fetch_assoc($resulta));
?>

davidj
01-11-2008, 05:44 AM
thats correct

but where you have this

{echo "selected=\"selected\"";}


you can us the following to simplify it

{echo "selected";}

smitho
01-13-2008, 10:59 PM
Is there another way of refreshing the dropdown lists?

If I add a dropdownlogin.php to the flow with the following code:


<form action="dropdown.php" method="post" enctype="multipart/form-data" name="details">
<label>Enter your name:
<input type="text" name="name" id="name">
</label>
<label>Submit
<input type="submit" name="submit" id="submit" value="Submit">
</label>
</form>
And in the dropdown.php just above the form I put this code:


<?php

$username=$_POST['name'];
echo "Welcome ".$username." please select your favorite car below: ";


?>
When you start at the dropdownlogin screen and enter your name then click the submit button the dropdown screen will echo the message with the name that was typed into the text field on the dropdownlogin screen. This is all good until you click on the first dropdown to Select Make. I get a error Notice: Undefined index: name in.... which obviously occurs because the screen is reloaded to populate the second dropdown but the $username=$_POST['name']; variable if lost. Is there a way around this?

davidj
01-14-2008, 05:58 AM
assign the name to a session and echo that

smitho
01-14-2008, 09:07 PM
Would sessions work for every situation?

The test I'm doing is I have a user log in then they can make a number of checkbox selections for a pdf file they are about to upload. Once they upload the pdf file I pull out some of the metadata and display that as well as give them some text fields to fill in and some dropdowns.

Because of the file upload process I'm not sure that sessions would work. The way I got around the problem was to create a popup. I pass the initial information from the main window to the dropdown popup and once the user selects their options apply them back to the main window. This seems to work. Strange that I can't seem to get the checkbox popup to work but the dropdown works fine.

Is there any issue in the way I'm going about this that you can see?