我有一个带有多个输入和相应按钮的表单。输入名称属性和按钮 id 都相同。IE
<form method="post" action="update.html">
<input type="text" name="title">
<button id="title">Submit</button>
<input type="text" name="metaKeywords">
<button id="metaKeywords">Submit</button>
<input type="text" name="metaDescription">
<button id="metaDescription">Submit</button>
</form>
现在我要做的是获取按钮单击的 id,然后将其值插入到以下位置的我的 php 函数代码中;
<?php
function update() {
// specify target file name to update
$fileName = 'variables.php';
// Let's make sure the file exists and is writable first.
if (is_writable($fileName)) {
// load target filename contents inside a variable
$content = file_get_contents($fileName);
// use reg ex to find and replace variable contents within target filename
$content = preg_replace('/\$**INSERT_BUTTON_ID_HERE**=\"(.*?)\";/', '$**INSERT_BUTTON_ID_HERE**="'.$_POST["**INSERT_BUTTON_ID_HERE**"].'";', $content);
// open target filename for writing
$handle = fopen($fileName, 'w');
// Write $content to our opened file.
fwrite($handle, $content);
// success message
echo "<p>Success, localisation file updated.</p>";
// close opened file
fclose($handle);
} else {
echo "<p class='errorMessage'>The localisation file is not writable</p>";
}
}
if (!empty($_POST['**INSERT_BUTTON_ID_HERE**'])) {
update();
}
?>
这可能吗?