1

I'm trying to pass my topic variable from this javascript function to my php file faqs.php. I know you can do this with a form, however this function is called when clicking text on the html page. I've tried AJAX but it didn't work for me and I feel like there must be a simpler way.

getFaqs function:

function getFaqs(topicId, topic) {
    $("#topic-container").html("");

    //insert javascript to send 'topic' to php file here

    $.getJSON("faqs.php", function(data) {
        if(data == "") {
            $("<div>", {class: "list-group-item", text: "Please add FAQs."}).appendTo($("#topic-container"));
        }
        $.each(data, function(faqId, faq){
            $("<div>", {id: "faq" + faqId, class: "list-group-item", text: faq}).appendTo($("#topic-container"));
        });
    });   
    return false; 
}

faqs.php:

<?php
header('Content-Type: application/json; charset=utf-8');

//insert some php to get 'topic' here

if(isset($_POST['topic'])){
    $topic=$_POST['topic'];
    $clean_topic = preg_replace('/\s+/', '', $topic);
}else{
    echo json_encode("Please enter a topic!");
}
$musicalinstruments = array("Question1"=>"What is the best musical instrument in the world?", "Answer1"=>"The English concertina", "Question2"=>"How many double bass players does it take to change a light bulb?", "Answer2"=>"None, the piano player can do that with his left hand");
$programminglanguages = array("Question"=>"Why do programmers confuse halloween and christmas?", "Answer"=>"Because Oct 31 = Dec 25");
$varietiesofpizza = array("Question"=>"Should I eat more pizza?", "Answer"=>"Yes. Always.");

echo json_encode ($topic);

?>
1
  • 1
    Can you add topics in JavaScript code? Commented Jan 8, 2017 at 20:20

4 Answers 4

1

Insert topic as GET variable in javascript

$.getJSON("faqs.php?topic=sometopic", function(data) {

And then read that GET variable in PHP

if(isset($_GET['topic'])){
Sign up to request clarification or add additional context in comments.

Comments

0

$.getJSON makes a GET request while $.ajax is the main component to send and communicate with the server. Here's a rough code that would do this just fine.

$.ajax({
    url: 'faqs.php', 
    type: 'POST',
    dataType: 'json',
    data: { topic: topics }, // topics is your topics varaible from the JS scope
    success: function(data) {
        if (data == "") {
            $("<div>", {
                class: "list-group-item",
                text: "Please add FAQs."
            }).appendTo($("#topic-container"));
        }
        $.each(data, function(faqId, faq) {
            $("<div>", {
                id: "faq" + faqId,
                class: "list-group-item",
                text: faq
            }).appendTo($("#topic-container"));
        });
    },
    error: function(xhr) {
        console.log('error :(');
    }
});

Comments

0

you can send it by url here is the code for it xmlhttp.open("GET", "phpFile.php?q=" + str, true); xmlhttp.send(); where q is he variable which is used to access the value of perticular variable ,str in this case, in php file

Comments

0

If you want use POST:

$.post('test.php', { name: "Anna" }, function(response) {
// Do something with the request, data is JSON object.
}, 'json');

And read in test.php in POST variable.

$_POST['name'] // Anna

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.