Jump to content

Selected in dropdown list from database


rocky48

Recommended Posts

You're still calling buildSelectOptions() from within the <option> tag. The function returns a full select element. You're also submitting the form via $_POST but using $_GET to get the values, and the select names don't match the variables you're trying to use. On top of that, your HTML is malformed. Replace your top form (lines 94 - 104) with this:

<!-- Start of FORM -->
<form action="VerseQuery.php" method="post">
<?php
	echo buildSelectOptions('event', getEvents($conn), $_POST['event'],1);
	echo buildSelectOptions('mood', getMoods($conn), $_POST['mood'], 27);
?>
	<input type="submit" value="submit"/>
</form>
<!-- End of FORM -->

As for the getEvents() and getMoods() not returning any data, is your database set up in the way described in the query? Table Event_Sub does have an id and Event_Sub_Type, and table Events does have columns id and Event_Type?

Link to post
Share on other sites

Right!

I get the list now, with <--- Please Select--->, but my original question was how can I set the selected value to the one of my choosing.

I want the Events showing id = 1, but the Mood id = 27.

I thought that the last parameter in the function was going to do that?

So as Barand said the function only has 3 arguments and I am passing 4.

I assume that a new argument is needed, but what?

Link to post
Share on other sites

A new argument is the last thing you need, you are already passing too many.

 

The third argument is the ID of the one you want selected. You effectively have two third parameters. Instead of passing 27 after the $_GET() parameter, pass it instead of the $_GET().

Link to post
Share on other sites

I have been delayed in replying as I have been having problems with my localhost.

Before I get onto your suggestion Barand, I have a problem running the code on the localhost.

When I run this I get a Warning: Warning: mysqli_query(): Couldn't fetch mysqli in...

The error is in line 22 and 114.  But this line of code is working in other scripts and it can't be the Included code also for the same reason? The stack calls line 114 first, so the only common variable is the $conn! So why is $conn causing the error?

 

So the only conclusion I can make that it is to do with the function code, but I can't see where! Incidently, I have also tried this script (see post#24) on the remote server and it stops at the same point, but I can't see the warnings as they are suppressed.

 

Now regarding your suggestion Barand, once I can get passed the above error, I intend to try your suggestion.

Looking back at the previous posts, I see it was Maxxd that suggested the inclusion of the $_POST parameter and on reflection I don't understand why, as no variables are posted from a previous script.

 

Any more help would be appreciated!

Link to post
Share on other sites

$_GET['event'] and $_GET['mood'] are simply examples. You wanted to know how to select an option of the DOM SELECT element. You'll have to get the current value and pass it into the function in place of the $_GET variables that are there now. I can only see the code that you posted, so I don't know where those values are coming from. Basically, I used $_GET as a stub for this section of my original reply:

 

 

Probably could've explained that better earlier - sorry about the confusion.

 

I'm assuming that the user has selected an event and mood, and that it was passed from the previous form. At first, I thought your form action was 'get', but it later appeared as though it was 'post'. If you're pulling a default from the database, use the database results to compare against the current option value. If the defaults are a hard-coded value, use those in place of the $_GET/$_POST variables, like this:

<!-- Start of FORM -->
<form action="VerseQuery.php" method="post">
<?php
	echo buildSelectOptions('event', getEvents($conn), 1);
	echo buildSelectOptions('mood', getMoods($conn), 27);
?>
	<input type="submit" value="submit"/>
</form>
<!-- End of FORM -->

Hope that makes sense this time!

Link to post
Share on other sites

No! This is the form used to select the event and the mood.

I have been trying to eliminate the present situation on my website where the dropdown  menus default to a blank line i.e. no selection option shown.

 

However I can't test this code until I resolve the error I am now getting in the function(see previous post.

 

Can you spot what is causing the error?

Link to post
Share on other sites

the current error - Couldn't fetch mysqli in... means that the connection variable doesn't contain a valid (procedural) database connection. either your database connection code is failing or your code is closing the connection at some point.

 

does your database connection code have error checking logic in it and when there is a connection error, does your code take an appropriate action, to prevent the rest of the code from trying to use a connection that failed? the current error is a 'follow on' error. it's not where the problem is at.

 

do you have php's error_reporting set to E_ALL. you should be getting more (types of) php error messages that would help pin down the cause of the problem.

Link to post
Share on other sites

your code actually works for me. the only way i could reproduce your implied errors (if you post the actual errors, they may contain information that may help) is if the connection is failing and your connection code isn't exiting when there is a connection error.

 

the only way you can get the - Warning: mysqli_query(): Couldn't fetch mysqli in your_file on line 22 and
                                                 Warning: mysqli_error(): Couldn't fetch mysqli in
your_file on line 22 errors, is if $conn exists, but isn't a valid connection.

 

include ("connect_Verses4Cards_LOCAL.php");

 

 

^^^ given the name of your include file, do you have two different files, one for the localhost and one for a live server, and your actual code is including the live server version and it doesn't contain either any connection error checking or it doesn't contain the exit; statement in the connection error logic?

