I get data.my_id from an AJAX call. I want to place it inside a hidden input named my_id, but I am not able to do it. Is there a special way to succeed with this sort of input? Is it normal not to see the AJAX return in the sources of the page? Thanks and sorry for my school English.
// dat.my_id is working in an alert
$(#my_id).html(data.my_id);
<input type="hidden" name="my_id" id="my_id" value="<?= $my_id; ?>" />
You need to keep value of hidden empty initially.
<input type="hidden" name="my_id" id="my_id" value="" />
Then on ajax response you can set the value as below:
$("#my_id").val(data.my_id);
To pass a value to your input you can use
$('#my_id').val(data.my_id);
Some examples
Related
I'm trying to figure out how to pass the URL of a current page via a hidden field so that I can redirect back to that page after the form's input has been handled. I've tried using javascript:location.href, however it looks as though it'll pass that as a literal string.
<input type="url" id="location" name="location" value="javascript:location.href" hidden />
When viewing the page source, I can see that the value of this input box is "javascript:location.href" rather than the page's URL. Any ideas here? Thanks in advance!
You can access the element in Javascript and change the value there
document.getElementById('location').value = location.href;
Example:
http://jsfiddle.net/6zxD5/
I don't think it works that way. You could just use the onload callback to insert it when the page is completely loaded:
<body onload="document.getElementById('location').value = document.location.href">
If your document containing the form is already a PHP file , you can do
$yourPath = $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
And in your html you would do
echo '<input type="url" id="location" name="location" value="'.$yourPath.'" hidden />';
You can use JavaScript to set the value of the hidden field:
document.getElementById('location').value = window.location.href;
Example:
https://jsfiddle.net/moogs/mhjh0je3/
Since you also tagged PHP, here's the PHP version:
<input type="hidden" id="location" name="location" value="<?php echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; ?>" />
Well, first i'm new in PHP. Is there a way to get the input value from a existing input on the page on page load with php and pass it to a variable?
For example i have this input: <input type="text" name="g_id_p" id="example1" value"foo">
I want to do something like this: $got_it = $_GET['g_id_p'];
Sorry again if i wrote my code wrong, im noobie on this. Hope to someone help me.
First, would be great to know what method is the form. (GET or POST)
Then after know what type of method you could call it in PHP:
METHOD POST:
<input type="text" name="g_id_p" id="example1" value"foo">
$variable = $_POST['g_id_p'];
METHOD GET:
<input type="text" name="g_id_p" id="example1" value"foo">
$variable = $_GET['g_id_p'];
If you haven't defined a method, in html the tag for a form is:
<form>
<!-- Here goes your input and some stuff -->
</form>
Then it would be something like:
<form name="form_name" class="form_class" id="form_id" method="TheFormMethod" action="ThePageThatExecutesThisForm">
<!-- Here goes your input and some stuff -->
</form>
TheFormMethod can be post, get, delete, put.
You can't really get an associated value of an input tag within the same PHP page but what you can do is set the value of a variable beforehand.
What I mean is, create an array that will store all the values of all the input tags.
$inputValues = array();
$inputValues['g_id_p'] = 'foo';
Then when you have the tag later on just echo it from the PHP var.
<input type="text" name="g_id_p" id="example1" value="<?php echo $inputValues['g_id_p']; ?>">
As you can see, we aren't really 'getting' the value that you set but the end result is the same.
You have to check if the values is set isset(), where you want to use the variable do:
if(isset($_GET['g_id_p'])){
//your code
}
I have a problem with using variables in URL's. The reason I need to do this in JavaScript is because I can't use PHP (Phonegap).
<form action="https://website.com/login.dll?Logon" method="post" id="logonForm">
<input type="hidden" id="curl" name="curl" value="Z2FZ2Fexample.phpZ3FZ26idZ3D<?php echo $_SESSION['user_email'];?>Z26typeZ3D0" />
This works, but now I am using Phonegap so I can't use PHP, So I need to get a variable set by the user and then use that variable in the URL (instead of the php echo) I guess. I'll still be using the same form.
So I need the user to enter the info I need and pass that to a variable,
<input id="data" type="text" name="data" placeholder="Placeholder" required />
What is the best way of doing this in Javascript?
Add another attribute to the form
onSubmit = 'setCurlValue();'
Add code for setCurlValue
function setCurlValue() {
$('#curl').val('Z2FZ2Fexample.phpZ3FZ26idZ3D'
+ $('#data').val()
+ ';?>Z26typeZ3D0');
}
I'm new at JavaScript and I need some help to submit my form when anyone select anything from the dropdown list. Here is the sample code :
<form action="" method="post">
<select name="car" onChange="this.form.submit()">
<option value="car1">car1</option>
<option value="car1">car1</option>
<option value="car1">car1</option>
</select>
<!-- Some other code -->
<input type="submit" name="sentvalue">
</form>
This is my form and when anyone selects the dropdown, the form automatic submit but I also need the sentvalue when automatic submit the form. So can anyone help me to capture the sentvalue when anyone select the dropdown.
If you want to send any other data with the form submit you can send it in hidden inputs
<input type='hidden' name='data1' value='Test1' />
<input type='hidden' name='data2' value='Test2' />
//etc
And if you want the value of submit button with the form just set the value attribute into your code for submit button every this else seems fine,
<input type="submit" name="sentvalue" value="Test1" />
Hope this answers your question
Just give the <input> tag a name
<input name="submitbtn" type="submit" value="sentvalue" />
then you can get it by $_POST['submitbtn'] in case of PHP which value is "sentValue"
It sounds like you want the value of the input field (sentvalue) to be submitted as well. But how do you guarantee that the value was specified by the user? Is this supposed to be a hidden input field?
Either way your input tag is incomplete. This sounds better:
<input type="text" name="sentvalue"></input>
Also, when you submit, the value of this field (sentvalue) will be passed in too. As long as your tags are right you don't need to worry.
You will have to create a hidden input element
<input type='hidden' id='sentvalue' value='' />
and call a function on submit instead of directly submitting
Use this in the function to set the values and submit
var e = document.getElementByName("car");
document.getElementById("sentvalue").value = e.options[e.selectedIndex].value;
this.form.submit();
I have a hidden input in the manner below:
<div id="message">
<input id="hiddeninput" type="hidden">
<span>Message with submit button <input type=button id="confirm" value="Submit"></span>
</div>
The hidden input is given a value after a jQuery POST. I need to retrieve the value that is set, and send it in another jQuery POST.
Interestingly, I get this:
<input id="hiddeninput" type="hidden">34345</input>
after fetching the value from the server in the first jQuery post.
Just $("#hiddeninput").val() does not retrieve the value which I want to send.
What is the correct way to do it in my example?
EDIT: In JQuery, This is how I set the value to the hidden field:
$.post("post.php", function(data){
if(data.length > 0){
var resultObj = eval(data)[0];
if(resultObj.SomeNumber >= 0)
{
$("#hidden").html(resultObj.SomeNumber);
}
});
You have to set the value of the hidden field like this, then it should work
<input id="hiddeninput" type="hidden" value="34345" />
The hidden element
<input id="hiddeninput" type="hidden">
does not have the value attribute. It should be like
<input id="hiddeninput" type="hidden" value="someValue">
$("#hiddeninput").html() would retrieve 34345 from the structure as you show although as stated above the value attribute should be used on a hidden field and then val() will work.
Since <input id="hiddeninput" type="hidden">34345</input> is not the "right" way to format the input tag,a s opposed to <input id="hiddeninput" type="hidden" value="34345"/> you need to use $("#hiddeninput").text()
try explicitly adding the value tag to the input elemeent before the post adds it i.e.
<input id="hiddeninput" type="hidden" value="">
If that doeesn't work have a look at this thread:
jquery selector can't read from hidden field