0

I am trying to append new row to my table, but some input fields got some value retrived from mysql. I know that php is executed server side and javascript client side. Help me with this.

Code that needs to be appended :

<td>
    <select name="categories[]">
        <?php foreach(categories() as $row){
            foreach($row as $key=>$cell){
            echo '<option>'.$cell.'</option>';
            }
        }?>
    </select>   
</td>

So my jquery append is the exact code above turned into a string:

SOMECODE.append('<td><select name="categories[]"><?php foreach(categories() as $row){foreach($row as $key=>$cell){echo "<option>".$cell."</option>";}}?></select></td>');

So how can i append my php code by using jquery ?

2
  • Surely you're asking how to append the HTML generated by the PHP code? Commented Dec 19, 2013 at 21:54
  • Did any of the below answers proved helpful? Please check the one that solved your problem, so this thread can be closed. Commented Jul 1, 2014 at 15:30

5 Answers 5

3

You can mix PHP with JavaScript, assuming that it is running on a valid PHP page.

For example, the following should achieve what you're trying to do:

var td = SOMECODE.append('<td />'),
    sel = td.append('<select name="categorii[]" />');

<?php 
foreach(categorii() as $row)
{
    foreach($row as $key => $cell)
    {
        echo "sel.append('<option>$cell</option>')";
    }
}
?>
Sign up to request clarification or add additional context in comments.

4 Comments

@SamHunter this isn't an example of AJAX. The JS code is generated by the PHP parser at load time, not asynchronously at run time (as per AJAX).
I love the answer BenM. And it answers the question 100%, but why on earth would anyone want to do it like this?
I actualy have a button with a function Add another field, that adds one identicaly with that i had. But cant process the php code
Then you can only achieve something like that using AJAX technology.
1

Yes you can, but it won't execute. You said it yourself. It's a server side language. What you need is ajax. Just make a php script that echoes the data you want. Receive them client side and append the data.

Comments

1

You cannot, and there's no need to. Instead, you would use Ajax to grab the new information and then JavaScript to create elements to hold that data and append that information to the DOM:

Comments

1

So the question is not how to execute the PHP code by javascript, your problem is how to get the right Data into Javascript.

There are different possibilities:

  • You could reload your page and appand a parameter to your php-skript allowing it to select the right data
  • you could request the data using AJAX. You will need to create a skript that take, as per previous point, parameters from the AJAX Call and deliver the right data in the result. Javascript will do the rest

Since your example doesn't need any information for creating the cetegories, you just need a php script creating them. There is no need to deliver a page and asking javascript to get Data, which is static. Just deliver this static data with your page.

Comments

0

You can't append php code using javascript. PHP is run by the server and javascript is run on the browser. However in a PHP file you can use a <script> tag to create a function to do this.

<script>
function appendCategories(SOMECODE) {
   SOMECODE.append('<td><select name="categorii[]"><?php foreach(categorii() as $row){foreach($row as $key=>$cell){echo "<option>".stripslashes($cell)."</option>";}}?></select></td>');
}
</script>

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.