Link to post
Share on other sites

Firstly regarding the connection script - I do have two!  The one shown is for the localhost with the correct values to connect to my local MYSQL.

 

The best way of showing you the actual errors is to send a screen dump, which is attached.

PHPError30-11.png

I have tested copies of the previous script and that works OK with the same connection file.

I am sure the answer lies within the script. Here it is again:

<?php

include ("connect_Verses4Cards_LOCAL.php");
$conn = get_db_conn_verse();
//Function to build select options based on passed array
function buildSelectOptions($name, array $options, $current=null){
	$htmlString  = "<select name='{$name}' id='{$name}'>\n";
	
	foreach($options as $value=>$option){
		$htmlString .= "\t<option value='{$value}'";
		if($value == $current){
		$htmlString .= " selected";
		}
		$htmlString .= ">{$option}</option>\n";
		}
		$htmlString .= "</select>\n";
		return $htmlString;
		}
		
function getEvents($conn){
		$qry = "SELECT id ,Event_Type FROM events ORDER BY id";
		$sql = mysqli_query($conn, $qry)or die(mysqli_error($conn));
		if(mysqli_num_rows($sql) < 1){
		return array();
		}
		while($res1 = mysqli_fetch_array($sql)){
		$ret1[$res1['id']] = $res1['Event_Type'];
		
		}
		
		return $ret1;
		}
		
function getMoods($conn){
		$qry = "SELECT id ,Event_Sub_Type FROM Event_Sub ORDER BY id";
		$sql = mysqli_query($conn, $qry)or die(mysqli_error($conn));
		if(mysqli_num_rows($sql) < 1){
		return array();
		}
		while($res2 = mysqli_fetch_array($sql)){
		$ret2[$res2['id']] = $res2['Event_Sub_Type'];
		}
		
		return $ret2;
		}
		
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!--Design by Free CSS Templates
http://www.freecsstemplates.org
Released for free under a Creative Commons Attribution 2.5 License

Name       : Yosemite
Description: A two-column, fixed-width design with dark color scheme.
Version    : 1.0
Released   : 20091106-->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="keywords" content="" />
<meta name="description" content="Input form for getting Verse output" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>1066 Cards 4U - Verse Input Form</title>
<link href="style.css" rel="stylesheet" type="text/css" media="screen" />
</head>
<body>
<div id="wrapper">
<div id="menu">
<ul>
<?php
include ("connect_menu_LOCAL.php");
$conn = get_db_conn_Menu();
$menu_items['Url']="";
$menu_sql = "SELECT Url FROM dtop";
$menu_res = mysqli_query($conn, $menu_sql) or die(mysqli_error($conn));
if (mysqli_num_rows($menu_res) === false) {
	echo "<p>".mysqli_error($menu_res)."</p>";
}
else {
while($menu_items =  mysqli_fetch_array($menu_res)){ 
	$item=$menu_items['Url'];
echo $item;
}
}
//free results
	mysqli_free_result($menu_res);

//close connection to MySQL
	mysqli_close($conn);
?>
</ul>
</div><!-- end #menu -->
<div id="header">
<div id="logo">
<h1><a href="http://www.1066cards4u.co.uk">1066 Cards 4U</a></h1>
</div><!-- end #wrapper -->
</div><!-- end #header -->
<div id="page">
<div id="page-bgtop">
<div id="page-bgbtm">
<div id="content">
<h1>Verse Choices Form</h1>
<p>
<br></br>
<br></br>
You can filter the database, by selecting one of the items in each drop down menu and clicking the submit button.<br></br>
This will list all greetings of the chosen type of Event and Mood.<br></br>
																	If you do not make a choice the default selections will be used.</p>
<br></br>
<h3>BASIC VERSION</h3>
<br></br>
<!-- Start of FORM -->
<form action="VerseQuery.php" method="post">
<?php
	echo buildSelectOptions('event', getEvents($conn), 1);
	echo buildSelectOptions('mood', getMoods($conn), 27);
?>
	<input type="submit" value="submit"/>
</form>
</form><!-- End of FORM -->
<br></br>
<h3>ADVANCED VERSION</h3>
<p>If you wish to change the Font color to any color, use the following button</p>
<br></br>
<!-- Start of FORM -->
<form action="AltVerseQuery1.php" method="post">
	<?php
	echo buildSelectOptions('event', getEvents($conn),1);
	echo buildSelectOptions('mood', getMoods($conn),27);
?>
	<input type="submit" value="submit"/>

</form><!-- End of FORM -->

<br></br><br></br>
<form method="post" action="Verse_Menu.php">
	<input type="submit" value="Return To Verse Menu"/>
</form>

</div><!-- end #content -->

<div id="sidebar">
	
<div id="search" >
<form method="get" action="http://1066cards4u.co.uk">
<div>
<input type="text" name="s" id="search-text" value="" />
<input type="submit" id="search-submit" value="GO" />
</div>
							</form>
