HTML code conversion to PHP and merge with html - php

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>
';
}
?>

Related

how to set input field as readonly or entry field

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; ?>>

Codeigniter Show Data from Database into Textbox

I created an accounts page wherein I can view all accounts from the database. Each of them has an action button called View More. If I press View More, additional details from the database will be displayed in textboxes. I want to get the ID for each of the row and pass it to the controller in order for it to be viewed. However, I can't seem to make them show in the boxes.
Expected: Image
Current: Image
Controller:
function viewmore()
{
$userid = $this->input->get('userid', TRUE);
$data['view'] = $this->model_accounts->viewmore_user($userid);
$data['main_content'] = 'view_adminviewmore';
$this->load->view('includes/admin_viewmore_template', $data);
}
Model:
public function viewmore_user($userid)
{
$query= $this->db->select('*')->from('accounts')->where('userid', $userid)->get();
return $query->result();
}
View for the Action Button:
<?php foreach($users as $row): ?>
<tr>
<td><?php echo $row->firstname; ?></td>
<td><?php echo $row->lastname; ?></td>
<td><?php echo $row->username; ?></td>
<td><?php echo $row->address; ?></td>
<td class="action-button mobile-important">
<button type="button" class="btn btn-custom-3">View More</button>
</td>
<?php endforeach; ?>
</tr>
View for displaying the data:
<?php foreach($view as $row): ?>
<br>
<p> First Name </p>
<input class="form-control" id="sel1" type="text" placeholder="" value="<?php echo $row->firstname; ?>">
<br>
<p> Last Name </p>
<input class="form-control" id="sel1" type="text" placeholder="" value="<?php echo $row->lastname; ?>">
<br>
<p> Username </p>
<input class="form-control" id="sel1" type="text" placeholder="" value="<?php echo $row->username; ?>">
<br>
<p> Address </p>
<input class="form-control" id="sel1" type="text" placeholder="" value="<?php echo $row->address; ?>">
<br>
<p> E-mail Address </p>
<input class="form-control" id="sel1" type="email" placeholder="" value="<?php echo $row->email; ?>">
<br>
<p> Contact Number </p>
<input class="form-control" id="sel1" type="text" placeholder="" value="<?php echo $row->contactnum; ?>">
<br>
<?php endforeach; ?>
If you are using CodeIgniter, why not use the Form Helper?
You can use code like this:
<?php echo form_input('sel1', $row->firstname); ?>
instead of:
<input class="form-control" id="sel1" type="text" placeholder="" value="<?php echo $row->firstname; ?>">
Read some more about it here: https://www.codeigniter.com/user_guide/helpers/form_helper.html
your action button must be like this:
you can use <?="hello word!!!" ?> instead of <?php echo "hello word!!!" ?>
and base_url() return the root of your codeigniter folder that set
in application->config->config.php
<a href="<?=base_url()." YOUR_CONTROLLER_NAME/viewmore/".USER_ID ?>View More</a>
link above return <a href="localhost/YOUR_CONTROLLER_NAME/viewmore?id=USER_ID>View More</a>
and your function viewmore() must be change to function viewmore($id=0),this $id give USER_ID from your url
and you better to know url in codeigniter contain this:
Controller/Function/variable that you want pass to your function

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>

HTML & PHP - How to make multiple forms submittable

I have the following code which loops through an array to create forms that are filled with values (which the user will be able to edit) on a single page. There are as many forms as there are loops in the array, which is always changing.
<body>
<div id="main">
<?php
foreach($articles as $item) { ?>
<div id='container'>
<form>
Title: <input type="text" name="title" size="80" value="<?php echo $item[0]; ?>">
<br>
URL: <input type="text" name="url" size="80" value="<?php echo $item[1]; ?>">
<br>
End Date: <input type="text" name="endDate" value="<?php echo substr($item[7], 14, strpos($item[7], '#') - strlen($item[7])); ?>">
<br>
<?php
if (substr($item[8], 0, 2) === 'Su'){
} else {
?>
Start Date: <input type="text" name="startDate" value="<?php echo substr($item[8], 7, 9); ?>">
<?php } ?>
</form>
</div>
<?php } ?>
</div>
</body>
Now, I want the user to have a single submit button at the bottom of the page which will submit ALL the forms on the page to MySQL database. The problem is I don't know how to do that.
I know the submit button takes the format of
<input type="submit" value="Submit">
I am assuming I need to give each form in the loop a unique name but from there I am at a loss as to what my next step should be to actually send and receive the information from these multiple forms.
Any help would be appreciated. Thanks.
You can't submit more than one form at once. What is wrong with putting all sets of <input>s into a single form?:
<body>
<div id="main">
<form>
<?php
$inpCnt = 0;
foreach($articles as $item) {
$inpCnt++; ?>
<div id='container'>
Title: <input type="text" name="title_<?php echo $inpCnt; ?>" size="80" value="<?php echo $item[0]; ?>">
<br>
URL: <input type="text" name="url_<?php echo $inpCnt; ?>" size="80" value="<?php echo $item[1]; ?>">
<br>
End Date: <input type="text" name="endDate_<?php echo $inpCnt; ?>" value="<?php echo substr($item[7], 14, strpos($item[7], '#') - strlen($item[7])); ?>">
<br>
<?php
if (substr($item[8], 0, 2) === 'Su'){
} else {
?>
Start Date: <input type="text" name="startDate_<?php echo $inpCnt; ?>" value="<?php echo substr($item[8], 7, 9); ?>">
<?php } ?>
</div>
<?php } ?>
</form>
</div>
</body>
You need to be able to define each of these inputs aswel. So I've used the loop to give each one a unique name.

