6 Replies - 1390 Views - Last Post: 25 March 2013 - 09:33 AM Rate Topic: -----

#1 DocNet  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 11-October 12

Listbox doesn't show selected value?

Posted 25 March 2013 - 07:30 AM

Hi, i'm trying to delete data from database and i have some problem.

In php/engine.php when i try to echo selected value from listbox in cms.php file, nothing's gona show? Why?

cms.php code:
<form method="post" action="php/engine.php" enctype="multipart/form-data">
  <select name="list" size="29" id="list">
  <?php
  Manage::Select();
  ?>
  </select>
  <input type="submit" name="delete" class="delete_btn" value="delete"/>
</form>



php/engine.php file
<?php
error_reporting(E_ALL);
ini_set('display_errors', '0');
include("connect.php");

//this part is important
if(isset($_POST['list']))
{
	if(isset($_POST['delete']))
	{
		echo $_POST["list"];
		/*$rez = mysql_query("delete from slike where slike_id='$del'");
		if($rez) header('Location: ../index.php');
		else echo mysql_error();*/
	}
}

class Manage{
	public static function Select(){
		$query = mysql_query("SELECT naslov FROM slike");
		if($query)
		{
			while($row=mysql_fetch_array($query)){
				echo '<option value="'.$row['slike_id'].'">'.$row['naslov'].'</option>';
			}
		}
		else{
			die(mysql_error());
		}
	}
	public static function Select_Slike(){
		$query = mysql_query("select * from slike");
		if($query)
		{	
			while($row = mysql_fetch_array($query)){
			echo '<div id="block1"><h5>'.$row['naslov'].'</h5><img src="images/'.$row['string'].'.jpg" class="slike"/></div>';
			}
		}
		else{
			die(mysql_error());
		}
	}
}
?>




Is This A Good Question/Topic? 0
  • +

Replies To: Listbox doesn't show selected value?

#2 Atli  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3712
  • View blog
  • Posts: 5,964
  • Joined: 08-June 10

Re: Listbox doesn't show selected value?

Posted 25 March 2013 - 08:17 AM

Hey.

Have you tried printing the POST data to see exactly what data the browser is sending PHP? Do something like:
if (!empty($_POST)) {
    var_dump($_POST);
}


This will print everything sent via POST, so you can see exactly what data you have, which will give you clues as to why your code is not workign as expected.


Also, on a side-note. Speaking from experience, it's rarely a good idea to physically DELETE data from a database. More often than not, you're better of just flagging rows as "hidden" or "removed" using a boolean field, and then filter those out in whatever SELECTs you normally use. This makes it far less likely that something goes wrong and you accidentally delete to much, and it gives you the option to restore deleted data if needed. (You never know when you may actually need to do that. Even if you don't see a need for it now, you may in the future.)
Was This Post Helpful? 1
  • +
  • -

#3 Anthonidas  Icon User is offline

  • D.I.C Head

Reputation: 30
  • View blog
  • Posts: 218
  • Joined: 25-April 11

Re: Listbox doesn't show selected value?

Posted 25 March 2013 - 08:22 AM

i don't see any problems in your highlighted part...

try to print_r() your $_POST and look if something is printed out.
Was This Post Helpful? 0
  • +
  • -

#4 DocNet  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 11-October 12

Re: Listbox doesn't show selected value?

Posted 25 March 2013 - 08:23 AM

View PostAtli, on 25 March 2013 - 08:17 AM, said:

Hey.

Have you tried printing the POST data to see exactly what data the browser is sending PHP? Do something like:
if (!empty($_POST)) {
    var_dump($_POST);
}


This will print everything sent via POST, so you can see exactly what data you have, which will give you clues as to why your code is not workign as expected.


Also, on a side-note. Speaking from experience, it's rarely a good idea to physically DELETE data from a database. More often than not, you're better of just flagging rows as "hidden" or "removed" using a boolean field, and then filter those out in whatever SELECTs you normally use. This makes it far less likely that something goes wrong and you accidentally delete to much, and it gives you the option to restore deleted data if needed. (You never know when you may actually need to do that. Even if you don't see a need for it now, you may in the future.)


Thanks for reply, i never before used "var_dump()" function,

i type this:

if(isset($_POST['list']))
{
	if(isset($_POST['delete']))
	{
		echo $_POST["list"];
		if(!empty($_POST))
		{
			var_dump($_POST);
		}
		
		/*$rez = mysql_query("delete from slike where slike_id='$del'");
		if($rez) header('Location: ../index.php');
		else echo mysql_error();*/
	}
}



and output in browser is:

array (size=2)
  'list' => string '' (length=0)
  'delete' => string 'delete' (length=6)


Was This Post Helpful? 0
  • +
  • -

#5 Atli  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3712
  • View blog
  • Posts: 5,964
  • Joined: 08-June 10

Re: Listbox doesn't show selected value?

Posted 25 March 2013 - 08:51 AM

OK, from that you can see that the "list" element is being sent, but it has an empty string as a value. So it is in fact being printed correctly, there just isn't anyhing to print.

The probable cause of this is some problem in the way the <select> element is set up. How does it look in the browser source? What exactly is your Manage::Select(); function printing?
Was This Post Helpful? 1
  • +
  • -

#6 DocNet  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 11-October 12

Re: Listbox doesn't show selected value?

Posted 25 March 2013 - 09:28 AM

View PostAtli, on 25 March 2013 - 08:51 AM, said:

OK, from that you can see that the "list" element is being sent, but it has an empty string as a value. So it is in fact being printed correctly, there just isn't anyhing to print.

The probably cause of this is some problem in the way the <select> element is set up. How does it look in the browser source? What exactly is your Manage::Select(); function printing?


I solved my problem.

In my select query, i typed just "select naslov from table", and as option value i used slike_id,
so i used column which is not selected, so i just rename my query in "select * from table" and now works fine.

Thank you guys for assist.

This post has been edited by DocNet: 25 March 2013 - 09:29 AM

Was This Post Helpful? 0
  • +
  • -

#7 Atli  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3712
  • View blog
  • Posts: 5,964
  • Joined: 08-June 10

Re: Listbox doesn't show selected value?

Posted 25 March 2013 - 09:33 AM

Glad you figure it out.

However, you should avoid using the * wildcard in SELECT queries. Fetching all the fields when you only need two of them is a waste. You would be better of just doing:
SELECT slike_id, naslov FROM table

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1