0

I tried everything and I'm losing my mind

    
    function submitItem(){
        textarea = $("#textarea").val();
        status = $("#status").val();
        category = $("#categories").val();
        date = $("#date").val();
            
        var ajaxReq = new XMLHttpRequest();
        ajaxReq.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                document.getElementById("message").innerHTML = this.responseText;
            }
        }
        ajaxReq.open("POST","../php/addItem.php",true);
        ajaxReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        ajaxReq.send("textarea="+textarea+"&category="+category+"&status="+status+"&date="+date);
    }
AJAX REQUEST
<?php
session_start();
require("server.php");
$cnx = new mysqli($server_name,$server_username,$server_password,$db);

$category = $_POST["category"];
$item = $_POST["textarea"];
$date = $_POST["date"];
$status = $_POST["status"];
$userID = $_SESSION["userID"];

$searchForCategory = "SELECT * FROM categories where categoryname = \"".$category."\"";
$result = $cnx->query($searchForStatus);
$row = $result->num_rows;

echo $row;

?>

And I tried everything, when I put and actual name it's working fine. I echo-ed the $category to see if it's working and it's working fine. I have no idea what the error might be

5
  • What does var_dump($category); show? Commented Nov 18, 2017 at 1:11
  • C:\wamp64\www\first\php\addItem.php:17:string ' ass ' (length=5) weird the word ass has the length of 5.. why do you think that happened Commented Nov 18, 2017 at 1:16
  • There are spaces at the beginning and end. Commented Nov 18, 2017 at 1:17
  • What kind of input is #categories? Commented Nov 18, 2017 at 1:18
  • thankyou so much.. if I knew you i'd hug you, I've been trying for hours Commented Nov 18, 2017 at 1:22

1 Answer 1

1

The textarea probably contains some special characters, which you aren't encoding properly. Use $.post, which will encode the data properly.

function submitItem() {
    $.post("../php/addItem.php", {
        textarea: $("#textarea").val(),
        category: $("#categories").val(),
        status: $("#status").val(),
        date: $("#date").val()
    }, function(response) {
        $("#message").html(response);
    });
}

If there's some reason you don't want to do this, use the encodeURIComponent() function around each parameter, e.g.

"textarea=" + encodeURIComponent(textarea) + "&category=" + encodeURIComponent(category) + ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.