How to make all PHP echo values go into a HTML Table - php

How do I fix this issue, you see in the picture below, my code creates a table successfully and echos the first item correctly, but if any other item is added to the cart, it gets displayed below the table, outside the border, not in a row.
Desired Result: For 'Sara Lee Muffin' and the quantity, and price (example) to be inside the table in it's own row and remove button. (Basically set out the exact same way a the Toasted Sandwhich, but below it)
My code is below the image.
<body>
<br>
<h3>Current shopping cart:</h3>
<table style="border: 1px solid black;">
<?php
//If the cart is not empty, then create a table for the Items
if(!empty($_SESSION['shopping_cart'])) {
?>
<tr><th>Item name</th><th>Quantity</th><th>Price</th><th></th></tr>
<?php
if(!empty($_SESSION['shopping_cart'])) {
foreach($_SESSION['shopping_cart'] as $keys => $values) {
?>
<tr>
<td style="text-align:center"><?php echo $values['item_name'];?></td>
<td style="text-align:center"><?php echo $values['item_quantity'];?></td>
<td style="text-align:center">$<?php echo $values['item_price']*$values['item_quantity'];?>
</td>
<td><form action="" method="get">
<input type="submit" class="btn btn-danger" name="remove" value="Remove">
<input type="hidden" name="hidden_id" value="<?php echo $values['item_id']; ?>">
</form></td>
</tr>
</table>
<br>
<?php
}
}
?>
<form action="" method="get">
<input type="submit" class="btn btn-primary" name="payment" value="Confirm">
</form>
<?php
}
else {
echo "No Items added yet.";
?><br>
<?php
}
?>
</body>

You have a closing </table> inside your foreach loop, so every item in the array prints out a </table> tag.
You should add another check for the shopping cart after the loop, and if it exists, print the closing </table> tag.

Related

Automatically click the button for you if there is a date

I have a file called file.php which has:
$row2= pg_fetch_array($result2);
$website = 'http://validate/?id='.$newDateString;
<tbody>
<tr>
<td><?php echo'<a href="'.$website.'">'. $row['name']; ?></td>
The code sends to a file named jasmine.php the url with the date since jasmine contains a calendar. It sends it when you click on the date which contains a link to jasmine.
Jasmine.php:
<div id="content">
<h2>Hello, Please let me know which date you are looking for </h2>
<div><label style="font-size: 14pt; font-family: Verdana;">Posting Date: </label></div><br />
<input type="text" id="date" value= "<?php echo $_GET['id']?>" class="css-input" placeholder="Posting Date..." readonly="readonly" /><br /><br />
<input type="button" id="validate" value="Let's work!" class="btn" />
<input type="button" id="clear" value="Wanna go Again?" class="btn" />
</div>
<div id="contentarea2" style="display: none;">
<h2>Results:</h2>
<div class="view_alerts">
<table>
<tr>
<td>
Code
</td>
<td>
# Posted
</td>
</tr>
<tbody id="view_alerts_tbody">
</tbody>
</table>
</div>
</div>
In Jasmine, I get the date already written for me from file.php, all I need to do is click the button so it could show me a table, is there a way, that it clicks the button on its own if theres a date written, so when I go on that link, it already puts the date and takes me to the table in jasmine.php
Taking the code from the answer above the if statement here should evaluate the input fields value (id is '#date'). An empty value will default to false hence the ! before the selector.
jQuery(document).ready(function()
{
if(!$('#date').val()) {
jQuery("#Your-Button-ID").click();
}
});
You can use below JQuery code for your purpose. When page load is completed (i.e. Page is ready)
jQuery(document).ready(function()
{
jQuery("#Your-Button-ID").click();
});

Form action not getting the data

