PHP result display in textbox onClick - php

I need to get result where PHP generated ID for current button click is displayed in html textfield...
Is there any easy way of doing this?
This is the link of what I am trying to do http://dtech.id.lv/lat.php
When user first comes he doesn't have code, so he clicks get code.
I need that php generated code is pasted in textbox after this click...
Could someone help?
Thank you!

Button clicked, something shows in the text field. Sometimes Javascript isn't necessary.
<?php
$code = "";
if (isset($_POST['buttonId']))
{
$code = 'Some code here';
}
?>
<form method="post" >
<input name="textField" id="textField" type="text" value="<?php echo $code; ?>" />
<input name="buttonId" id="buttonId" type="submit" value="Submit" />
</form>

Assuming your html looks like this:
<button id="myPHPid">...</button>
<input type="text" id="idRecipient" />
then
<button id="myPHPid" onclick="document.getElementById('idRecipient').value = this.id">...</button>

try this code
$('button').live('click',function() {
var $butVal= $(this).val();
alert($butVal);
$('input#buttonValue').val($butVal);
return false;
});
live DEMO

Related

Undefined Index when passing data to different HTML5 page

The following is one of my HTML pages.
<form id="regForm" method="post" action="enquiry_process.php" novalidate="novalidate">
<fieldset>
<legend>Personal Details</legend>
<label>First Name:</label>
<input type="text" name="owner" id="owner" /><br />
<label>Last Name:</label>
<input type="text" name="owner2" id="owner2" /><br />
</fieldset>
<p>
<input type="Submit" onclick="validateForm()"/>
<input type="Reset" value="Reset" />
</p>
</form>
The following is my 2nd HTML page.
<form id="bookForm" method="post" action="view_enquiry.php">
<?php
$fname = $_POST['owner'];
$lname = $_POST['owner2'];
?>
<input type="hidden" name="owner" value="<?php echo $fname; ?>">
<input type="hidden" name="owner2" value="<?php echo $lname; ?>">
<fieldset>
<legend>User Details</legend>
<p>Your First Name: <span id="confirm_fname"></span></p>
<p>Your Last Name: <span id="confirm_lname"></span></p>
<input type="submit" name="submit" value="Confirm Booking" />
<input type="button" value="Cancel" id="cancelButton" onclick="cancelBooking()" />
</fieldset>
The functions you see like validateForm() and cancelBooking() are Javascript functions that validate my form or return the user from the 2nd page to the 1st and I believe they have nothing to do with my question.
When I click submit on the first HTML page, it should pass on the value of the owner and owner2 to the 2nd page right?
I keep on getting Undefined index and after looking around, it seems like I have to use isset() or empty() in my PHP, but this seems to only mask my notices but does not actually fix it? When I just add isset(), it ends up giving my 3rd page Undefined Variable. The method on my forms are already post.
Is there another problem here? Thank you.
EDIT: The following is are my relevant Javascripts.
ValidateForm:
function validateForm(){
"use strict";
gErrorMsg = "";
var nameOK = chkOwnerName();
var nameOK2 = chkOwnerName2();
var isAllOK = (nameOK && nameOK2);
if(isAllOK){
isAllOK = storeBooking();
}
else{
alert(gErrorMsg);
gErrorMsg = "";
}
return isAllOK;
}
Storebooking:
function storeBooking() {
"use strict";
sessionStorage.firstname = document.getElementById("owner").value;
sessionStorage.lastname = document.getElementById("owner2").value;
window.location = "enquiry_process.php";
}
I have another function called getbooking that runs with the condition window.onload
function getBooking(){
//if sessionStorage for username is not empty
if((sessionStorage.firstname != undefined)){
//confirmation text
document.getElementById("confirm_fname").textContent = sessionStorage.firstname;
document.getElementById("confirm_lname").textContent = sessionStorage.lastname;
}
chkOwnerName and chkOwnerName2 are functions that validate the form with patterns and I don't think they're relevant.
I also updated my 2nd HTML page with Javascript related contents because I assumed it wasn't relevant at first.
You can debug with below code by adding it in your 2nd form page.
echo "<pre>"; print_r($_POST); die;
If your form data is not going to your 2nd form then Array() will come as empty.
u can try by print_r($_REQUEST[]); on second form top page (enquiry_process.php) , i hope the both the form is in same folder and name of second form page is "enquiry_process.php" .
Since u r sending data using post form u should be able to retrieve it by print_r($_POST); or print_r($_REQUEST);
"if(empty($var))" and "if(isset($var))" are conditions, they check something and execute code within "{}" if the test returns true. So they don't 'fix' problems.
Your script worked fine for me without your js. Maybe the problem.
Just try your code step by step. You will find what's wrong.

Pass unencoded url from form

I've set up a search box on the homepage, that should link into the Opencart search by passing a URL.
The problem I'm having is that the resulting URL is always unencoded:
store/index.php?route%3Dproduct%2Fsearch%26filter_name=test
What I'm aiming for is
store/index.php?route=product/search&filter_name=test
My current code is:
<?php
$storesearch = $_REQUEST['store-search'] ;
$filter = 'route=product/search&filter_name';
$filtered = html_entity_decode($filter);
?>
<form id="store" method="GET" action="http://localhost/vinmobile/store/index.php?<?php echo $storesearch; ?>">
<input type="text" id="store-search" name="<?php echo $filtered; ?>" value="Store Search" onclick="this.value = '';">
<input type="submit" name="" value="Search">
</form>
<?php echo $filtered; ?> // Just to show it onscreen to make sure it's written correctly
I've tried encode, decode, html_entity_decode, urldecode, rawurldecode...
I've tried the form method as GET, POST
I've tried the variable declaration as $_REQUEST, $_GET, $_POST
Every time, I still end up with the url looking like:
store/index.php?route%3Dproduct%2Fsearch%26filter_name=test
I'm sure it's something simple I've missed, but I 'think' I've tried everything and would be very grateful if someone could point me in the right direction.
Cheers
webecho
Ended up using Javascript instead and it works perfectly:
`<script>
function process()
{
var url="store/index.php?route=product/search&filter_name=" + document.getElementById("filter").value;
location.href=url;
return false;
}
</script>
<form id="store" onSubmit="return process();">
<input type="text" name="filter" id="filter" value="Store Search" onclick="this.value = '';">
<input type="submit" value="Search">
</form>`
Thanks for your suggestions though

Add Text Alternative if Not insert data in Form

How do I fill in the result by default before users enter details of their data.
For example my php code is like this
<b>Address:</b> <?php echo htmlentities($dnn['you_address'], ENT_QUOTES, 'UTF-8'); ?>
If before the user fill in the form of address, how do I display the default information.
For example
Address: Address has not added
You can simply use placeholder!
<input type="text" name="you_address" placeholder="Address has not added" value="<?php echo htmlentities($dnn['you_address'], ENT_QUOTES, 'UTF-8'); ?>" />
As in this PHP Fiddle -hit "run" or F9 to execute- something like this
<?php
$defVal = 'default input';
?>
<form action="" method="POST">
<input type="text" name="test-input" value="<?php echo $defVal?>">
<input type="submit" value="submit" name="submit">
</form>
<?php
if(isset($_POST['submit'])){
echo $_POST['test-input'];
}
?>
Using Javascript or Jquery. If use PHP only you will get result after you run submit form.
If you want to showing result before run submit form. You can use Javascript like alert below.
<script>
function check(){
if(document.getElementById('address').value==''){
alert("Address has not added");
}
And in your address input give id = "address" and onkeypress="check()"

clear html form data using php or javascript

I've got kind of a situation here : I have the following form :
<form action="sample.php" id="searchform" method="post">
<input type="text" id="key_words" name="key_words" value="<?php echo isset($_POST['key_words']) ? $_POST['key_words'] : '' ?>"style="width:377px;">
<input type="text" name="minimum_price" value="<?php echo isset($_POST['minimum_price']) ? $_POST['minimum_price'] : '' ?>">
<input type="text" name="maximum_price" value="<?php echo isset($_POST['maximum_price']) ? $_POST['maximum_price'] : '' ?>">
I'm using the php script in value because i need to keep the value in text box persistent. So, now i need to clear the values in text box when i click a button:
<button type="reset" value="clear" onclick="clearform()">Clear</button>
I've tried a few things and failed. Help please? JavaScript can also be used for clearform() method.
You just need to get the elements by id and set the value attribute to empty string:
function clearform()
{
document.getElementById('key_words').value = '';
//same thing for other ids
}
For your minimum_price and maximum_price you need to add an id since you only have name right now.
Also in this case since you don't want to use HTML's standard reset functionality don't make the button type reset.
It`s will be work for you:
<form action="sample.php" id="searchform" method="post">
<input type="text" id="key_words" name="key_words" value="test1"style="width:377px;">
<input type="text" name="minimum_price" value="test2">
<input type="text" name="maximum_price" value="test3">
<button type="reset" value="clear" onclick="clearform()">Clear</button>
</form>
<script>
function clearform() {
var form = document.getElementById("searchform");
var textFields = form.getElementsByTagName('input');
for(var i = 0; i < textFields.length; i++) {
textFields[i].setAttribute('value', '');
}
}
</script>

How to post multiple input fields

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);
});

Categories