I have some view in a php project. I call a method by HTML submit button, but the method isn't run and I don't understand why. The method contains a query what is uploading 2 text to a SQL database.
Here is my method:
public function uploadCtrl() {
if(isset($_POST['send_note'])){
$title =$_POST['title_of_note'];
$text =$_POST['text_of_note'];
$id = $_SESSION['user_logged_status'];
var_dump($title, $text, $id);
$database->noteToDatabase($title, $text, $id);
}
here is the view:
public function uploadNote(){
?>
<form action='' method='post'><br>
Title:<input type='text' name='title_of_note'><br>
Text:<input id='long_text' type='text' name='text_of_note'><br>
<input type='submit' name='send' value='SEND'><br>
<a href='?send_note'>Note save</a>
</form>
<?}
and the query method:
public function noteToDatabase($title, $text, $id){
$stmt = $this->conn->query("INSERT INTO note (title, text,user_id) VALUES ('$title','$text','$id')");
var_dump($stmt);
}
I call the method here:
if(isset($_POST['new_note'])){
$ctrl = new note_ctrl();
$ctrl->uploadNoteToDatabase();
// $ctrl->uploadCtrl();
}
I checked the post method using Mozilla Firebug and it looked okay, but this method never runs.
try mentioning the method name in action
parameter of form
tag
form action="note_ctrl/uploadCtrl" method="post"
Thanks
Are you using any framework?
Use $_POST['send']
instead of $_POST['send_note']
public function uploadCtrl(){
if(isset($_POST['send'])){
If your methods are in one class. You can try to call your uploadCtrl()
method in view.
public function uploadNote(){
if(isset($_POST['send'])){$this->uploadCtrl()}
?>
<form action='' method='post'><br>
Title:<input type='text' name='title_of_note'><br>
Text:<input id='long_text' type='text' name='text_of_note'><br>
<input type='submit' name='send' value='SEND'><br>
<a href='?send_note'>Note save</a>
</form>
<?
}
If view is in another class, you need to make your method public function uploadCtrl(){
static like public static function uploadCtrl(){
, so then you can call it in view like note_ctrl::uploadCtrl()