I can't get the value of the check box field. This is the structure of my code
<form action="<?php echo base_url()?>Home/dashboard" method="post">
<button class="btn btn-default" type="submit" name="delete" onclick="cliked1(event)"
style="margin-left:2px;background-color:blue;color:white;">Delete Item</button>
<td><input class="checkbox" type="checkbox"name="cb" value="1"> 1 </td>
</form>
In the controller
else if(isset($_POST['delete'])) {
$cb = $this->input->post('cb');
echo $cb;
}
I recreated the process it in other file and it works
<form action ="<?php echo base_url()?>Shop/test" method="POST">
<button class='btn btn-danger btn-xs' type="submit" name="delete" value="delete"><span class="fa fa-times"></span> delete</button>
<input type="checkbox" name="cb[]" value="lol">
<br>
<input type="checkbox" name="cb[]" value="yeaa">
</form>
In the Controller
function test()
{
$this->load->view('test');
if(isset($_POST['delete']))
{
$id = implode('^',$this->input->post('cb'));
print_r($id);
}
}
And I apply this process and it turns like this
<table id="example" class="display" cellspacing="0" width="100%">
<thead>
<th>ID</th>
<th>Item Photo</th>
<th>Item Name</th>
<th>Stocks</th>
<th>Date Posted</th>
<th>Price</th>
<th>Actions</th>
</thead>
<tbody>
<form action ="<?php echo base_url()?>Shop/test" method="POST">
<?php
$x=1;
foreach($shop_items as $key)
{
?>
<tr>
<td><input class="checkbox" type="checkbox" name="cb[]" value="<?php $key->shop_item_id?>"> <?php echo $x;?> </td>
<td><center><img src="<?php echo base_url()?>uploads/items_main/<?php echo $key->shop_item_main_pic;?>" style = "width:60px;height:50px;"alt=""></center></td>
<td><?php echo mb_strimwidth($key->shop_item_name, 0, 18,"...");?></td>
<td style="width:10px;"><center><button><span class="fa fa-eye"></span></button></center></td>
<td><?php echo date('F,d,Y',strtotime($key->shop_item_date_posted))?></td>
<td><?php if(!$key->shop_item_sale){ echo number_format($key->shop_item_orig_price);}
else{ echo "<font color='red'>".number_format(intval($key->shop_item_orig_price-($key->shop_item_orig_price*($key->shop_item_sale/100))))."</font> ";}?></td>
<td>
Bid | View
</td>
</tr>
<?php
$x+=1;
}
?>
<button class='btn btn-danger btn-xs' type="submit" name="delete" value="delete"><span class="fa fa-times"></span> delete</button>
</form>
</tbody>
</table>
The problem is that the submit button does not work.
In case of a checkbox, when the checkbox is not checked, you will not get the checkbox set in the $_POST. So to get the value of checkbox, you need to do this:
if (isset($_POST["cb"])) {
// Checkbox is checked.
$cb = $_POST["cb"];
} else {
// Checkbox is not checked.
}
And please do not check on type="submit".
From one of the resources:
In most cases this will work, but there are a few occasions when it won't. To identify when this condition will fail we have to look at the different ways of submitting a form.
There are two ways of submitting a form, one is to click the submit button, the other is to hit return, of which will invoke the submit button. The problem occurs when you hit the return button (as most people do), when using Internet Explorer (the most commonly used browser) and when the submit button does not have focus. This will submit the form, but it will not send the submit variable, which would mean the script we are using above will fail.

Form in Form is not allowed, but what else

I fetch SQL data with a while loop and insert the data into a table. With a form and a submit button i can save modified values.
My simplified code:
<table>
<tr>
<?php
//SQL QUERY
//...
while ($row = mysql_fetch_array($sql_header)) {
$var_ab = $row['ab'];
$var_id = $row['id'];
?>
<form id="form1" action="" method="POST">
<td>
<input type="text" value="<?php echo $var_ab; ?>"
</td>
<td>
<input type="text" value="<?php echo $var_id; ?>"
</td>
//PLACEHOLDER FOR SECOND FORM
<?php
}
?>
</tr>
<td>
<input type="submit" name="save" value="SAVE" class="classsubmit" />
</td>
</form>
</tr>
</table>
So far, so good. So, how can I insert a second form to delete an entry? I've tried to place this code (PLACEHOLDER FOR SECOND FORM - see above)
<td>
<form id="form2" action="" method="POST">
<input type="text" value="<?php echo $var_id;?>"
</form>
</td>
but it's not working and it's not allowed to nest forms.
Any suggestions?
If you only want to delete an entry on the page or in the database you could try a button or a span with an onclick function.
for example:
<span onclick="window.location='?delete=<?php echo $row[(unique-value-in-database-from-this-row)]; ?>'; return false">Delete entry</span>
Make sure you add return false or the first form will be submitted. If you use a button make sure it has type="button"
On this page could be a PHP code like this:
if(isset($_GET['delete']))
{
$item = $_GET['delete'];
//SQL connect
$result = mysql_query($conection ,"DELETE FROM table WHERE uniquevalue='$item'");
}
I hope gives an idea for a solution.

Check if the radio button is pushed

