0

I need to be able to add additional rows to the following form group on the click of a button. The PHP statement echos a dynamic list of options for the select input based off records in the database.

  <h3>Check 1:</h3>
  <div class="row" id="check_1">
      <div class = "col-md-4 padding-top-10">
          <label for="checkJobName_1" class="control-label">Job/Client Name:</label>
          <select class="form-control" id="checkJobName_1" name="checkJobName_1">
              <option selected disabled>Choose a Client/Job</option>
              <?php echo $dynamicJobList; ?>
          </select>
      </div>

      <div class = "col-md-4 padding-top-10">
          <label for="checkNumber_1" class="control-label">Check #:</label>
          <input type="text" class="form-control" id="checkNumber_1" name="checkNumber_1" placeholder="Enter Check #"/>
      </div> 

      <div class = "col-md-4 padding-top-10">
          <label for="checkAmount_1" class="control-label">Check Amount:</label>
          <input type="text" class="form-control" id="checkAmount_1" name="checkAmount_1" placeholder="Enter $Amount of Check"/>
      </div>                    
 </div>

This is the javascript function I wrote to do so:

function addCheck()
{
            check_i++;
            var checkDiv = document.createElement('div');
            checkDiv.innerHTML = '<h3>Check '+check_i+':</h3><div class="row" id="check_'+check_i+'"><div class = "col-md-4 padding-top-10"><label for="checkJobName_'+check_i+'" class="control-label">Job/Client Name:</label><select class="form-control" id="checkJobName_'+check_i+'" name="checkJobName_'+check_i+'"><option selected disabled>Choose a Client/Job</option><?php echo $dynamicJobList; ?></select></div><div class = "col-md-4 padding-top-10"><label for="checkNumber_'+check_i+'" class="control-label">Check #:</label><input type="text" class="form-control" id="checkNumber_'+check_i+'" name="checkNumber_'+check_i+'" placeholder="Enter Check #"/></div><div class = "col-md-4 padding-top-10"><label for="checkAmount_'+check_i+'" class="control-label">Check Amount:</label><input type="text" class="form-control" id="checkAmount_'+check_i+'" name="checkAmount_'+check_i+'" placeholder="Enter $Amount of Check"/></div></div>';
            document.getElementById('checks').appendChild(checkDiv);        
    }

But it doesn't work cause if the PHP statement embedded in the HTML I'm trying to append to the "checks" form-group... how would you do it?

1
  • 1
    you can't run php on the client. php is a purely server-side language. if you want php output to be inserted into whatever JS is inserting, you'll have to do an ajax call to get php output. Commented Jun 2, 2015 at 18:33

2 Answers 2

1

Php is a complete server side language, which means no

Your best option is to json_encode your array and send it through ajax call, or in your php top side code area.

file1:

$array = array("data1" => array() ....);
echo json_encode($array);

Your html (with jquery):

$.post("file1 uri" , function(data) {
    var json = JSON.parse(data);
    foreach (var x in json) alert("x: " + x + " json[x]" + json[x]);
});
Sign up to request clarification or add additional context in comments.

Comments

0

PHP is a server side language, it sends data to the client and responds to requests.

Javascript is a client side language, that alters what the client is looking at, and not what the server knows.

The only way to get them to work together is to use AJAX calls in jQuery.

Comments

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.