Reputation: 1039
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?
Upvotes: 0
Views: 75
Reputation: 3488
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.
Upvotes: 0
Reputation: 33813
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>
Upvotes: 1