I have a hidden input field that I fill with some arbitrary value using jquery such that:
function foo() {
// Obtains the contents of a div within the current page
var $elem = $("#something").html();
// Places the contents of the div into a hidden input field
$("#hidden").val($elem);
}
In debugging the thing, I can see that the elem variable obtains the html from within the desired div, but not whether the variable is passed to the hidden input field value.
I have a form:
<form method="POST" action="file.php" target="_blank">
<input id="hidden" type="hidden" value=""/>
<input type="submit" onmouseover="foo()" value="Download"/>
</form>
That upon submission executes the file.php:
<?php
$html = $_POST["hidden"];
echo $html;
?>
The echo call returns nothing, and all I get is a blank page. I am wondering why the value of the hidden input field is not being changed, or why it is not being passed during the POST call.
Some further experimentation revealed that even if I set the value of the hidden field with some random value:
<input id="hidden" type="hidden" value="Some Value"/>
it is still not obtained during the execution of the PHP file. The echo call returns nothing. What is the problem with my obtaining the value of this hidden input field? I have worked very sparingly with PHP but in the past have not had issue with obtaining values from a form upon POST submission.
You need to have a name for your input field in order to access it in php with $_POST
<input id="hidden" name="hidden" type="hidden" value=""/>
You should just indicate name attribute
<form method="POST" action="file.php" target="_blank">
<input name="hidden" id="hidden" type="hidden" value=""/>
<input type="submit" onmouseover="foo()" value="Download"/>
</form>
Related
I have a form which I want to post to my PHP file. The form consists of the submit button and a hidden value which contains the value of a localStorage entry:
<form action="submit.php" method="post">
<input type="hidden" name="formAnswers" id="localStorageAnswers" />
<input type="submit" id="btnSubmit" value="Submit" />
</form>
jQuery:
$('#btnSubmit').on('click', function() {
$("#localStorageAnswers").val(localStorage.getItem(answers));
});
PHP:
if (isset($_POST['formAnswers'])) {
echo $_POST['formAnswers'];
}
When the PHP file is launched no answers are shown. At first I thought maybe It wouldn't set it on the on('click') but I've set the value at a sooner point on the page with no difference.
I believe answers is not a variable, but a keyword. Use localStorage.getItem("answers") instead of localStorage.getItem(answers)
I want to create a hidden field in my HTML page, all good, just a simple <input> field. Then, I'm trying to read the value of that input field as the page loads the first time, so the closest I've gotten to that is using a $_GET statement. I'm simply trying to echo out the value so that I can see that it's working like such:
<input type="hidden" name="selection" value="fixtures">
<?php
echo $_GET['selection']
and that just gives me nothing, if I try:
echo "<h1>$_GET['selection']</h1>
then I get 0.
Question is - what <form> parameters you have? Did you submit form? Simples example for it will be:
//form.php
<form method="GET" action="next.php">
<input type="hidden" name"selection" value="fixtures" />
<input type="submit" name="submit_btn" value="Send" />
</form>
//next.php
echo '<h1>'.$_GET['selection'].'</h1>';
You can achieve same thing with:
Link
I'm using CodeIgniter and in a single PHP file with JavaScript inside, I want to pass a JavaScript variable to the body (PHP) and make it a hidden input. But whenever I use the controller to post the value (where the JavaScript variable is), it returns none. Here are some parts of the code:
JS:
function pass() {
//some code
document.getElementById('yes').innerHTML = yes; //where yes is a var
}
HTML (PHP):
<form action="search">
<input type="hidden" name="yes" value="<?php $yes= "<p id='yes'> </p>"; echo $yes;?>" />
<input type="submit" name="yes" value="Done" />
</form>
So whenever I post the yes in the controller $yes = $this->input->post('yes'); it returns nothing.
How can I pass the JavaScript variable so I can use it again in the next file? Thank you!
You did'nt set the form method so it defaults to GET
You should set
<form action="search" method="POST">
try
JS :
var yes = "<?php echo $_POST['yes']; ?>";
document.getElementById('yes').innerHTML = yes;
You have to set the value property of the <input>, not the innerHTML. You also need to give the <input> a different name than other fields or the "submit" button. Finally, you have to give your <input> an "id" property so that you can actually get it with getElementById().
You should be setting the value of the hidden input, not the innerHTML. This code should work:
function pass() {
//some code
document.getElementById('yes').value = yes; //where yes is a var
}
Another problem, as noted by Pointy, is that the hidden input doesn't actually have an id, so you should give it an id (in this case the id should be yes).
Something you should also do is escape the html you are inserting into the hidden input with PHP, so it doesn't accidentally get parsed. You can do this with htmlspecialchars():
<form action="search">
<input type="hidden" id="yes" name="yes" value="<?php $yes= htmlspecialchars("<p id='yes'> </p>"); echo $yes;?>" />
<input type="submit" value="Done" />
</form>
Your submit button and your hidden field have the same name
yes .
You try to access your hidden input by id yes , and your input
does not have this id , use getElementByName('yes') instead or give
your hidden field id='yes'.
You use innerHtml which only sets or returns the inner HTML of an element,it should be value.
HTML CODE:
<form action="search">
<input id='yes' type="hidden" name="yes" value="<?php $yes= "<p id='yes'> </p>"; echo $yes;?>" />
<input type="submit" name="yess" value="Done" />
</form>
JS :
document.getElementById('yes').value = yes;//yes is a variable
I have a simple registration form.
I want to pass value entered in one page to other in a text field.
how to pass and access it from next page in php.
this is my first php page.
Thanks in advance.
You can add hidden fields within HTML and access them in PHP:
<input type="hidden" name="myFieldName" value="someValue"/>
Then in PHP:
$val = $_POST['myFieldName'];
If you're going to ouput this again you should use htmlspecialchars or something similar to prevent injection attacks.
<input type="hidden" name="myFieldName" value="<?=htmlspecialchars($_POST['myFieldName']);?>"/>
Suppose this the form input in page A
<form name="" action="" method=post enctype="multipart/form-data">
<input type="text" name="myvalue" value="">
<input type=submit>
</form>
In page B
In the page you want to get values put this code
<?PHP
foreach ($_REQUEST as $key => $value ) {
$$key=(stripslashes($value));
}
?>
<form name="" action="" method=post enctype="multipart/form-data">
<input type="text" name="myvalue" value="<?PHP echo $myvalue" ?>">
<input type=submit>
</form>
So yo can use or attach variable value to another form do what else you want to do
use following code, that should help you.
<form action ="formhandler.php" method ="POST" >
<input name = "inputfield" />
<input type="submit" />
</form>
on formhandler.php file yo need to enter following code to get the value of inputfiled.
$inputfield = isset($_POST['inputfield'])?$_POST['inputfield']:"";
// now you can do what ever you want with $inputfield value
echo($inputfield);
Can i send a input value into form action ?Let say, on the form the phone number taken.Now can i send the phone number as form action parameter "number"? Is their any way to send it?
<form method="post" action="abc.php?number=ph_number" enctype="multipart/form-data">
<input type="text" name="ph_number" value=""/>
<input type="submit" name="search" value="SEND"/>
</form>
How can i do it?
Thanks in advance
riad
<form method="GET" action="abc.php" enctype="multipart/form-data">
<input type="text" name="number" value=""/>
<input type="submit" name="search" value="SEND"/>
</form>
Change action="abc.php?number=ph_number" to action="abc.php
Change name="ph_number" to name="number"
When you click submit, the value contained in "number" text field will be passed to abc.php.
Receive the value with $value = $_REQUEST['number']; in abc.php.
You can leave an empty action, and use the onSubmit event to load a javascript function that does whatever and redirects to the page according to the input value.
Html
<form .. action="" onsubmit="return abcByPhone(this);">
Javascript
function abcByPhone(form) {
url = from.number.value;
...
}
EDIT:
I actually didn't read the question properly. I thought you wanted to redirect to different pages according to the input. Using plain GET (like the others mentioned) is fine for this.