Jump to content

Problem With Php Generated Form


Haggis1990

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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());
}
}
}
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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