Undefined error and url issues with htaccess

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.

your RewriteRule does not match. AFAIK all paths start with a /.

RewriteRule works perfectly well when I call a page like below. I don’t think that’s the problem, but thank you.

site.com/book/6
site.com/book/10

etc.

EDIT : I solved my problem by passing bookid via AJAX, but I still don’t get why I can’t pass it via PHP using this snippet.

Because thing_id never got into the query string.

There are a couple ways you could get thing_id into the query string. One is to go through the /book/6 style URLs, and the rewrite rules will add thing_id to the query string. But your Ajax isn’t using those URLs. Instead the request is going straight to process.php, which means your Ajax request has to put thing_id in the query string on its own. It sounds like you solved your problem this second way.

1 Like

Yes, exactly.

Thanks for the explanation!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.