Hello,
My code is below, I just keep getting undefined error while making an ajax request.
Here’s my code.
htaccess
RewriteRule ^book/(\w+)/? contentpages/book/book.php?thing_id=$1 [NC,L]
I use a URL like this. site.com/book/6 (6 is the book ID.)
book.php
<?php
$root = $_SERVER['DOCUMENT_ROOT']; // root
require($root . "/includes/master.php"); // database class, etc.
require($root . "/contentpages/book/main.php"); // main.php gets book info. (author name, covers, etc)
?>
<!DOCTYPE html>
<html>
<body>
<div class="dropdown">
<div class="result"></div>
<div id="result"></div>
<button class="btn btn-danger dropdown-toggle add button-big-center" type="button" id="dropdownMenu1"
data-toggle="dropdown">Add
<span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1">
<li role="presentation"><a name="readit" id="readit" role="menuitem" tabindex="-1" href="#">Affirmative</a>
</li>
</ul>
</div>
</body>
</html>
main.php
$get_id = $_GET['thing_id'];
process.php
<?php $root = $_SERVER['DOCUMENT_ROOT']; // root require_once($root . "/includes/master.php"); // important stuff require_once($root . "/contentpages/book/main.php"); // I include it to get $get_id variable if (isset($_POST['readit'])) { $data = Array( 'user_id' => 1, 'progress_id' => 1, 'thing_id' => $get_id );
$id = $db->insert('usersxthing_progress', $data); if ($id) { echo 'user was created. Id=' . $id; } else { echo 'insert failed: ' . $db->getLastError(); }
}
?>
JS
$("#readit").click(function () {
var readit = $('#readit').text();
if (readit == "Affirmative") {
$.post(
"/includes/contentpages/process.php",
{readit: readit},
function (data) {
$(".result").hide().html(data).fadeIn(2000);
$(".add").hide().html("success!").fadeIn(1000);
});
}
});
I’m getting
Notice: Undefined index: thing_id in /Applications/MAMP/htdocs/contentpages/book/main.php on line 2
error, why? I include the main.php above (it contains the get request), so there shouldn’t be a problem, but there is. Should I use sessions for this?
If you need more detail, I’ll happily post more code. I tried to make it short.