0

This may be a no brainer for some of you but I'm really scratching my head on this one. I started using CakePHP and got hooked on it but it is different than regular PHP (they use some shorthand for a lot of coding). At any rate, I'm trying to update my database when a button is pressed without having the user to enter in new data in a form. Currently I'm using AJAX and a button tag that calls the javacript function. It's supposed to be as if you are registering for a class that will be added to "My Courses"later (populated by the enrolment [sic] table in the database). This is the AJAX code:

<script>
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
  {
    document.getElementById("alert").innerHTML=xmlhttp.responseText;
  }
}
xmlhttp.open("POST","/Enrolments/register.php",true);
xmlhttp.send();
}

</script>

In hindsight it seems silly to need the if statement that checks for IE6 and below but that was just the W3 recommendation. This is what what follows for the button:

<button onclick="loadXMLDoc()">Register</button>

And of course there is some content on the page (a video to preview the course). This is what the register.php looks like:

<?php
$user = $this->Session->read('Auth.User');
$id = $user['user_id'];
$this->Status->read(null, $id);
$this->Status->set('course_id', 1);
$this->Status->save();
echo "You are now registered for this course";

?>

I didn't add a look up for the correct course_id to be added. I was just trying to test it by passing a "1" for that field (currently the enrolment [sic] table has only two columns- user_id and course_id) to see if it would actually update the database. Presently, it does not. Any advice would be great.

Edit: To be clear, I'm trying to make it where the enrolments table is updated with the following information upon clicking "Register": user_id = the current user_id, course_id = 1.

laser_dan
  • 3
  • 2
  • This is weird, you said you work with CakePHP but you also say you make a request to "/Enrolments/register.php", that is wrong. In CakePHP (and all MVC frameworks out there) you make a call to a controller/action – Guillermo Mansilla Nov 29 '13 at 16:10
  • Hey Guillemo, as stated below, I've tried making the call to the controller/action. I changed it to "register.php" because I thought maybe I missed something with this CakePHP stuff (which I'm still pretty new to). – laser_dan Nov 29 '13 at 16:19
  • So what happens when you use `xmlhttp.open("POST","/Enrolments/register",true);` ? Can you access `$this->request->data` from within `EnrolmentsController::register()`? – iso27002 Nov 29 '13 at 18:14

1 Answers1

0

From @Guillermo comment this seems not to be cakephp's way to do it, however when using post:

xmlhttp.open("POST","/Enrolments/register.php",true);

you need to at least set this header also:

http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

But if you can live with the character limits of a simple get, just use it:

xmlhttp.open("GET","/Enrolments/register.php",true);
Community
  • 1
  • 1
  • In CakePHP you don't call a php file like that, you should be calling a controller/action – Guillermo Mansilla Nov 29 '13 at 16:12
  • I've tried using register.ctp (also defined in the enrolments controlller) as well as "GET" and it still doesn't work. – laser_dan Nov 29 '13 at 16:15
  • and of course it won't work! you can't request a view, the view is rendered by the controller. Take a look [at the book](http://book.cakephp.org/2.0/en/getting-started.html) – Guillermo Mansilla Nov 29 '13 at 16:21
  • Well no, I'm not saying it's coded as "register.ctp" in the function. It just says "register" which is defined in the EnrolmentsController. There is, however a view associated with it. – laser_dan Nov 29 '13 at 16:23
  • I've actually spent a copious amount of time reading the book, though I do appreciate you posting a helpful source! – laser_dan Nov 29 '13 at 16:23
  • It's still not working but there seems to be something amiss about the way CakePHP handles XML at all (it sucks at it). This is the only answer so thank you for trying. – laser_dan Dec 01 '13 at 21:16