how to set input field as readonly or entry field - php

I am having input item if data is available the field should be readonly else
required field.
<input type="text" name="login_password"
id="login_password"
placeholder="Desgination" value="<?php echo
$row["designation"] ?>" Readonly required>

You can try this out, if there is some value in the field it will echo readonly else it will be blank field with required
<input type="text" name="login_password" id="login_password" placeholder="Desgination" value="<?php echo $row["designation"]; ?>" <?php if(trim($row["designation"]) !="") echo "Readonly"; else echo "required"; ?> >

My 2 cents! Without if inside the html code, a bad habit imho
$readonly = isset($row["designation"]) ? 'required' : '';
<input type="text" name="login_password" id="login_password" placeholder="Desgination" value="<?php echo $row["designation"] ?>" <?php echo $readonly; ?> required>

Try This
<?php
if(!empty($row["designation"])){
$condition = "readonly";
else
$condition = "required";
?>
<input type="text" name="login_password" id="login_password" placeholder="Desgination" value="<?php echo
$row["designation"] ?>" <?php echo $condition; ?>>

Related

HTML code conversion to PHP and merge with html

I need help for this conversion of HTML to PHP
This is my code and I want to write inside the if tag with echo to get the results of the base of conditions for more info I am getting the error around the value of the input field from the database.
<td>
<input type="text" name="sale_rate[]" onchange="getProductData(<?php echo $x; ?>)" id="sale_rate_<?php echo $x; ?>" class="form-control" autocomplete="off" value="<?php echo $val['sale_rate'] ?>">
<input type="hidden" name="sale_rate_value[]" id="sale_rate_value_<?php echo $x; ?>" class="form-control" autocomplete="off">
</td>
to like this
<?php if($user_id == 1)
echo "<td><input type="text" name="sale_rate[]" onchange="getProductData(<?php echo $x; ?>)" id="sale_rate_<?php echo $x; ?>" class="form-control" autocomplete="off" value="<?php echo $val['sale_rate'] ?>">
<input type="hidden" name="sale_rate_value[]" id="sale_rate_value_<?php echo $x; ?>" class="form-control" autocomplete="off">
</td>";
?>
Any kindly of help will be so appreciated
You can close and reopen <?php ?> tags. So you don't need echo
<?php if($user_id == 1){ ?>
<td>
<input type="text" name="sale_rate[]" onchange="getProductData(<?php echo $x; ?>)" id="sale_rate_<?php echo $x; ?>" class="form-control" autocomplete="off" value="<?php echo $val['sale_rate'] ?>">
<input type="hidden" name="sale_rate_value[]" id="sale_rate_value_<?php echo $x; ?>" class="form-control" autocomplete="off">
</td>
<?php } ?>
Alternatively, you can use if/endif syntax:
<?php if($statement): ?>
<some-html>...</some-html>
<?php endif; ?>
There are many different approaches, which amount to as much a matter of style as anything else.
Personally, I prefer to not have numerous instances of <?php [CODE HERE] ?> throughout my markup, so, instead, I would favour something more like this:
<?php
if($user_id == 1) {
echo '
<td>
<input type="text" name="sale_rate[]" onchange="getProductData('.$x.')" id="sale_rate_'.$x.'" class="form-control" autocomplete="off" value="'.$val['sale_rate'].'">
<input type="hidden" name="sale_rate_value[]" id="sale_rate_value_'.$x.'" class="form-control" autocomplete="off">
</td>
';
}
?>

pass value from $_GET to $_POST

So, I have a disabled email form field whose placeholder and value are retrieved from $_GET['email']:
<input name="email" type="text" placeholder="<?php if(isset($_GET['email'])) echo $_GET['email']; ?>" value="<?php if(isset($_GET['email'])) echo $_GET['email']; ?>" disabled>
When the user fills out the form, I was hoping that $_POST['email'] would have the email value, but it doesn't (it's empty). What am I missing/forgetting? Is there a clever way to pass this value along? Thanks!
change attribute disabled to readonly because disabled not submit values..
<input name="email" type="text" placeholder="<?php if(isset($_GET['email'])) echo $_GET['email']; ?>" value="<?php if(isset($_GET['email'])) echo $_GET['email']; ?>" readonly>
As Cuchu stated, you could use readonly instead of disabled. Or you could duplicate the field and change the type to hidden.
<form method="post" action="register.php">
<input type="text" value="<?php if(isset($_GET['email'])) echo $_GET['email']; ?>" disabled>
<input name="email" type="hidden" value="<?php if(isset($_GET['email'])) echo $_GET['email']; ?>">
</form>
I think it is better to use the readonly attribute instead of disabling the input.
<input name="email" type="text" placeholder="<?php if(isset($_GET['email'])) echo $_GET['email']; ?>" value="<?php if(isset($_GET['email'])) echo $_GET['email']; ?>" readonly="readonly">
Placeholder is a prompt, not a value. If you want the textfield to have a value of the email, use the "value" attribute, not the "placeholder".
You should enclose in a form tag and set the method to post as
<form action="" method="post">
<input name="email" type="text" placeholder="<?php if(isset($_GET['email'])) echo $_GET['email']; ?>" value="<?php if(isset($_GET['email'])) echo $_GET['email']; ?>" disabled>
</form>
//try this
<form method="post">
<input type="text" name="email"><input type="submit" value="click" name="btnClick" id="btnClick">
<input type="text" name="email1" placeholder="inserted value" value="<?php echo (isset($_POST['email']))? $_POST['email'] : "" ?> ">
</form>

