0

I have a PHP file where I display a html table (HTML) with data from a mysql table.

<?php
include 'connexion.php';
session_start();
$idfirm = $_REQUEST['idfirm'];
$namefirm = $_REQUEST['namefirm'];

The rows in the table have the

<tr class="clickableRow"...

Then on row click (javascript) I want to be able to call (POST) another PHP file that will display other information based on some info from the table row. I have difficulties in achieving this.

What I have done so far is:

echo '
...
jQuery(document).ready(function($) {
          $(".clickableRow").click(function() {
                var tableData = $(this).children("td").map(function() {
                    return $(this).text();
                }).get();

// So I can access the field data either like this
                var myID = $.trim(tableData[0]); 
// or like this:
                var name = $(this).closest(\'tr\').find(\'td:eq(1)\').text();
                alert(myID);';
echo"  // here I would need to access some variables that I received above in PHP from POST
                    var namefirmx = '{$namefirm}';
                    var idfirmx = '{$idfirm}';
";
// and here I would like to call another PHP file (with POST) that will display info about the employee

So, how can I make a POST to another php file and send POST variables:name,namefirmx, idfirmx,myID

I am not sure how to make the POST.

I believe there is no other way to call the POST but javascript. (Remember that the POST must be made on row click)

Please help me out here...

4
  • A simple way - create a form with hidden elements and method attribute "post" in each row, and when user clicked on row, send submit event to form in javascript. But need ajax? Commented Nov 10, 2013 at 16:41
  • I don't need Ajax. I do not need anything to be returned from the final PHP file. I need the final PHP to be displayed (since it contains HTML and shows stuff, including Buttons that will help in further navigation) Commented Nov 10, 2013 at 16:49
  • How do I create forms on each row? Seems like I would make this very heavy like this. Since I can access the onclick event, and also I can access the variables that I need for the POST... is there no way to call the POST straight from this function (rowclick)? I imagined the POST just bellow my above code... in a few lines, just like any other js function. Was I wrong? Commented Nov 10, 2013 at 16:53
  • You can use a single form on the page, but at final you need to post a data via form. Commented Nov 10, 2013 at 17:03

3 Answers 3

1

Quick example of php script, some html tags are missed, but I think you will get a picture:

<?php 
print_r($_POST);
?>

<script src="http://codeorigin.jquery.com/jquery-1.10.2.js"></script>
<script>
    jQuery(document).ready(function($) {
          $(".clickableRow").click(function() {
            $(this).find("form").submit();
          });
    });            
</script>

<table border="1">
  <tr class="clickableRow">
     <td>
          <form method="post"><input type="hidden" name="p1" value="v1"><input type="hidden" name="p2" value="v2"></form>
          v1
     </td>
     <td>v2</td>
  </tr>
</table>  
Sign up to request clarification or add additional context in comments.

1 Comment

Seems like it would make my code extremely difficult to read and prone to errors. Thank you for the answer. I will +1 it since it's a valid answer. The answer I posted (in the same time) uses a more simplified code, that is composed of one JS function that I can call without changing the HTML. However, if nobody else considers that your answer is bad..., then I will accept your answer since it seems a good one (I did not try it)
1

I solved my problem using a SO previous question. I am not sure what to do with this question. Anyway, the answer is: adding a function in javascript, and calling it with my variables. The function is:

        function post_to_url(path, params, method) {
            method = method || "post"; // Set method to post by default if not specified.

            // The rest of this code assumes you are not using a library.
            // It can be made less wordy if you use one.
            var form = document.createElement("form");
            form.setAttribute("method", method);
            form.setAttribute("action", path);

            for(var key in params) {
                if(params.hasOwnProperty(key)) {
                    var hiddenField = document.createElement("input");
                    hiddenField.setAttribute("type", "hidden");
                    hiddenField.setAttribute("name", key);
                    hiddenField.setAttribute("value", params[key]);

                    form.appendChild(hiddenField);
                 }
            }
            document.body.appendChild(form);
            form.submit();
        }            

Comments

1

You can always add php variables inside your js code like the following :

<?php 
$x="ABC";
?>
<script language="javascript">
jsX="<?php echo $x;?>";
alert(jsX);
</script>

2 Comments

I edited my question because in the meantime I solved the "getting" the php vars like echo "var namefirmx = '{$namefirm}';"; Thank you anyway, I will +1 your answer though. Please help me achieve the POST
I tried this already, but the POST does not happen. This is my code: $.post( "modiemployee.php", { idfirm: idfirmx, namefirm: namefirmx, idx: myID } ); Nothing happens, just the alerts show up. I need the modiemployee.php to be displayed just like when POST-ing from a simple HTML form in a simple HTML/PHP page

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.