I have a form that has a foreach loop and inside the loop I have an input type radio, so every time it has a new value like the codes below:
<?php
$aircrafts = AircraftPermit::getaircraft();
foreach($aircrafts as $aircraft)
{
$pacas = AircraftPermit::getrouteaircraft($aircraft->name ,$pilotid);
if($pacas)
{
?>
<tr>
<td>
<input type="radio" id="ac" value="" disabled="disabled"><?php echo $aircraft->name ;?><br />
</td>
<td align="center">
<input name="submit" title="Give permission to selected aircraft" type="submit" value="Give Permit" disabled="disabled">
</td>
<td align="center">
<font color="green">Granted</font>
</td>
</tr>
<?php
}
else
{
?>
<tr>
<td>
<input type="radio" id="ac" value="<?php echo $aircraft->name ;?>"><?php echo $aircraft->name ;?><br />
</td>
<td align="center">
<input name="id" type="hidden" value="<?php echo $pilotid ;?>">
<input name="submit" title="Give permission to selected aircraft" type="submit" value="Give Permit">
</td>
<td align="center">
<font color="red">Restricted</font>
</td>
</tr>
<?php
}
}
?>
Now at the end I have a script to check if the radio button is selected like below:
<script type="text/javascript">
function radio_is_on() {
var elementId = document.getElementById('ac');
if (elementId.checked==false)
{
alert('Please select the aircraft first!');
return false;
}
if (elementId.value)
{
return true;
}
}
</script>
When the radio button is not pushed the message pops up okay but when it's pushed it returns no value and the form send null value. Please tell me where I'm wrong.
Thanks
You cannot have multiple id tags, which is probably why it's not working. Try changing it to a class instead. See below for example in jquery.
HTML:
<input type="radio" class="ac" value="<?php echo $aircraft->name ;?>"><?php echo $aircraft->name ;?><br />
JAVASCRIPT:
function radio_is_on() {
if ($('.ac:checked').val()) {
return true;
}
else {
alert('Please select the aircraft first!');
return false;
}
}
You have same id for all radio buttons - that is not correct.
And only one button is considered all the time.
You can number them, eg. ac1, ac2 and so on.
as far as I can remember a radio button is "pushed" when it has the checked attribute, and it is not pushed when it doesn't have it.
...
if (elementId.hasOwnProperty('checked'))
...
instead of
...
if (elementId.checked==false)
...

Trouble with dynamic form values with PHP

I have a loop in a form for dynamically adding/removing items, however, only the last item is removed every time. I can add items just fine though.
I can't figure out why no matter which items I try to remove from both add_friends and register_tasks, ONLY the last item is removed. When I echo $_POST['task_name'] it's always the last item on the list, never the one I selected(unless of course I select the last item).
Here is the form
<center><h2>Create Event</h2></center>
<form method="post">
<?
$form = new common_functions();
if($form->check_saved_forms($my_username, "event") == TRUE){
$this->existing_forms(); //allow the user to select existing forms that they have created
}
?>
<tr><td>Title:</td><td><input type="text" width="20%" name="event_title" value="<? echo $form_data[0]; ?>" /></td></tr>
<tr><td>Details:</td><td><input type="text" width="20%" name="event_details" value="<?echo $form_data[1]; ?>" /></td></tr>
<tr><td>Date:</td><td><input type="text" width="20%" name="event_date" id="event_date" value="<? echo $form_data[2]; ?>" /></td></tr>
<tr><td> <input type="submit" name = "submit_event" id = "lOBut" value="Create Event" /></td></tr>
</table>
<?
//this is the section giving me trouble
$form->add_friends($added_friends);
$form->register_tasks($tasks,$nums);
?>
</form>
Both add_friends and register_tasks dynamically add and remove friends/tasks by a textbox, and a list of the added items are shown. users can remove items, however, right now only the last item is removable and I can't figure out why.
Here is register (add_friend is the same, but labeled differently)
function register_tasks($tasks,$nums){
<table>
<td><input type="textbox" size="50" name="register_description"/> </td></tr>
<td><input type ="textBox" name="register_num" /> </td>
<td><input type="submit" name="add_register_tasks" value="Add"/></td></tr>
</table>
<?
if(count($tasks) > 0){
?>
<table>
<tr><td><b>Registration Item Description</b></td><td><b># Of Available Spots</b></td></tr><br>
<?
foreach (array_combine($tasks, $nums) as $task => $task_num){
?>
<tr><td><? echo $task; ?></td><td><? echo $task_num; ?></td>
<td><input type="submit" name="remove_task" value="Remove"/></td>
<td><input type="hidden" name="task_name" value="<? echo $task; ?>"/>
<td><input type="hidden" name="task_num" value="<? echo $task_num; ?>"/></td></tr>
<?
}
?></table><?
}
You need to place the form tag inside the loop. Right now what happens is all the records are displayed in a single form and no matter what item you choose, it takes only the last value. Another alternate solution would be to use GET method and pass the number/name as query string.

Categories