Display field from different table in a input form

How would I display student.Student instead of student_id
<?php
for ($i = 0; $i < $chkcount; $i++) {
$id = $chk[$i];
$res = $MySQLiconn->query("SELECT Student.ID, student.Student, student.School,student.PR, Jumps.dis_ft_1, Jumps.dis_in_1, Jumps.dis_ft_2, Jumps.dis_in_2, Jumps.dis_ft_3, Jumps.dis_in_3, Jumps.dis_ft_4, Jumps.dis_in_4, Jumps.dis_ft_5, Jumps.dis_in_5, Jumps.dis_ft_6, Jumps.dis_in_6
FROM Student LEFT JOIN Jumps ON Student.ID = Jumps.student_id WHERE ID=" . $id);
while ($row = $res->fetch_array()) {
?>
<tr>
<td>
<input type="hidden" name="id[]" value="<?php echo $row['ID']; ?>"/>
Student ID: <input type="text" name="st[]" value="<?php echo $row['student_id']; ?>"
class="form-control"/>
Attempt 1 <input type="text" name="df1[]" value="<?php echo $row['dis_ft_1']; ?>"
class="form-control"/>
<input type="text" name="di1[]" value="<?php echo $row['dis_in_1']; ?>"
class="form-control"/>
Attempt 2 <input type="text" name="df2[]" value="<?php echo $row['dis_ft_2']; ?>"
class="form-control"/>
<input type="text" name="di2[]" value="<?php echo $row['dis_in_2']; ?>"
class="form-control"/>
Attemp 3 <input type="text" name="df3[]" value="<?php echo $row['dis_ft_3']; ?>"
class="form-control"/>
<input type="text" name="di3[]" value="<?php echo $row['dis_in_3']; ?>"
class="form-control"/>
Attempt 4 <input type="text" name="df4[]" value="<?php echo $row['dis_ft_4']; ?>"
class="form-control"/>
<input type="text" name="di4[]" value="<?php echo $row['dis_in_4']; ?>"
class="form-control"/>
Attempt 5 <input type="text" name="df5[]" value="<?php echo $row['dis_ft_5']; ?>"
class="form-control"/>
<input type="text" name="di5[]" value="<?php echo $row['dis_in_5']; ?>"
class="form-control"/>
Attempt 6 <input type="text" name="df6[]" value="<?php echo $row['dis_ft_6']; ?>"
class="form-control"/>
<input type="text" name="di6[]" value="<?php echo $row['dis_in_6']; ?>"
class="form-control"/>
</td>
</tr>
<?php
}
}
?>
Not sure it can be done. But Student name would be better than student id.
Wouldn't post with more text so just typing so it will post.
I am learning PHP.
If there is only one field called Student i.e Student.Student, then you could just output the name <?php echo $row['Student']; ?>.
However, if there are ever name clashes, you can always give the field a new name for PHP to access.
SELECT Student.ID, Student.Student AS some_new_name FROM ....
<?php echo $row['some_new_name']; ?>

Save input for next input

Well, I have this code:
<form name="download" id="download" method="post" enctype="multipart/form-data">
<div>
<label for="beginning_date"><span class="required">*</span> Beginning Date: </label>
<input type="text" size="30" id="beginning_date" name="beginning_date" value="<?php echo date("d.m.Y"); ?>" required="required" />
</div>
<input type="submit" value="Next" name="next"/>
</form>
and php..
if(array_key_exists('next', $_POST))
{
if (preg_match('/^\d{1,2}\.\d{1,2}\.\d{4}$/', $date))
{
//the code continues...
}
else
{
echo "Wrong formation <br />";
}
}
This is a long form, and I want that if someone inputs a date like this "02.03.20322", and the error appears, they don't have to start over to fill the form... the form already appears with the "fills field"...
I've already tried like these:
<input type="text" size="30" id="beginning_date" name="beginning_date" value="<?php if (isset($date)) { echo $date; } else { echo date("d.m.Y"); } ?>" required="required" />
but... no changes :(
Looking at your code, there simply is no $date so what you should check for is the existence of $_POST['beginning_date'] instead of $date like so:
<input type="text" size="30" id="beginning_date" name="beginning_date" value="<?php if (isset($_POST['beginning_date'])) { echo $_POST['beginning_date']; } else { echo date("d.m.Y"); } ?>" required="required" />
When using method post the submitted values of the user go into the $_POST array. The name of the form element is used as key.
So to retrieve the date submitted by the user, you should use:
$_POST['beginning_date']
Just save it in $_SESSION,
at the top of page or anywhere in your action.php:
if (isset($_POST)){
$_SESSION['date'] = $_POST['beginning_date'];
}
and then:
<input type="text" size="30" id="beginning_date" name="beginning_date" value="<?php echo (isset($_SESSION['date'])) ? $_SESSION['date'] : date("d.m.Y");?>" required="required" />
Using session is useful in case that in the future you need to change your action page. but if you want to use post instead(for same page posting), just echo it:
<input type="text" size="30" id="beginning_date" name="beginning_date" value="<?php echo (isset($_POST['beginning_date'])) ? $_POST['beginning_date'] : date("d.m.Y"); ?>" required="required" />
php..
session_start();
if(array_key_exists('next', $_POST))
{
if (preg_match('/^\d{1,2}\.\d{1,2}\.\d{4}$/', $date))
{
//the code continues...
}
else{
echo "Wrong formation <br />";
$_SESSION['date']=$_POST['beginning_date'];
}
}
html..
<input type="text" size="30" id="beginning_date" name="beginning_date" value="<?php if
(isset($_SESSION['date'])) { echo $_SESSION['date'] } else { echo date("d.m.Y"); } ?>"
required="required" />

Counting $_POST

I have a big form that contains X amount of posts that has 15 fields per post along with 1 hidden field.
Let's assume I have 14 posts. This means my form would send 211 fields (14x15 fields plus 1 hidden field).
The user does not have to fill in all fields.
I want to count the number of posts that the form sends but I seem to be running into difficulty.
Using count($_POST) returns 152. This leads me to believe that count() is ignoring empty fields.
As a result, using a formula such as (count($_POST) - 1) / 15 would return the wrong result (10.0666) and is inefficient should the number of fields change in the future.
So, does anyone have any ideas as to how to get the proper count of my posts?
My form looks like so:
<form name="scraped" action="<?php echo str_replace( '%7E', '~', $_SERVER['REQUEST_URI']); ?>" method="post">
<input type="hidden" name="OSscraper_hidden" value="N">
<?php
$inpCnt = 0;
foreach($articles as $item) {
?>
<input type="text" name="title_<?php echo $inpCnt; ?>">
<input type="text" name="name_<?php echo $inpCnt; ?>">
<input type="text" name="url_<?php echo $inpCnt; ?>">
<input type="text" name="img_<?php echo $inpCnt; ?>">
<input type="text" name="pet_<?php echo $inpCnt; ?>">
<input type="text" name="color_<?php echo $inpCnt; ?>">
<input type="text" name="value_<?php echo $inpCnt; ?>">
<input type="text" name="height_<?php echo $inpCnt; ?>">
<input type="text" name="weight_<?php echo $inpCnt; ?>">
<input type="text" name="hair_<?php echo $inpCnt; ?>">
<input type="text" name="eyes_<?php echo $inpCnt; ?>">
<input type="text" name="race_<?php echo $inpCnt; ?>">
<input type="text" name="phone_<?php echo $inpCnt; ?>">
<input type="text" name="address_<?php echo $inpCnt; ?>">
<input type="text" name="zip_<?php echo $inpCnt; ?>">
<?php
$inpCnt++;
} ?>
<input type="submit" value="Submit">
</form>
Change your form to look like:
<input type="text" name="foo[<?php echo $inpCnt; ?>][title]">
<input type="text" name="foo[<?php echo $inpCnt; ?>][name]">
<input type="text" name="foo[<?php echo $inpCnt; ?>][url]">
Then you will get:
$_POST['foo'] = [
0 => ['title' => '...', 'name' => '...', 'url' => '...'],
1 => ...,
...
];
It saves you from having to do the grouping yourself, and is easier to count or iterate over the input.
why not just count($articles)*15 and echo into a hidden input. You are using another hidden input anyway....
Try this code, and demo is here
Please just use the idea not the exact copy.
<?php
error_reporting(E_ALL ^ E_NOTICE);
//debugging
if(#$_POST['submit'] == 'Submit'){
echo '<pre>';
print_r($_POST);
echo '</pre>';
echo "<br>\n";
echo 'Number of posts = count($_POST["posts"])='.count(#$_POST['posts'])."<br>\n";
//finding number of posts that are set and not empty
$count = 0;
foreach($_POST['posts'] as $v1){
//$v is an array
foreach($v1 as $v1k=> $v1v){
if(strlen($v1v) > 0){
++$count;
$inputs[$v1k] = $v1v;
}
}
}
echo 'Count of non-empty posts = $count = '.$count."<br>\n";
echo '<pre>';
print_r($inputs);
echo '</pre>';
}
?>
<form name="scraped" action="" method="post">
<input type="hidden" name="OSscraper_hidden" value="N">
<?php
$articles =array('test');
$inpCnt = 0;
foreach($articles as $item) {
?>
<input type="text" name="posts[][title_<?php echo $inpCnt; ?>]">
<input type="text" name="posts[][name_<?php echo $inpCnt; ?>]">
<input type="text" name="posts[][url_<?php echo $inpCnt; ?>]">
<input type="text" name="posts[][img_<?php echo $inpCnt; ?>]">
<input type="text" name="posts[][pet_<?php echo $inpCnt; ?>]">
<input type="text" name="posts[][color_<?php echo $inpCnt; ?>]">
<input type="text" name="posts[][value_<?php echo $inpCnt; ?>]">
<input type="text" name="posts[][height_<?php echo $inpCnt; ?>]">
<input type="text" name="posts[][weight_<?php echo $inpCnt; ?>]">
<input type="text" name="posts[][hair_<?php echo $inpCnt; ?>]">
<input type="text" name="posts[][eyes_<?php echo $inpCnt; ?>]">
<input type="text" name="posts[][race_<?php echo $inpCnt; ?>]">
<input type="text" name="posts[][phone_<?php echo $inpCnt; ?>]">
<input type="text" name="posts[][address_<?php echo $inpCnt; ?>]">
<input type="text" name="posts[][zip_<?php echo $inpCnt; ?>]">
<?php
$inpCnt++;
} ?>
<input type="submit" value="Submit" name="submit">
</form>

Categories