-3

I have this javascript code that generates rows with a form. but I need to add one php variable associated in the value(the var uno in the code ) but it doesn't work. I'd appreciate your help.

    function formHtml() {
        var uno ='<input type="text" <?php echo" value='$pe';?> data-type="address" name="address[]" id="address_'+rowcount+'" class="form-control autocomplete_txt" autocomplete="off">';


        html = '<tr id="row_'+rowcount+'">';
        html += '<th id="delete_'+rowcount+'" scope="row" class="delete_row"><img src="./src/images/minus.svg" alt=""></th>';
        html += '<td>';
        html += '<input type="text" data-type="countryname" name="countryname[]" id="countryname_'+rowcount+'" class="form-control autocomplete_txt" autocomplete="off">';
        html += '</td>';
        html += '<td>';
        html += '<input type="text" data-type="countryno" name="countryno[]" id="countryno_'+rowcount+'" class="form-control autocomplete_txt" autocomplete="off">';
        html += '</td>';
        html += '<td>';
        html += '<input type="text" data-type="phone_code" name="phone_code[]" id="phone_code_'+rowcount+'" class="form-control autocomplete_txt" autocomplete="off">';
        html += '</td>';

        html += '<td>';
        html += '<input type="text" data-type="country_code" name="country_code[]" id="country_code_'+rowcount+'" class="form-control autocomplete_txt" autocomplete="off">';
        html += '</td>';

        html += '<td>';
        html += uno;
        html += '</td>';

        html += '</tr>';
        rowcount++;
        return html;
    }

2 Answers2

0

ASSUMING you are NOT using _POSTed form data (because the security issues cannot be covered here)...

in your HTML:

<script type="text/javascript" src="your_php_file.php" /></script>

in "your_php_file.php":

<?php header('content-type: text/javascript'); ?>

function name() {
   var uno = '<input type="text" value="<?php echo $pe; ?>" data-type="address" name="address[]" id="address_'+rowcount+'" class="form-control autocomplete_txt" autocomplete="off">';
}

<?php exit; ?>

although... i'm not sure why you are setting a variable to a constructed HTML input field... instead of just setting it's value...

aequalsb
  • 3,765
  • 1
  • 20
  • 21
-1

try writing

value="<?php echo $pe;?>"

or

<?php echo "value='$pe'";?>

both statement does the same thing.

Keep in mind whenever you start <?php tag the server starts executing php script after it. and execute it till ?> tag. and according to server it had to execute:

 echo" value='$pe';

which will never work.

Edit: php will consider the value of $pe if written between double quotes(") but won't consider if written between single quotes('). Eg:

<?php
$pe = 34;
echo "$pe"; //Output: 34
echo '$pe'; //Output: $pe
?>
jazeabby
  • 80
  • 7
  • 1
    You are using PHP within a JS. Not gonna work unless your JS is constructed by PHP. JS running on local machine – Will Aug 28 '19 at 15:26
  • @abby what WIll is saying is if the JS script (client side) isn't generated BY PHP (server side)... the PHP code cannot have access to the server-side variables... see my example below... the only other option is AJAX (but that is still talking with the server and is the only way to have client-side/server-side interaction) – aequalsb Aug 28 '19 at 15:31
  • My assumption was that, the above function was written in .php file hosted on a server locally, not js/html file. If it is in .js or an .html file, of-course @aequalsb's method or AJAX is to be used here.. – jazeabby Aug 30 '19 at 20:05
  • and i'm not sure why Will mentions "local machine"... not sure exactly what he means by that because it can be interpreted more than one way... regardless, another way is to set the field value using javascript (once it is client side) by setting a global javascript variable with PHP during the document generation... such as in the `` tag... `... then set your field value to `my_global_var` after your document is loaded – aequalsb Aug 31 '19 at 14:32