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);
});
Related
I am trying to get the value of a form (text field) with _POST, and store it to a text file but it doesn't work. Here's my code:
HTML
<form>
<input type="text" name="test" id="test" value="">
<input type="button" onclick="location.href='example.com/index.php?address=true';" value="ODOSLAŤ" />
</form>
PHP
if (isset($_GET['address'])) {
$email = $_POST['test'];
$myfile = fopen("log.txt","a") or die("Error.");
fwrite($myfile, "\n".$email);
// this prints nothing
echo $email;
}
I can't get the value of that text field. Nor GET nor POST doesn't work for me. What am I doing wrong?
You are missing a post method in your form.
<form method="post" action="example.com/index.php?address=true">
<input type="text" name="test" id="test" value="">
<input type="submit" value="ODOSLAŤ" />
</form>
You also have to change the following line.
if (isset($_GET['address'])) {
With
if (isset($_POST['address'])) {
You want to post after triggering an action as FreedomPride mentioned
Conclusion
You want to declare for example a post methodif you know that you would like to post that data in the future.
As FreedomPride also mentioned :
You are using a GET, but if a user does not input anything your script won't work, there by it is recommended to use a POST
You are not submitting your form.
Change your code as follows....
<form method="post" action="example.com/index.php?address=true">
<input type="text" name="test" id="test" value="">
<input type="submit" value="ODOSLAŤ" />
</form>
You'll get what you want....
This is what you're missing as Tomm mentioned.
<form method="post" action="yourphp.php">
<input type="text" name="address" id="test" value="">
<input type="submit" name="submit"/>
</form>
In your PHP, it should be post if it was triggered
if (isset($_POST['address'])) {
$email = $_POST['test'];
$myfile = fopen("log.txt","a") or die("Error.");
fwrite($myfile, "\n".$email);
// this prints nothing
echo $email;
}
Explanation :-
In a form, an action is required to pass the action to the next caller with action. The action could be empty or pass it's value to another script.
In your PHP you're actually using a GET. What if the user didn't input anything. That's why it's recommended to use POST .
Say I have a HTML form that lets you input a name and age and returns with a list of people with that name and age.
<form method="post" action="/search_results">
<input type="text" name="personName">
<input type="text" name="personAge">
<input type="submit" value="Submit!">
</form>
And on the search results page I have a list of the results, but I only display 50 at a time and allow users to go forwards/backwards between the page with buttons. So the results page would also look for a POSTed 'pageNumber' value and default to 0 if there is none.
When they click a button, how would I resubmit the age and name and also submit the corresponding pageNumber from the button?
I'm using PHP
Add a hidden field to the form:
<form name=search"" method="post" action="/search_results">
<input type="hidden" name="pageNumber"
value="<?php echo isset($_POST['pageNumber']) ? (int) $_POST['pageNumber'] : 0; ?>">
<input type="text" name="personName">
<input type="text" name="personAge">
<input type="submit" value="Submit!">
</form>
Add a JavaScript function to modify the hidden field value:
<script>
function search(pageNumber) {
var form = document.forms.search;
if (!form) return;
form.elements.pageNumber.value = pageNumber;
form.submit();
}
</script>
Apply the JavaScript function for the page buttons:
<span onclick="search(1)">1</span>
<span onclick="search(2)">2</span>
Obviously, the buttons should be generated with PHP in the following manner:
<?php
for ($p = 0; $p < $pagesNum; ++$p) {
echo "<span onclick='search($p)'>$p</span>";
}
?>
<form method="post" action="/search_results">
<input type="text" name="personName" value="<?php echo (isset($formdata['personName'])?$formdata['psersonName']:"") ?>">
<input type="text" name="personAge" value="<?php echo (isset($formdata['personAge'])?$formdata['personAge']:"") ?>">
<input type="hidden" name="currentPage" value="<?php echo (isset($formdata['currentPage'])?$formdata['currentPage']:"0") ?>">
<input type="submit" value="Submit!">
</form>
formdata are the data which are the inputs of the previous submit. These data should be returned by the search_results page along with the view.
Below is the js
<script>
$(document).ready(function(){
$(".paginationBtns").click(function(){
var page = $(this).attr('pageValue');
$("input[name='current']").val(page);
$("form").submit();
});
});
</script>
Assumptions of the forward and backward button
<a href="#" pageValue=0>Back</a><a href="#" pageValue=50>Next</a>
You have to append the back and next pageValue while loading each page.
The best practice in pagination to use a simple link that contains the parameres (GET) and not (POST). That way, when you have the parameters in the url you can cache it, add to favorites, get indexed by google, share your page in email/facebook etc.
<a href='http://example.com/?personName=<?=$_POST['personName']?>&personAge=<?=$_POST['personAge']?>&page=<?=$next_page_number?>'>Next</a>
If for some reason you must or really want to use POST you can save the values in hidden inputs within a form in the page and then sumbit it when clicking on "next page" button.
Form example:
notice its just an example you should sanitize the variables and not put them directly from the $_POST
<form name="pagination" method="post" action="/search_results">
<input type="hidden" name="page" id="page" value="2">
<input type="hidden" name="personName" value='<?=$_POST['personName'];?>'>
<input type="hidden" name="personAge" value='<?=$_POST['personAge'];?>'>
</form>
notice that we set the next page numbers & do submit by using a command from the form of:
<button onclick='document.getElementById("page").value= "2";document.pagination.submit();'>Next page</button>
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 am taking input from user in a form. Can I get that value(user input) to calculate other fields in the same form.
<input size="12" id="inputField" name="inputField" autofocus="" type="date" onblur="return dateValidate(this)"/>
Can I collect this form input later in the form and use it to calculate other fields. I was trying to using to use $_POST to retrieve the value but I am not sure if thuis is the right thing.
You cannot do it in PHP before submitting the form. You can easily do so in JavaScript. However you could simply add some AJAX code to send the value to your PHP script when user enters something in the box, and parse the response accordingly. PHP works on server side, and does not interact with user without any server side request
You can certainly do it in 2 ways...
1st way
<?php
$first_digit = '';
$second_digit = '';
$third_digit = '';
if(isset($_POST['calculate'])) {
$first_digit = $_POST['first_digit'];
$second_digit = $_POST['second_digit'];
$third_digit = $first_digit + $second_digit;
}
?>
<form method="POST">
<input type="text" name="first_digit" value="<?php if(isset($first_digit)) echo $first_digit; ?>" />
<input type="text" name="second_digit" value="<?php if(isset($second_digit)) echo $second_digit; ?>" />
<input type="text" name="third_digit" readonly value="<?php if(isset($third_digit)) echo $third_digit; ?>" />
<input type="submit" name="calculate" value="Calculate" />
</form>
2nd Way
Total the variables in the code and instead of showing the result output in a text box you can instead calculate and echo out the result, or you can store that in the database.
Note: Be sure you keep the third input[type=text] as readonly as it is showing you the calculated value so probably you don't want your users to change
Yes you can get input from user to calculate other fields in the same form, you should use scripts like OnChange() or onClick() to have this.
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()