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
Related
I am a new to this type of coding so I was wondering if someone could help me.
Here's what I want to achieve, an input field where the user can enter text and have another text appended to this. So for example, when the user enters text e.g "My Name!!", upon posting there would be another hidden text appended to this, so the server would receive "Hidden Text!","My Name!!".
Here's an image explaining this in an easier way.
Here is my code so far..
<form method="post" action="jumpin.php">
<label>Desired Username:</label>
<div>
<label id='labletext'><?php echo $_SESSION['user_name_custom']; ?></label>
<input type="text" id="userid" name="userid" />
<input type="submit" value="Check" id="jumpin" />
</div>
<script>
$('#userid').keyup(function(){
$(this).css('color','#000');
});
$('#userid').blur(function(){
var value = $('#labletext').text()+$(this).val();
$(this).val(value);
});
</script>
</form>
This code doesn't seem to be working, all the server receives is the text the user submitted and not the "labletext".
You can use a hidden input field.
<input type="hidden" name="extra_label" value="<?php echo $_SESSION['user_name_custom']; ?>" />
Not visible to your users, but the data is passed to your server.
In your server-side code, you'll access the variable like $_REQUEST['extra_label'].
<input type="hidden" name="label" value="yourvalue" />
And in your submission you can do this
if(isset($_POST['submit']))
{
$text = $_POST['text'];
$label = $_POST['label'];
$string = $label.$text;
}
You have to put the concatenated value into the form field so it will be submitted to the server when you post the form.
$('#userid').blur(function(){
var value = $('#labletext').text().trim() + ' ' +$(this).val();
$(this).val(value);
});
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 have a form with some jQuery UI sortables that have data-attributes that get serialized into a hidden textbox, so when I send the form, my PHP script can look at the value of $_POST["contents"](which is a string like this 1-2-2-1) and pass it to my MySQL UPDATE query. The problem is, the reported value of the textbox before sending the form (from the javascript), is different than the one in $_POST.
The form:
<form method="post" action="?update">
<input type="text" name="id" style="display:none" value="1" />
<input type="text" name="path" placeholder="Endereço Ex.: /pacotes/pureenergy" value="/some/path" /> <input type="text" name="title" placeholder="Título da página" value="<?php echo $t;?>" /><br />
<!-- Some stuff hidden for briefness -->
<input type="text" id="ci" name="contents" style="display:none" value="1-2-2-1" />
<input type="submit" value="Atualizar página" onclick="updateForm()">
</form>
The Javascript:
function updateForm() {
var txt = $("#dropzone li:first-of-type").data("id");
$("#dropzone li:not(:first-of-type)").each(function() {
txt = txt+"-"+$(this).data("id");
});
$("#ci").text(txt);
alert(txt);
}
The PHP:
$id = $_POST["id"];
$p = $_POST["path"];
$t = $_POST["title"];
$c = $_POST["contents"];
mysql_query("UPDATE aruna.pages SET path='$p', title='$t', contents='$c' WHERE id=$id") or die(mysql_error());
The value reported by the javascript alert() is behaving as intended, but when I send the form, the value of the $_POST["contents"] is the same that was hardcoded in the HTML.
EDIT: PHP doesn't complain about $_POST["contents"] being unset, which it would if there was a typo.
You may try .val() instead of .text()
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>
I have these codes:
testing.php
<html>
<head>
<script type="text/javascript">
function copy_data(id){
var a = document.getElementById(id).value;
document.getElementById("copy_to").value=a;
}
</script>
</head>
<body>
<form action="testprocess.php" method="post">
<input type="text" name ="a" id="copy_from" onkeyup="copy_data('copy_from')"/>
<input type="text" value=0 name ="b" id="copy_to"/>
<input type="submit">
</form>
</body>
</html>
testprocess.php
<?php
$test = $_POST['copy_to'];
echo $test;
?>
I get an error saying that 'copy-to' is an undefined variable. Can you please tell me why?
Thanks.
$_POST values are passed through an element's name attribute rather than the ID. Try this:
<input type="text" value=0 name="copy_to" id="copy_to"/>
And make sure you use a an underscore in your PHP variable:
$test = $_POST['copy_to'];
Needs to be $_POST['a'] the id isn't submitted into the post array, it's the name attribute
because you have no element with name copy_to in your form.
Try below :
<form action="testprocess.php" method="post">
<input type="text" name ="a" id="copy_from" onkeyup="copy_data('copy_from')"/>
<input type="text" value=0 name ="copy_to" id="copy_to"/>
<input type="submit">
</form>
$_POST will contain the values of your form fields based on the name attribute of the form elements.
<input type="text" name="copy_from"/> will become $_POST['copy_from']
and
<input type="text" name="copy_to"/> will become $_POST['copy_to']
You are using the value in the id attribute of the input (and spelling it inconsistently), so it is undefined to PHP.
It ought to be a _ instead of a -.
EDIT: Oh god, it's even worse. It ought to be $_POST['a'], because of the name-attribute. The name-attribute is used to specify the name/identifier under which a GET or POST parameter will be passed to the web application. The id-attribute is mostly used to identify HTML elements at the client, e.g. when doing some stuff with javascript.