Reputation: 2018
I am making a PHP application, testing out Object Orientation with it as I am trying to learn OO as opposed to procedural programming.
Basically I have a list of courses the user can book using the system, and they appear in the users profile. From the profile I want a "remove" link next to each booked course which when clicked, removes the course.
Will I have to use a html form for this? I have started doing it but it doesnt seem right. I have a function called "delete_course" which does the stuff I want it to do, and all I need to pass over is the ID of the post in the DB. So... my question is... would it be best practice to post over the ID in a form to a page with the function call?
And if this is the best way, is it best to post it to the SAME (current) page the user is on, or post to another page, say, "cancel_course.php"??? What are the benefits?
Thanks
Oh and btw, I am relatively new to PHP but have done plenty of JS so I know ajax is a better option, just trying to learn PHP :)
Upvotes: 2
Views: 400
Reputation: 20875
Best option is using an ajax call (you stated you know it well) to remove the item from the profile, so you'll have two advantages
But, since you state you are new to PHP, my strongest advice is not to use plain PHP files to do this sort of things, especially if you require (like you should) a database. This way you may learn bad abhits (like using mysql extension functions instead of PDO), and really this knowloedge is neither truly object oriented, nor used in real life applications.
PHP developers (as Python and Ruby ones) nowadays use frameworks to build their applications. These frameworks often provide an OR mapping from database to PHP code and let you write real OO code, also easier to maintain thanks to the MVC paradigm.
Most of these frameworks are open source, so you can learn PHP stuffs by looking at the code. I personally recommend CakePHP.
Upvotes: 2
Reputation: 2408
You have some options to pick from, to name a few:
1) Via URL: Create hyperlinks like:
http://www.example.com/myprofile.php?deleteCourseid=23
http://www.example.com/myprofile.php?deleteCourseid=54
etc And from myprofile.php:
if (isset($_GET["deleteCourseid"]){
$deleteme = (int)$_GET["deleteCourseid"];
// Some query here.
}
2) Via POST in a form. Same logic applies.
I would advise you to also use some session logic to identify a user so you can delete the course based on $_GET["deleteCourseid"] AND something like $_SESSION["userid"]
Assuming you have some table like: [Postgresql]
CREATE tblusercourse(
usercourseid SERIAL PRIMARY KEY,
userid INTEGER REFERENCES tblusers(userid),
courseid INTEGER REFERENCES tblcourse(courseid),
)
Upvotes: 3
Reputation: 21899
If you want to POST a variable over HTTP without using JavaScript, you will need a form in your HTML, with a method attribute of 'post'.
Each delete button could have a value of the course's ID in the database which would make it easy for you to identify what course is being deleted in your PHP.
HTML:
<form method="post">
<ul>
<li>
<h1>Learn how to sew course</h1>
<button type="submit" name="deleteCourse" value="1">Delete</button>
</li>
<li>
<h1>Learn how to knit course</h1>
<button type="submit" name="deleteCourse" value="2">Delete</button>
</li>
</form>
PHP:
if(!empty($_POST["deleteCourse"])) {
$courseList->deleteById($_POST["deleteCourse"]);
}
Upvotes: 4
Reputation: 1258
If I understoop correctly, You have list of something and each something has it's own delete button.
For example, Latvian language course [Delete Course]
I find very simple, that [Delete Course] will have link to the same page with GET parameter, like, delete_course=$id. And then
if (isset($_GET['delete_course'])) {
$class->delete_course($_GET['delete_course']);
}
Upvotes: 2