0

I need to get values from a postgre db and use the fopen to open the link inside its records to open real xml file.

<?php
echo "<form id=read2 method=post action=read2.php>";
//other html and table codes
while ($row = pg_fetch_row($result)) {
    echo "<tr><td>$row[1]</td><td><input type=hidden name=data value=$row[3] /><a href=javascript:; onclick=document.getElementById('read2').submit();>$row[2]</a></td><td>$row[4]</td><td>$row[5]</td></tr>";
}

the read2.php:

<?php
$data=$_POST['data'];
$explode = explode("/inbox/", $data);
$final = "";
$final.="data/";
$final.=$explode[1];

echo "Result of data before explode is: $data <br />";
echo "Result of data after explode is: $explode[1] <br /><br />";

$myfile = fopen("$final", "r") or die("<h1>Unable to open file!</h1>");
$xml = htmlspecialchars(fread($myfile,filesize("$final")));
?>
<pre>
<?php echo $xml; ?>
</pre>
<?php fclose($myfile); ?>

My problem is here: <input type=hidden name=data value=$row[3] /> I am able to pass to read2.php the correct value and use the explode to adjust what I really need, but I am not able to chose which value to get due to name=data which is the same for all and the read2.php only get the last one in list.

I tried with a counter inside the while: name=data[count]; count++ But in this case I do not know how to get "name" from $_POST

It is also possible that the javascript code I use to send the form is not the best for this situation. Can you please help me fix?

5
  • 1
    first in HTML you need to quote values like <form id="read2" method="post" ..... Commented Dec 30, 2015 at 11:05
  • I know this rule but life's too short to quote everything! just kidding ;) thing is that it would be better to put the html code outside the php but at the moment this name=data thing is a bit more important than writing a good looking code. This is in a test environment, later on production I will fix the html don't worry! Commented Dec 30, 2015 at 11:13
  • I can't help with your specific problem, but I promise that getting into the habit of writing good code from the start, rather than bad code with the intention of fixing it later, will infinitely improve your life, save you time, reduce your stress and make everything much better. I can almost guarantee that you'll never go back and add those " characters in, at least, until they cause some terrible bug or failure in a specific browser, once your code is live. Commented Dec 30, 2015 at 11:48
  • It is true what you say and probably I didn't express myself well, sorry! I'm working with the same code in test and production environment. (test=virtual machine emulated inside Virtual box in my laptop office - production=real server) The production environment run with all the good code, quotes and old stable code. The test instead, c.r.a.p. code, no or some quote ect.. I revert the test snapshot to the original Production and stable code when I know that the test went successful. From there I update the real production with all the steps I did (if there were) to fix issues during test. Commented Dec 31, 2015 at 13:30
  • it's tricky, bad, long.. but it happened that you're sure of writing good code in production you used during test and then it behave differently. However, thank you guys for warned me! appreciate it! Commented Dec 31, 2015 at 13:30

2 Answers 2

1

I think I understand what the problem is but please excuse me if I'm off target with what follows. If you were to approach this slightly differently it should be relatively easy.

Have one form separate from the table that contains the recordset data and links - the form need only have the one hidden element which you target using javscript and set the value dynamically.

The following is not tested so it might not work "out of the box" but I think the idea is sound.

<!doctype html>
<html>
    <head>
        <title>Table data - form submission</title>
        <script type='text/javascript'>
            function submitdata(event){
                var el=typeof( event.target )!='undefined' ? event.target : event.srcElement;
                var data=document.getElementById('data');
                data.value=el.dataset.value;
                data.parentNode.submit();
            }
        </script>
    </head>
    <body>
    <?php

        echo "
        <!-- 
            this is the form that actually sends data. 
            Programatically set the value of the hidden element
            and submit the form
        -->
        <form name='senddata' method='post' action='read2.php'>
            <input type='hidden' id='data' name='data'/>
        </form>



        <table>";
            while( $row = pg_fetch_row( $result ) ) {
                echo "
                <tr>
                    <td>{$row[1]}</td>
                    <td>
                        <a href='#' data-value='{$row[3]}' onclick='submitdata(event)'>{$row[2]}</a>
                    </td>
                    <td>{$row[4]}</td>
                    <td>{$row[5]}</td>
                </tr>";
            }

        echo "
            </table>";
    ?>
    </body>
</html>
Sign up to request clarification or add additional context in comments.

2 Comments

Wonderful! I tried your code and seems working! I need to test everything better of course but this is what I was looking for! Many and many thanks!
No problems - glad it helped. Also consider accepting the answer so that other know the question is resolved and the solution works.
0

You should move the hidden parameter from multiple to single and set the hidden parameter value using jquery or javascript and submit the form. Try as below :

<?php
echo "<form id='read2' method='post' action='read2.php'>";
echo "<input type='hidden' name='data' id='data'/>";
echo '<table>';
while ($row = pg_fetch_row($result)) {
    echo "<tr><td>$row[1]</td><td><a dataval=$row[2] href='#' onclick='callfunc(this)'>$row[2]</a></td><td>$row[4]</td><td>$row[5]</td></tr>";
}
echo '</table>';
echo '</form>';
?>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
function callfunc(obj)
{
    var dataval = $(obj).attr('dataval');
    $('#data').val(dataval);
    $('#read2').submit();
}
</script>

Above code is tested and working.

1 Comment

I didn't see your code in-time because I did not update the page since last reply, but your code is GREAT as well!!! I'm going to test both and see what's better to use in my php. Thanks mate :D This is exactly how the problem should be solved: "move the hidden parameter from multiple to single and set the hidden parameter value" just like the solution proposed by RamRaider. Thanks guys!

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.