0

I'm using PHP, but I have some variables that I'd like to post to another page using JavaScript. However, I cannot get it to work. The strange thing is that I use this exact same function on some other pages, so I can't figure out why it's doing this.

The function is simply an input button (linked to a person's name and ID). When the button is pressed, I'd like for the name and ID to be posted to another page.

function flagPost(replyID, userName){
  if(!confirm("Flag Post?"))
    return false;
  $.post('postprocessing.php',"replyID=" + replyID + "&username=" +
    userName + "&tableName=testTable&category=flagPost",
    function(response){
      window.location.reload()
    }
  );
}

I know replyID and userName are being set correctly, because when I write the variables to the page, they display correctly.

function flagPost(replyID, userName){
  document.write(replyID + userName)

I even created a new page just to make sure something on postprocessing.php wasn't throwing off the POST.

I made this page titled postprocessing2.php where this was the only thing on it (aside from including the config file and whatnot).

$category = $_POST['category'];
  if($category == "flagPost"){
    $table = $_POST['tableName']; //just testing, I use escape strings in the "real" code
    $postID = $_POST['replyID'];
    $username = $_POST['username'];
    mysql_query("insert into flaggedPosts VALUES('','$postID','$table','$username')");
  }
4
  • 4
    If you're reloading the page anyway, there is no point in using AJAX at all. Commented Jul 15, 2012 at 15:10
  • I agree with SLaks, but either way can you elaborate on "cannot get it to work"? Are there any errors? What is happening? It's worth pointing out also your code is an open invitation to SQL injection. Commented Jul 15, 2012 at 15:12
  • @Martin When I click the "OK" button nothing happens at all. It just sits there. On the other pages that use this funciton the page reloads (as indicated by window.location.reload()). I want the confirmation window via javascript and I wasn't sure how to POST the data any other way. If you could show me I'd really appreciate it. Commented Jul 15, 2012 at 15:25
  • @SLaks how can I post the information while still using javascript for the confirmation window? Can you please give me an example? Commented Jul 15, 2012 at 15:27

1 Answer 1

1

Replace"replyID=" + replyID + "&username=" + userName + "&tableName=testTable&category=flagPost"

with

$.post(url,{ replyID: replyID, username: userName, tableName: "testTable", category: "flagPost"},function...
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.