У меня есть файл PHP, где я отображаю таблицу html (HTML) с данными из таблицы mysql.
<?php
include 'connexion.php';
session_start();
$idfirm = $_REQUEST['idfirm'];
$namefirm = $_REQUEST['namefirm'];
Строки в таблице имеют
<tr class="clickableRow"...
Затем в строке click (javascript) я хочу, чтобы иметь возможность вызвать (POST) другой файл PHP, который будет отображать другую информацию на основе некоторой информации из строки таблицы. У меня есть трудности в достижении этого.
То, что я сделал до сих пор:
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
Итак, как я могу сделать POST для другого php файла и отправить переменные POST: name, namefirmx, idfirmx, myID
Я не уверен, как сделать POST.
Я считаю, что нет другого способа вызвать POST, но javascript. (Помните, что POST должен быть сделан по щелчку строки)
Пожалуйста, помогите мне здесь...
Я решил свою проблему, используя предыдущий вопрос SO. Я не уверен, что делать с этим вопросом. В любом случае, ответ: добавление функции в javascript и вызов ее с моими переменными. Функция:
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();
}
Быстрый пример php-скрипта, некоторые html-теги пропущены, но я думаю, вы получите изображение:
<?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>
Вы всегда можете добавить php-переменные в свой js-код, например:
<?php
$x="ABC";
?>
<script language="javascript">
jsX="<?php echo $x;?>";
alert(jsX);
</script>