Haggis1990 Posted December 3, 2012 Share Posted December 3, 2012 Hey, I've got a problem with a generated form out of a mysql database. The look of the output turns out well, but it looks like the form action is directly shoot when the page is loading so the data in the database is changed directly and not when the checkbox is clicked. Hopefully it is a bit clear, or the code may actual help you a bit: <?php class Item { var $db; public function __construct() { require_once('database.php'); $this->db = new Database('localhost', 'root', '', 'todo'); } public function getItemList() { $id = 0; $this->db->getItems(); if ($this->db->numRows() == 0) { echo 'Geen taken'; } else { foreach($this->db->Items() as $taken) { $id = $id + 1; $name = "item".$id; if ($taken['Klaar'] == 1) { echo '<form method="post" action="'.$this->changeItem($name, $id).'">'; echo '<input type="hidden" name="'.$name.'" value="0">'; echo '<input type="checkbox" name="'.$name.'" value="1" checked="checked" onchange="submit();">'; echo $taken['Omschrijving']; echo '</form>'; } else { echo '<form method="post" action="'.$this->changeItem($name, $id).'">'; echo '<input type="hidden" name="'.$name.'" value="0">'; echo '<input type="checkbox" name="'.$name.'" value="1" onchange="submit();">'; echo $taken['Omschrijving']; echo '</form>'; } } echo '<p>', $this->db->numRows(), ' taken</p>'; } } public function changeItem($name, $id) { if (isset($_POST[$name])) { $this->db->Itemsklaar(1, $id); } else $this->db->Itemsklaar(0, $id); } } ?> It looks like the form action is shoot by loading the page, and with the checkbox not generated yet there is going the wrong data into the database. Hopefully someone can help me out. Quote Link to comment https://forums.phpfreaks.com/topic/271532-problem-with-php-generated-form/ Share on other sites More sharing options...
Muddy_Funster Posted December 3, 2012 Share Posted December 3, 2012 PHP can't "soot" a form by it's self, by the time the form makes it to the browser php is done with it and can no longer interact with the page elements directly - you need a client side language like Javascript to do that. The issue must be with some controll logic elsewhere in your script. without access to all your classes and functions we won't be able to narrow it down much further than that. Quote Link to comment https://forums.phpfreaks.com/topic/271532-problem-with-php-generated-form/#findComment-1397196 Share on other sites More sharing options...
Haggis1990 Posted December 3, 2012 Author Share Posted December 3, 2012 Other two files i'm working with are: Index.php <!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=utf-8" /> <title>Naamloos document</title> </head> <body> <?php include('classes.php'); $nItem = new Item; echo $nItem->getItemList(); ?> </body> </html> and database.php <?php class Database { protected $_link, $_result, $_numRows; public function __construct($server, $username, $password, $db) { $this->_link = mysql_connect($server, $username, $password); mysql_select_db($db, $this->_link); } public function disconnect() { mysql_close($this->_link); } public function getItems() { $this->_result = mysql_query('Select ID, Omschrijving, Klaar From todo_item', $this->_link); $this->_numRows = mysql_num_rows($this->_result); } public function numRows() { return $this->_numRows; } public function Items() { $items = array(); for ($x = 0; $x < $this->numRows(); $x++) { $rows[] = mysql_fetch_assoc($this->_result); } return $rows; } public function Itemsklaar($klaar, $id) { $sql = "UPDATE todo_item SET Klaar = '".$klaar."' WHERE ID = ".$id.";"; if (!mysql_query($sql,$this->_link)) { die('Error: ' .mysql_error()); } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/271532-problem-with-php-generated-form/#findComment-1397201 Share on other sites More sharing options...
Muddy_Funster Posted December 3, 2012 Share Posted December 3, 2012 right, so you are calling $this->changeItem() when writing the form. $this->changeItem() in turn calls $this->db->Itemsklaar() with one of two values. $this->db->Itemsklaar() performs an update to the database with the values given. cutting it down - your script is written so as to perform an update on the database when you are building the forms. The forms themselves are not pert of the problem. Quote Link to comment https://forums.phpfreaks.com/topic/271532-problem-with-php-generated-form/#findComment-1397204 Share on other sites More sharing options...
Haggis1990 Posted December 3, 2012 Author Share Posted December 3, 2012 Ah, I know what I'm doing wrong with thanks to your post As you say: echo '<form method="post" action="'.$this->changeItem($name, $id).'">'; will fire the function 'changeItem' offcourse @ page loading. But how can I change that line so it will just fire when the checkbox is changed? Quote Link to comment https://forums.phpfreaks.com/topic/271532-problem-with-php-generated-form/#findComment-1397210 Share on other sites More sharing options...
Muddy_Funster Posted December 3, 2012 Share Posted December 3, 2012 just leave the action empty or make it action="#". then let your if(isset($_POST['name'])) do it's job Quote Link to comment https://forums.phpfreaks.com/topic/271532-problem-with-php-generated-form/#findComment-1397214 Share on other sites More sharing options...
Haggis1990 Posted December 3, 2012 Author Share Posted December 3, 2012 The problem is now for me that the if(isset($_POST['name'])) is in a function which need two arguments. So I think I need some AJAX scripting for it, but i'm not sure of that? Quote Link to comment https://forums.phpfreaks.com/topic/271532-problem-with-php-generated-form/#findComment-1397216 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.