0

I have a question about php and javascript. I'm new about this.

I have a php array and I use a php function to visualize these informations in the page. In particular I use a Jquery table and I have this simple php script to generate this table:

In the html <body> tag i have:

<table cellspacing="1" class="tablesorter">             
    <thead>
        <tr>
            <th>GoTo</th>  
            <th>Latitude</th> 
            <th>Longitude</th> 
            <th>OtherInfo</th> 
            <th>Saved</th>
        </tr> 
    </thead> 
    <tbody id="table">
    <?php
    $index = 0;
    foreach ($entries as &$value) {
        echo("<tr><td><a href=\"javascript:moveToLocation($value->latitude,$value->longitude,'$value->otherInfo');\">Go to</a></td>");
        echo("<td>".$value->latitude."</td>");
        echo("<td>".$value->longitude."</td>");
        echo("<td>".$value->otherInfo."</td>");
        echo("<td id=textNotsaved>Not saved</td>");
        echo("</tr>");
        echo("<script> addMarker(".$value->latitude.",".$value->longitude.",".$index."); </script>"); 
        $index++;
    }
    ?>
    </tbody> 
</table>

Now, I would to use some javascript functions (so, javascript events) to modify some cells of my table.

1) What is the best way for this?

2) My php array correspond to my model (if I would to consider MVC into my simple page), is possible tho modify a php variable using javascript?

1
  • what id do sometimes is to "echo" the php variable to a javaskript, when the page is rendert javaskript can then work on the data, thats possible. With jquery it is possible to edit the table cells, so i don't see a problem there. Commented Apr 11, 2014 at 8:17

2 Answers 2

5

Before answering your question, I 'd like to point out a couple of things which are really important here. The way your web application works is the following

  1. HTML is being produced by your application on the server (PHP)
  2. HTML is being served to your browser by your web server
  3. HTML is being parsed from your browser, so as to initialize the DOM tree (the document JavaScript object and all of its children)

As a matter of fact, JavaScript and PHP are completely agnostic of each other. That means that JavaScript does not know or care if your code came from PHP, Python or a static HTML page and PHP does not know or care if its produced code will be manipulated by JavaScript.

What you would like to do actually is manipulate the DOM elements of the table, something that is definitely possible. This changes though will stay on the browser and won't be propagated to the server.

If you 'd like to send these changes to the server, you have to prepare a PHP file that will accept data and save them to your database, file or whatever medium you use to keep your data. To do that you will either have to submit a form or do this with AJAX. jQuery supplies great functions to deal with AJAX requests.

I hope that this helped you.

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

Comments

0

X-editable might be the one for you. It can make an AJAX postback if you want to change the value.

Try out the DEMO.

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.