Javascript onclick html field value without editing .js file - php

<input type="text"
name='username'
onblur="if(this.value=='')this.value='Username';"
onfocus="if(this.value=='Username')this.value='';"
value="Username"
class="register"/>
This is the code that I am using but the way my code is setup I want the text to be viewable in the field before the user types in their information but the way the form is setup now the
value="<?php echo $db_first_name; ?>"
but the code i'm using needs
value="Username"
to display username in the field before clicked by user and put in their own information.
I tried
value="Username<?php echo $db_first_name; ?>"
But obviously that did not work. Any Ideas?
Link to view functionality of form: http://odconnect.com/beta/registration2.php

Try the placeholder attribute (valid as of HTML5):
<input type="text" name="username" id="username" placeholder="Username" value="<?= $db_first_name; ?>" />
Alternatively (in other words, if you want to support users whose browsers don't support HTML5), you can test to see whether the user already has a username entered. If it's null or an empty string, set the value to "Username" otherwise, set the value to the value of $db_first_name. Then change the onBlur event to check for the length of the value of the field, and if the length is zero, have it display "Username". Finally, change the onFocus event to listen for a mouse-click - when the user gives focus to the field, set the value to an empty string.
<input type="text" name="username" id="username" value="<?= $db_first_name == "" ? "Username" : $db_first_name ?>" onBlur="checkUsernameBlur(this, 'Username');" onFocus="checkUsernameFocus(this, 'Username');" />
<script type="text/javascript" language="javascript>
function checkUsernameBlur(inputObj, label) {
var strLen = inputObj.value.length;
if (strLen == 0) {
inputObj.value = label;
}
}
function checkUsernameFocus(inputObj, label) {
var value = inputObj.value;
if (value == label) {
inputObj.value = "";
}
}
</script>

To work in lower html versions not only in html5 change the Username text in all the three places to the wanted word or php variable
<input type="text" name='username' name='username' onblur="if(this.value=='')this.value='<?php echo $db_first_name; ?>';" onfocus="if(this.value=='<?php echo $db_first_name; ?>')this.value='';" value="<?php echo $db_first_name; ?>" class="register"/>

Related

Input is being excluded when form is submitted

1 Things first: My I have abbreviated htmlspecialchars() to h() in my custom API.
Have an input where everything else is showing up when I run <pre><?php print_r($_POST) ?></pre> But this particular input isn't
<input id="phone_description_<?php echo $phone_count; ?>" type="text"
name="phone[<?php echo $phone_count; ?>]['phone_description']"
value="<?php echo h($phone['phone_description']); ?>"
<?php
if ($phone['phone_description'] == 'Primary') {
echo ' disabled';
}
?>
placeholder="e.g. Adwords Tracking Number"
class="phone_desc"
/>
$phone_count is a counting variable in a foreach loop just FYI. Not really sure why an input with a name attribute isn't even showing up in the $_POST array.
Edit
Here is the code that is generated from the above code
<input id="phone_description_0" type="text" name="phone[0]['phone_description']" value="Primary" disabled placeholder="e.g. Adwords Tracking Number" class="phone_desc" />
The input will be skipped by POST when it is disabled. Use "readonly" instead.

How to loop inside textbox in PHP

How can I loop numbers inside a textbox whenever page is refresh? in PHP
<input type="text" name="name1" value="loophere">
I want a textbox automatically have a value number whenever I refresh a page.
I need to used this for a column in my database and I want it automatically. I already have one column auto_increment in my database. Big thanks.
Use rand()
<input type="text" name="name1" value="<?php echo "1".rand();?>">
http://php.net/manual/en/function.rand.php
OR Use str_shuffle()
<?php
$string = "123456789AaBbCcDdEeFfGgHhIiJjKkLlMmNnPpQqRrSsTtWwXxYyZz"; //You can use any string
$key_len = "5";
$string_shuffle = str_shuffle($string);
$session_key = str_shuffle(substr($string_shuffle, 3, $key_len));
?>
<input type="text" name="name1" value="<?php echo $session_key;?>">

Default value if input field empty

I have a search form with various field but i want to set a default into the url if the field is empty:
<input type="text" name="price-max" class="form-control" placeholder="Max Price" >
and when the form is submited my url looks something like this
search.php?location=location&category=Category&status=lease&price-max=
instead i want a default value so that when i submit the form with an empty price field the url should like.
search.php?location=location&category=Category&status=lease&price-max=999
(Just) Do it server-side:
$price_max = empty($_GET['price-max']) ? 999 : $_GET['price-max'];
// /\ default value
That way, when you use $price_max in your query, it will be either user input or the default value (999 - or whatever value you decide to go with).
You don't even have to mess with the url to achieve that.
The previous code is the same as:
if(empty($_GET['price-max'])){
$price_max = 999; // default value
}else{
$price_max = $_GET['price-max'];
}
Sidenotes:
You could combine the above code with trim;
There's info / example in the docs: Example #3 Assigning a default value;
You didn't show the php code, but make sure you are protected against sql-injections;
You could put your default value as placeholder in the input, just so the user 'sees' something in there.
If you want to put default value, you should define value in each input field.
<input type="text" value="defualt_value" name="price-max" class="form-control" placeholder="Max Price" >
Instead of sending the form directly, call a function to validate the form fields. In that function, check if that field is empty and then add your default value. Finally that function submits the form.
You just need to define feild default value
<input type="text" name="price-max" class="form-control" placeholder="Max Price" value="999">
1st method
Define value attribute in your input field but this will show your default value in text field
<form method = "GET" action="search.php">
<input type="text" name="price-max" class="form-control" placeholder="Max Price" value = "<?php echo $_GET['price-max'] ? $_GET['price-max'] : 999 ?>">
<input type="submit" value="submt">
if you didn't input then it will carry price-max = 999 otherwise whatever you will type
2nd Method
Use jQuery or javascript for this
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<form method = "GET" action="search.php" id="my_form">
<input type="text" name="price-max" id="price-max" class="form-control" placeholder="Max Price">
<input type="submit">
</form>
<script type="text/javascript">
$(document).ready(function() {
$("#my_form").submit(function() {
if($("#price-max").val()=="") {
$("#price-max").val('999');
}
});
});
</script>
I have added id attribute to form and price-max field
in controller example
public function getMessages(Request $request){
$page = request('page', 1);
$limit = request('limit', 100);
$status = request('status', 'all');
$sort = request('sort', 'asc');
$id = request('id', 'asc');
$referenceId = request('referenceId', '');
$from = request('from', '');
$to = request('to', '');
$ack = request('ack', '');
.......
}

hidden array returns OK but text array is empty html to PHP

Form:
<form method="POST" action="edit_work.php">
<input type="hidden" name="wid[]" size="1" value="<?php echo "$wid1" ?>" >
<input type="text" name="course[]" size="15" value="<?php echo "$course1" ?>" >
PHP:
extract($_POST);
for($i=0;$i<$count;$i++) {
echo $wid[$i];
echo $course[$i];
}
gives the wid values OK but not the text entered for the course names.
I have been through all forums for 2 days now. Any help? Thanks.
If you want your PHP to retrieve your data from the form, can't you name your text input "course", then get it inside your PHP with $_POST['course'] ?
What is your $count ?
Using brackets with your name attribute inside your input tag may be dangerous.
If you're using a list of inputs maybe you can define a text format like name="course#" where # is your index and then access it form your $_POST variable using $_POST['course'.$index]
You don't need to extract($_POST) in that case.

Is it possible to check whether a checkbox has been ticked within it's value?

To save me making masses of edits to the rest of my code I want to make a checkbox act like a textbox.
I have the following textbox, that if a number is greater than 0 then it adds items to a basket.
<input type="textbox"
value="<? echo $item[qty]; ?>"
name="<? echo $productid."_".$product_quantity[id]."_".$product_option[id]; ?>"
/>
How can I make a checkbox act the same way? I.e If it's ticked it's value equals 1, therefore adding it to my basket. If it remains unticked then it's value equals 0, therefore ignored.
<input type="checkbox"
value="<? echo $item[qty]; ?>"
name="<? echo $productid."_".$product_quantity[id]."_".$product_option[id]; ?>"
/>
I have several of these checkboxes going down the page.
Change the value from value="<? echo $item[qty]; ?>" to value="1"
In the action of you form i.e. wishlist.php :
if(isset($_POST["name_of_checkbox"])) {
//It means check box has been ticked
$myvalue = 1;
} else {
//It means check box was not ticked
$myvalue = 0;
}
HTH
This is another approach to the question maybe this is what you need.
I would introduce a hidden field which will store/post the 0 or 1 value. We will bind an event with the checkbox that will update the value of the hidden input.
<input name="my_cb" id="my_cb" type="checkbox" value="1" />
<input type="hidden" name="my_cb_hidden" id="my_cb_hidden" value="" />
<script type="text/javascript">
$("#my_cb").click(function() {
if($("#my_cb").is(":checked")){
$("#my_cb_hidden").val($(this).val());
} else {
$("#my_cb_hidden").val(0);
}
alert($("#my_cb_hidden").val());
});
you can change the value of checkbox value from
value="<? echo $item[qty]; ?>"
to
value="1"
then if it is checked then on Submit it will automatically store 1 as a value
EDITED :
after submitting in php code do it as as you updated your question
if(isset($_POST['checkbox name']))
{
$item[qty] = 1;
}
else
{
$item[qty] = 0;
}
but this will only make the changes in the $item array's value inspite of the value your checkbox hold.

Categories