</div>
<div style="clear: both;"> </div>
<h2>Useful Links</h2>
<ul.a>
	<li><p><a href="Articles/Instructions for Using Verse Database-V2.12.pdf" target="_blank">Insructions for  using Verse Printing UtilityPrintable PDF Version</a></p></li>
	<li><p><a href="Articles/Nominal Settings.PDF" target="_blank">Nominal Settings and Cutting List for Verse Database PDF Version</a></p></li>
	<li><p><a href="Articles/How to use the Color Chooser_rasterized.PDF" target="_blank">How to use the Color Picker</a></p></li>
</ul.a>

		</div><!-- end #sidebar -->
		<div style="clear: both;"> </div>
</div><!-- end #page-bgtop -->
</div><!-- end #page-bgbtm -->
</div><!-- end #page -->
</div>

<div id="footer">
	<p>Copyright (c) 2008 Sitename.com. All rights reserved. Design by <a href="http://www.freecsstemplates.org/" rel="nofollow">FreeCSSTemplates.org</a>.</p>
</div><!-- end #footer -->
</body>
</html>

This problem is driving me crazy!!

I hope you can spot something amiss!

Link to post
Share on other sites

You're calling buildSelectOptions() with the second $conn - are you sure that it still contains the desired database connection?

 

As Barand pointed out, you're including 2 separate connection files and overwriting the value of $conn with each. So, at first, $conn is the results of get_db_conn_verse(), then - right before the calls to buildSelectOptions() - the results of get_db_conn_Menu().

Link to post
Share on other sites

the problem is line 88 in your REAL code, along with including/defining/making two database connections.

 

the code in post #24, that you stated was your real code, had lines 69-89 removed. it turns out on line 88 you are closing the database connection, so, any use of the database after that point will produce the error in question.

 

rather than to piece together blocks of code to build your web page, you need to organize your program logic so that any database specific code is grouped together and it all uses a single database connection. see the following recommended layout - http://forums.phpfreaks.com/topic/297824-database-issues-and-working/?do=findComment&comment=1519095 either don't close your database connection and let php close it for you or close it after the point where you have finished with database queries.

Link to post
Share on other sites

Right!

I changed as you suggested and guess what the dropdown lists were shown with defaults selected.

Great I thought ;D

But :

Now when I click on an Event and a Mood nothing is posted to the following script.

Ahh! i thought, somehow during the multiple postings it was suggested that I remove the option tags, so I modified the script.

Now no lists at all.

So i seemed to have gone around in circles and landed up where I started.

 

How can I get the value that the user selects from the list to be posted for the next script?

Do you add another parameter to the function?

I'm afraid I am a little out of my depth to modify this so I will leave it to you experts to suggest a solution.

Link to post
Share on other sites

you need to determine what your script is doing, in order to pin down where and what the problem is, before you can fix it. just randomly changing things without any purpose or plan won't produce any result.

 

is the html markup correct for the form, the select menu, and option choices within the select menu?

Link to post
Share on other sites

The buildSelectOptions() function will create a full, well-formed HTML select element. That element needs to be inside form tags with an appropriate method attribute. Then, the script that will receive the form data (specified by the form's action attribute) will use the form's method (the code from #37, post) to get the values. So, in VerseQuery.php, you can get mood like so:

$mood = $_POST['mood'];

Now, $mood will be the value selected by the user.

Link to post
Share on other sites

I know what what the script is supposed to be doing and the previous version of the script works OK, but I wanted the selection lists to:

(a) to show a value from the list and not a blank line.

(b) To be able to choose what value in the list is shown as default [as an html select list can do]

© be able to post the user selection for use in the following script.

 

I am not sure if it is me not understanding  you or you not understandin me!

 

The buildseleletoptions dropdown list is supposed to replace an html selection options list, but generated from a DBase.

 

As I explained above if I put the options tags around the php code I loose the list.

 

If you run the following you will see the result: www.1066cards4u.co.uk/alt_inputform6.php

 

If you want to see the way the live site is running at present, just select the Verses tab, then the Verse Printing Utility button and make some choices to follow the program thro.

Link to post
Share on other sites

we know what you are trying to do, the point is, in order for you to do it, and not just us fixing each thing that's wrong with your code, you must know what the output is supposed to be and when it isn't the correct output, to debug what your code is doing to find why it isn't the expected output.

 

i recommend that you look at the 'view source' of your page in your browser. you will see a php/mysql error at the bottom that would help you determine why it currently isn't working. if you had organized your code as suggested in post #40 in this thread, with any database dependent code separate from any html markup, the php error wouldn't be buried in the 'view source'.

Link to post
Share on other sites

I did not know until now, how useful view source can be for debuging scripts.

I could see how the script had generated the html select code and then after reading maxxd's post, it dawned on me why the following script was not producing any results.  I was assuming that the Form Name was as I had defined it, but seeing the source code revealed to me that the function was already generating that as event and mood and posting these values.

 

So the script on post #37 is the one that was pretty much as the final script.

Link to post
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.