Good day! I'm new to PHP and I'm stuck on this. The problem is, how can I load an specific function from a class into the php file. This is all I got:
HTML FILE
<form action="myfile.php" method="post">
<input type="text" name="subject"/>
<input type="text" name="section"/>
<input type="submit" name="submit"/>
</form>
PHP FILE
<?php
include 'class_lib.php';
$subject = $_POST['subject'];
$section = $_POST['section'];
if(isset($_POST['submit'])){
$class = new myClass;
$success = $class->insertFunction();
if($success == TRUE)
{echo 'Inserted';}
else
{echo 'Error';}
}
?>
FUNCTION FILE
class myClass{
function insertFunction(){
$sql = "Insert into tblName (subject, section) VALUES($subject,$section)";
$success = mysql_query($sql) or die (mysql_error());
return $success;
}
}
The problem is when I submit the button it gives me a blank page. Please help me, i'm new with this so spare with me. Any help is much appreciated.
There are multiple ways to get the values available to the class-function,
lets try the quick fix -
1. Instead of - $success = $class->insertFunction();
write
$success = $class->insertFunction($subject, $section);
//Pass the parameters
and
class myClass{ function insertFunction($subject, $section){ //Get the parameter values [....] } }
Now check the value of $success
. If the query was successfully executed then the value should be the primary key (auto-increment). I hope the database table has such a field.
2. Now lets do it in the proper way -
Add two properties to the class file -
class myClass{
public $subject;
public $section;
function insertFunction(){
$sql = "Insert into tblName (subject, section) VALUES($this->subject, $this->section)";
return mysql_query($sql);
}
}
Then change the .php file as -
<?php
include 'class_lib.php';
[......] //include your class file.
$subject = $_POST['subject'];
$section = $_POST['section'];
if(isset($_POST['submit'])) {
$class = new myClass;
$class->subject = $subject;
$class->section = $section;
$success = $class->insertFunction();
if($success)
echo 'Inserted';
else
echo 'Error';
}
?>
I hope it gives some idea.