0

I have a bargraph.html file and a seperate app.js file. I am trying to pass a variable named data which contains values in my bargraph.html and I want to pass it to my app.js file.

Part of my app.js file

function passfunction(data) {
            return data
            console.log(data)
        }

$(document).ready(function(){

    $.ajax({
        success: function(data) {
            console.log(passfunction.data);

Part of my bargraph.html file

<form method="POST" name="dataform" action="" id='dataForm'>
  <select id="data1" name="data1">
    <option value=""></option>
    <option value="DateRecorded">DateRecorded</option>
    <option value="InletVoltage">InletVoltage</option>
    <option value="InletCurrent">InletCurrent</option>
    <option value="ActivePower">ActivePower</option>
    <option value="ApparentPower">ApparentPower</option>
    <option value="PowerFactor">PowerFactor</option>
    <option value="SystemID">SystemID</option>
    </select>
    <select id ="data2" name="data2">
    <option value=""></option>
    <option value="DateRecorded">DateRecorded</option>
    <option value="InletVoltage">InletVoltage</option>
    <option value="InletCurrent">InletCurrent</option>
    <option value="ActivePower">ActivePower</option>
    <option value="ApparentPower">ApparentPower</option>
    <option value="PowerFactor">PowerFactor</option>
    <option value="SystemID">SystemID</option>
  </select>
  <button type="button" id="submitButton" name="submitButton">Submit</button>
 </form>

<script type="text/javascript">

$('#submitButton').click(function(e){



      var data1=$("#data1").val();
      var data2=$("#data2").val();

        $.ajax({
            type: 'POST',
            url: 'data.php',
            dataType: 'html',
            data: {data1:data1,data2:data2},
            success:function(data){ 
                passfunction(data);
                console.log(data);

In my bargraph.html, in console.log(data), it is displaying the right data that I want to use in my app.js file. However I am unable to pass it correctly to my app.js.

Currently when I run my code, in app.js in line console.log(passfunction.data);, it is displaying undefined in my console. It should be displaying the same data from bargraph.html console.log(data).

Why is this? What am I doing wrong? I need some assistance please

The suggested answer is not relevant to my question

Danny
  • 135
  • 8
  • is `app.js` included in bargraph.html? – devpro Feb 27 '19 at 10:44
  • @devpro yes app.js is included in my .html – Danny Feb 27 '19 at 10:44
  • is it dummy? `function passfunction(data) { return data console.log(data) }` – devpro Feb 27 '19 at 10:47
  • @devpro sorry don't know what you mean dummy? – Danny Feb 27 '19 at 10:48
  • where u define `data1` ?? `data2`? – devpro Feb 27 '19 at 10:50
  • According to your post from yesterday, your bargraph JS doesn't run until something is clicked? But your app.js runs as soon as the page has loaded, so the data it probably unavailable. – Jonnix Feb 27 '19 at 10:50
  • your console showing u undefine, but undefine what? – devpro Feb 27 '19 at 10:51
  • @devpro see my updated code . – Danny Feb 27 '19 at 10:53
  • @JonStirling Yeah but i've clicked on the options from drop down and it's returning undefined in my console from app.js as i've mentioned in my post – Danny Feb 27 '19 at 10:54
  • Yeah, because app.js already ran before you clicked the button. – Jonnix Feb 27 '19 at 10:54
  • @JonStirling How can I sort this out :o – Danny Feb 27 '19 at 10:57
  • Not sure since I can't work out what you're actually trying to do with the code you've got. The simplest way I guess is to put the app.js bit in a function, then call that function in the success part of the AJAX call in bargraph. But for a more robust set of options, see the duplicate link. – Jonnix Feb 27 '19 at 11:00
  • @JonStirling I am creating a graph in my app.js file, I want to be able to choose from the drop down menu and pass the values from "data" in my bargraph.html file to my app.js file to use and redraw the graph.. what is the simplest way for this. – Danny Feb 27 '19 at 11:06
  • `passfunction.data` is `undefined`, why wouldn't it be? If you want to call `passfunction` with `data` as the parameter you write `passfunction(data)` – Lennholm Feb 28 '19 at 10:42
  • @Lennholm I have tried this just now and still `console.log(passfunction(data));` is displaying "undefined" in browser console. – Danny Feb 28 '19 at 11:01
  • @Lennholm *correction, it is not displaying undefined now, but it is displaying my html file in text?? – Danny Feb 28 '19 at 11:06
  • @Danny Because that's what `data` is, the response to your AJAX request. Make sure you're making your request properly (correct URL, the correct HTTP method, etc). – Lennholm Feb 28 '19 at 14:06
  • @Lennholm can we chat please? – Danny Feb 28 '19 at 14:07
  • @Danny Sorry, I don't have the time for chat. But please post the entire code of your `$.ajax()` call in the question, the way you've cut it off in the middle of the success callback makes it very difficult to help you. – Lennholm Feb 28 '19 at 14:10
  • @Lennholm please see https://stackoverflow.com/questions/54885152/chart-to-update-from-a-drop-down-selection – Danny Feb 28 '19 at 14:25

0 Answers0