Post request is not sending all variables

I have a form which consists of rows set up like:
<div class="row unit" onmouseover="this.style.background = '#eeeeee';" onmouseout="this.style.background = 'white';" onclick="if(event.srcElement.nodeName != 'INPUT' && event.srcElement.nodeName != 'SELECT' && event.srcElement.nodeName != 'TEXTAREA'){goToByScroll(event.srcElement,-160);}">
<div class="col">
<div class="select">
<input type="checkbox" id="select<?php echo $i; ?>" name="select<?php echo $i; ?>">
</div>
</div>
<div class="col name">
<p><input class="searchable" type="text" id="name<?php echo $i; ?>" name="name<?php echo $i; ?>" value="<?php echo $name; ?>"></p>
<p>Badge ID: <input class="searchable" type="text" id="badge<?php echo $i; ?>" name="badge<?php echo $i; ?>" value="<?php echo $badge; ?>" style="width: 50px;"></p>
</div>
<div class="col phone">
<p>Work: <input class="searchable" type="text" id="phone<?php echo $i; ?>" name="phone<?php echo $i; ?>" value="<?php echo $phone; ?>"></p>
<p>Cell: <input class="searchable" type="text" id="cell<?php echo $i; ?>" name="cell<?php echo $i; ?>" value="<?php echo $cell; ?>"></p>
<p>Home: <input class="searchable" type="text" id="home<?php echo $i; ?>" name="home<?php echo $i; ?>" value="<?php echo $home; ?>"></p>
</div>
<div class="col email">
<p>Work: <input class="searchable" type="text" id="email<?php echo $i; ?>" name="email<?php echo $i; ?>" value="<?php echo $email; ?>"></p>
<p>Personal: <input class="searchable" type="text" id="perEmail<?php echo $i; ?>" name="perEmail<?php echo $i; ?>" value="<?php echo $perEmail; ?>"></p>
</div>
<div class="col file">
<p class="removeFile"><input type="text" id="filename<?php echo $i; ?>" name="filename<?php echo $i; ?>" class="file" value="<?php echo $filename; ?>" readonly>
Remove: <input type="checkbox" id="removeFile<?php echo $i; ?>" name="removeFile<?php echo $i; ?>"></p>
<input type="file" id="file<?php echo $i; ?>" name="file<?php echo $i; ?>" onchange="myForm.elements['filename<?php echo $i; ?>'].value=myForm.elements['file<?php echo $i; ?>'].value;">
</div>
</div>
These rows get repeated using a javascript function which increments the name:
function addTableRow(table){
for(i=0; i<myForm.elements['entriesNum'].value; i++){
var $tr = $(table).find(".row:last").clone();
$tr.find("input,select,textarea").attr("name", function(){
var parts = this.id.match(/(\D+)(\d+)$/);
return parts[1] + ++parts[2];
}).attr("id", function(){
var parts = this.id.match(/(\D+)(\d+)$/);
return parts[1] + ++parts[2];
});
$(table).find(".row:last").after($tr);
$tr.find('.fileElements').remove();
$tr.find('input[type!="radio"], textarea').removeAttr("value");
$tr.find('input').removeAttr("checked");
$tr.find('select option:first-child').attr("selected", true);
$tr.find('input[type!="radio"]').removeAttr("disabled");
$tr.find('input[type="radio"]').attr("disabled", "disabled");
$tr.find('.error').hide();
}
}
This works perfectly until the number of rows gets higher than 111. At this point when I submit no more data gets included in the array no matter how many rows I add. I was able to deduce this by using print_r($_REQUEST);. I have edited my php.ini and set all the maxes to be absurdly high with still no change.
Whenever I've had issues with javascript created form elements not showing up in the POST, it's always had to do with the way I've structured the FORM tags.
Things to look for...
Is your FORM tag properly closed off?
Is your FORM start and close at the same level in the DOM? For example, you wouldn't want to do this...
<form method='post'>
<div>
<!--lots of stuff-->
</form>
</div>
You would instead want this...
<form method='post'>
<div>
<!--lots of stuff-->
</div>
</form>
Do you have another FORM tag that's opened and not closed before the one you're actually working with? Like this...
<form method='get'>
<!--some kind of form about something else-->
<form method='post'>
<!--the data you're currently focused on-->
</form>

Categories