Hi guys I'm having difficulties with trying to call a function when I use the html <form>
action attribute.
$cm = new customer_module();
$rm = new room_module();
$c_array = $cm->getByStatus('CHECKED-IN');
foreach($c_array as $row)
{
$temp = $row['room_no'];
$r_array = $rm->getByRoomNumber($temp);
$rray = $r_array[0];
echo(
$cm->checkOutCustomer($row['last_name'],$row['first_name']).
"<tr>".
"<td>".$row['room_no']. "</td>".
"<td>".$rray['type']. "</td>".
"<td>".$row['last_name']. "</td>".
"<td>".$row['first_name']. "</td>".
"<td>".$row['phone_no']. "</td>".
"<form action="$cm->checkOutCustomer($row['last_name'],$row['first_name']);">".
"<td>"."<Button>Check Out</button>"."</td>".
"</form>".
//"<td>"."<input type='button' name='checkingOut' value='Check Out' />"."</td>".
"</tr>");
}
As you can see, I'm trying to run the code $cm->checkOutCustomer($row['last_name'],$row['first_name'])
for the action attribute.
The code runs fine for the first line after my echo, but when I pasted it for action it seems there is a syntax problem with $cm
.
Please advise me what the problem is and thanks a lot!
PHP is evaluated by the server.
HTML is interpreted by the client.
Never the twain shall meet.
There are two options:
Move your function to a separate page. Set the form's action to that page. This would require some redirecting, to send the user back to the page with all of the table rows.
Move your function to a separate page. Use JavaScript to cancel the form's submission, and then use AJAX to send the data to that separate page. This kind of sucks, because you'll have to handle cross-browser events and AJAX, so I'd suggest a library, like jQuery.
This line is wrong.
"<form action="$cm->checkOutCustomer($row['last_name'],$row['first_name']);">".
This should work.
"<form action=" . $cm->checkOutCustomer($row['last_name'],$row['first_name']) . ">" .
Looks like a copy and paste error is all.
If you want that checkOutCustomer
method should execute when the form is submitted, then its not the correct way to implement. You need to specify a url in the action attribute. The form will be submitted to this url where you can access the form fields as the elements of $_POST or $_GET super globals (depending upon the method) and then process them.
But in case this method checkOutCustomer
returns a url that you want to set as the action attribute of the form, then
instead of
"<form action="$cm->checkOutCustomer($row['last_name'],$row['first_name']);">".
try
"<form action=" . $cm->checkOutCustomer($row['last_name'],$row['first_name']) . ">".
I don't see why @sdleihssirhc's answer was voted down when it was right, and the wrong answers got upvotes. PHP is evaluated server side - HTML is processed by the browser. This means your code currently checks out every user as they are listed. Not what you want? Didn't think so.
The action
attribute is NOT for executing server side functions on submits. You want to set it to a PHP file and have that PHP file do your processing. Try going over the basics of HTML forms again too, because you don't seem to understand.
Also try to create a Customer
class and just add a $customer->checkout()
method, and just do a search for active customer records (i.e. not checked out) and display info from those. And then do something like $customer = new Customer($roomNo, $info ...)
and then $customer->checkIn()
It just makes more sense to anyone maintaining the system. Anyway, from what I can see, you're trying to create a hotel management system. PHP isn't the best suited language for the job at hand and don't give any of that "only language i know" crap - learn something suited to the task, because lack of knowledge is no excuse for picking a bad language for the job at hand.
--
I won't give any working code apart from small things or pointers - you have to learn, rather than have others do and just copy / paste.