Combobox, PHP and MySQL

Ask about a PHP problem here.
Post Reply
techm3
Posts: 12
Joined: Wed Dec 28, 2011 3:34 pm

Combobox, PHP and MySQL

Post by techm3 »

Hi there,
I'm populating a combobox with values stored in a database.
When I try to submit the form again the combobox gets no value at all.
<?php
$errors	= array();
	if(isset($_REQUEST['mode']) && $_REQUEST['mode'] == "send"){
		if (empty($_POST['touser']))
			$errors[] = "Seleccionar destinario.";
		if (empty($_POST['body'])){
			$errors[] = "Redacte cuerpo de mensaje.";
		}
	
		if(empty($errors)){
			$from 	= $_SESSION['username'];
			$subject= $_POST['subject'];
			$mess	= $_POST['body'];
			$to	= $_POST['touser'];
			$priority= $_POST['priority'];
		
			send_mess($from, $subject, $mess, $to, $priority);
		}
// Part of the form
$result = mysql_query("SELECT `username`,`name` FROM `usersInfo`");
   if (mysql_num_rows($result)!=0){ 
    echo '<select name="touser" id="touser"> 
          <option value=" " selected="selected">Choose one</option>'; 
           while($touser = mysql_fetch_array( $result )){ 
              echo '<option value="'.$touser['username'].'">'.$touser['name'].'</option>'; 
            } 
    echo '</select>'; }
?>
Is there any way I can get this working?
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: Combobox, PHP and MySQL

Post by Helx »

I have no clue how to fix it, but I copy/pasted this into DreamWeaver and line 25 seems to be the point of trouble :)
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Combobox, PHP and MySQL

Post by jacek »

It looks like it should work as far as I can tell. Could you post the source of the created page ? It could be that something is making a mess of the HTML.
Image
techm3
Posts: 12
Joined: Wed Dec 28, 2011 3:34 pm

Re: Combobox, PHP and MySQL

Post by techm3 »

Here's the page code.
<?php
	include('../example/other.php');

	$users_info	= fetch_users_info($_SESSION['username']);

	if(!isset($_SESSION['username'])){
		header('Location: http://example.com');
		die();
	}

	$errors	= array();
	if(isset($_REQUEST['mode']) && $_REQUEST['mode'] == "send"){
		if (empty($_POST['touser']))
			$errors[] = "Seleccionar destinario.";
		if (empty($_POST['body'])){
			$errors[] = "Redacte cuerpo de mensaje.";
		}
	
		if(empty($errors)){
			$from 	= $_SESSION['username'];
			$subject= $_POST['subject'];
			$mess	= $_POST['body'];
			$to	= $_POST['touser'];
			$priority= $_POST['priority'];
		
			send_mess($from, $subject, $mess, $to, $priority);
	
			header('Location: http://example.com/');
			die();
		}
	}else{
		header('Location: http://example.com');
		die();
	}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
	<title>Redactar Mensaje | Example</title>
	<link rel="stylesheet" href="/css/core.css" type="text/css" />
</head>

<body>
<div id="wrapper">

	<div id="sitename" class="clear">
		<img src="#" />
	</div>
	
	<div id="navbar">	
		<div class="clear">
			<ul class="sf-menu clear">
            	<!-- PAGE NAVIGATION -->
				<li><a href="#"><span>inicio</span></a></li>
				<li><a href="#"><span>estadísticas</span></a></li>
				<div class="pl9">
					<li><a href="#"><span><b>desconectar</b></span></a></li>
				</div>
			</ul>
		</div>
	</div>
	
	<div id="header" class="clear">
		<div class="header-text">
			<h2><strong>Redactar</strong> Mensaje</h2>			
		</div>
	</div>
	<div class="header-bottom"></div>
	
	<div id="body-wrapper">
	
		<!-- BREADCRUMB NAVIGATION -->
		<div class="bcnav">
			<div class="bcnav-left">
				<div class="bcnav-right clear">
					<h3>Hola <?php echo $_SESSION['username']; ?>,</h3>
				</div>
			</div>
		</div>

		
		<div id="body" class="clear">			
			<div class="clear">				
				<div class="column column-445 column-left">
			
					<form action="" method="POST">
						<label for="subject"><b>Título Mensaje</b></label><br>
						<input type="text" name="subject" value="<?php echo $_REQUEST['subject'];?>"/><br><br>
						<label for="body"><b>Cuerpo de Mensaje</b></label><br>
						<textarea type="text" name="body" col="28" rows="10" title="Your Message" class="short_text"><?php if (isset($_POST['body'])) echo htmlentities($_POST['body']); ?></textarea><br><br>
						<input type="submit" value="Enviar" />
					</form>
				</div>

				<div id="sidebar" class="column column-445 column-right">
						
						<label for="touser"><b>Destinario</b></label><br>
<?php 
$result = mysql_query("SELECT `username`,`name` FROM `users`");
    if (mysql_num_rows($result)!=0){ 
    echo '<select name="touser" id="touser"> 
          <option value=" " selected="selected">Seleccionar Destinario</option>'; 
           while($touser = mysql_fetch_array( $result )){ 
              echo '<option value="'.$touser['username'].'">'.$touser['name'].'</option>'; 
            } 
    echo '</select>'; }
?>
				</div>
			</div><br><br><br>
			<div class="line"> </div><br>						
		</div>		
	</div>
	<?php include('../footer.php');?>

</div>
</body>
</html>
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Combobox, PHP and MySQL

Post by jacek »

No I mean if you view the page in your browser and look at the resulting HTML does it look broken ?
Image
techm3
Posts: 12
Joined: Wed Dec 28, 2011 3:34 pm

Re: Combobox, PHP and MySQL

Post by techm3 »

No, it doesn't. Everything looks like it supposed to be.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Combobox, PHP and MySQL

Post by jacek »

techm3 wrote:No, it doesn't. Everything looks like it supposed to be.
If all the options are there in the code then the only thing I can think of is that somethign is breaking the page somewhere so maybe try making sure the html is valid using http://validator.w3.org/
Image
Post Reply