0

I have a problem, I want to get some data from a mysql database (which isn' t a problem). Let's say i want all data from the column "Ticketkosten"

$result = $wpdb->get_results( "SELECT * FROM prijzen");
foreach ( $result as $print ) { 
   $ticketkosten = $print->Ticketkosten;
}

Then i want to get all this data and pass all the data from "Ticketkosten" to a javascript funtion, which then makes a calculation and display the final prices. The reason I want to do this is because i use a "combobox" and when i change the value in this box the calculation has to be done again real time.

The javascript code i have right now is:

var aantal_boekingen= new Array();
 aantal_boekingen["option1"]=1;
 aantal_boekingen["option2"]=2;
 aantal_boekingen["option3"]=3;
 aantal_boekingen["option4"]=4;
 aantal_boekingen["option5"]=5;

var ticketkosten = 12; //THIS SHOULD BE THE MYSQL "TICKETPRIJZEN" DATA

var boekingskosten = 1;

var transactiekosten = 0.29;


function boekingen() {
    var aantalBoekingen=0;
    var theForm = document.forms["vergelijk_form"];
     var selectedBoekingen = theForm.elements["aantal_boekingen"];

    aantalBoekingen = aantal_boekingen[selectedBoekingen.value];

    return aantalBoekingen;
}

function calculateTotal() {
    var ticketprijs = ticketkosten + ((boekingskosten + transactiekosten) / boekingen());
    var totaalPrijs = ticketprijs / boekingen();

    var x = document.getElementsByClassName("totalPrice");
    var i;
    for (i = 0; i < x.length; i++) {
        x[i].innerHTML = "€"+totaalPrijs.toFixed(2);
    }
}

The ticketkosten which you see is now hardcoded, what i want is that this ticketkosten variable changes to the data of the database.

The javascript code is now working, but i want it to not be hardcoded.

So in short: I want to get the prices "Ticketkosten" out of the database, then pass it from php to javascript and make a calculation with these prices.

Can anyone help me out? I am stuck for more than a week now.

1 Answer 1

1

There are many solutions for this.

Where is your JavaScript code ? In a PHP file ? In a JS file ?

If it is in a PHP file you can do:

var ticketkosten = <?= $ticketkosten ?>;

Where $ticketkosten is the value from your database.

If you are in a JS file. You can get the value from a data-attribute like this:

var ticketkosten = document.getElementById('oneDomElement').getAttribute('data-ticketkosten');

Where oneDomElement can be whatever HTML element you want (hidden or not). It contains a data-attribute with the value from the database.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for your answer. Its all in the same PHP file, The php code is located in the body and the javascript after the footer. So the only thing to do is adding the var ticketkosten = <?= $ticketkosten ?>; ? My javascript skills are very basic, if i even get the basics:P
Yes, if your JavaScript code that define the var ticketkosten is after the PHP code that knows the value of the ticketkosten, you can easily do var ticketkosten = <?= $ticketkosten ?>;
Thanks, it works sort of. The only problem left is that it only get's the last value of the column, let's say 0,80 But not the 1,20 and 1,00
Yes that is because in your foreach you erase the value of the $ticketkosten variable. So it is only the last that will be set. You should add the value to an array so you can pass an array in your JavaScript and treat it like an array. Or do you want the $ticketkosten variable to be the sum of all your ticketkosten ?
Thanks, i got the data in an array. to use it properly i had to use a JSON array. I can see the data now, but can't make a calculation because It needs to be converted to an non json array
|

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.