Jump to content

php and javascript array


pfkdesign

Recommended Posts

hi,

i just wondering how can i use php in js. i have the flowing javascript code to add dynamic row, but i have an array in this js (HeatSelectOption[]) to get the label from php .

here is the step.js .

function addRowToTable()
{
  var tbl = document.getElementById('tblheat');
  var lastRow = tbl.rows.length;
  // if there's no header row in the table, then iteration = lastRow + 1
  var iteration = lastRow-4;
  var row = tbl.insertRow(lastRow);

  // left cell
  var cellLeft = row.insertCell(0);
  var textNode = document.createTextNode(iteration);
  cellLeft.appendChild(textNode);

    // select cell
  var cellRightSel = row.insertCell(1);
  var sel = document.createElement('select');
  sel.name = 'steptype_'+ iteration;
  sel.id = 'steptype_'+ iteration;
  sel.options[0] = new Option(HeatSelectOption[0], '1');
  sel.options[1] = new Option(HeatSelectOption[1], '2');
  cellRightSel.appendChild(sel);

  // right cell
  var cellRight = row.insertCell(2);
  var el = document.createElement('input');
  el.type = 'text';
  el.name = 'textfield1_'+ iteration;
  el.id = 'textfield1_'+ iteration;
  el.size = 10;
}

 

and the array on the page is:

 

<script>
HeatSelectOption = new Array();
HeatSelectOption[0] = "<?php echo $selectOption['first_label'];?>";
HeatSelectOption[1] = "<?php echo $selectOption['second_label']; ?>";
</script>

 

any idea ? ??? ???

Link to comment
https://forums.phpfreaks.com/topic/119708-php-and-javascript-array/
Share on other sites

That should work fine.

 

Are you having a problem with it?

 

Just remember that PHP gets executed and stops before Javascript sees any code. If you need this to be dynamic, you would need to use AJAX.

 

Don't bump your post so quickly.

 

Ken

hi , unfortunately it is not working,i used ajax ( customized code js and php code ) , and i can not understand why it is not working ?! ??? :o

i'm adding dynamically a row by pressing (+) or removing by pressing (-) (a dropdown menu and a text field) and inserting  into the database, it is working when i use static label.

so it should be a problem to use php with js , the javascript executed first and pass blank label (it is not let to execute php) !! thats why it is return blank label for dropdown menu!

 

any idea?

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.