1

I'm trying to append PHP code in Jquery. I'm getting confused in the quotation marks and not getting correct result.

below code.

  <script>
    $(document).ready(function(){
        $("#add").click(function(event){
             event.preventDefault();
             //var text_box = "<br>Fee type: <input type='text' name='f1[]'>amount : <input type='text' name='f2[]'><br>";
             var text_box = "<?php
                        #Fee type textbox
                            if(form_error('feetype'))
                                echo "<div class='form-group has-error' >";
                            else
                                echo "<div class='form-group' >";
                        ?>
                            <label for='feetype' class='col-sm-2 control-label'>
                                <?=$this->lang->line('invoice_feetype')?>
                            </label>
                            <div class='col-sm-6'>
                                <input type='text' class='form-control' id='feetype' name='feetype[]' value='<?=set_value('feetype')?>' >
                            </div>
                            <span class='col-sm-4 control-label'>
                                <?php echo form_error('feetype'); ?>
                            </span>
                        </div>";
            $("#info").append(text_box);
        });
<script>
2
  • 5
    first rule of programing know your programming languages , you can't execute php on the client side Commented Apr 6, 2017 at 7:52
  • the code will added as a plain text. not as you want (that it will be added and executed as well) Commented Apr 6, 2017 at 7:54

1 Answer 1

1

You can use an AJAX call to execute a PHP script on the server. So, for your if statements, just send the variables with conditions to be tested to a php script via ajax and php will return the html code of the div which you can then append to an element.

As an example for your first div;

You can set up an ajax call like;

<script>
    $(document).ready(function(){
        $("#add").click(function(event){
             event.preventDefault();

            var part;
            var test = 'feetype';

            $.ajax({
         type: 'POST',
           url: "get_div.php",
           data: {test:test},
           success: function(result){
               part = result;
           }
     })

var text_box = part + "<label for='feetype' class='col-sm-2 control-label'>"
/***
all other code here
***/
</script>

Then in your php script

<?php
    #Fee type textbox
    if(form_error($_POST['test']))
     echo "<div class='form-group has-error' >";
    else
     echo "<div class='form-group' >";

Let me know if it helps or if you find a problem.

Sign up to request clarification or add additional context in comments.

5 Comments

the php code is used between as well. it is not limited to if condition. how to get values from another page. i'm stuck
its working fine. but it is only executing once ... I have placed the code in head section
Hey, please note that it is always a good practice to place javascript code at the end of the document before the closing body tag. First try to place it exactly before the closing body tag i.e </body>
Still the same. It is getting executed only once.
Hey, try to troubleshoot the click listener to see if it responds after clicking more than once by setting up an alert("anything") to see if it is called. You can try to find out exactly which function or what part of code is not running by setting by alerts but i really can't tell what the problem might be unless i look at the code.

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.