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
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

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.