Validate radio button selected in each group - php

Here is the code. The "surveys" are generated elsewhere. I need validation that a radio button is clicked in each group of answers. The number of answers and answer groups will be variable.
<div id="main">
<div class="container">
<!-- display a table of surveys -->
<h2><?php echo $survey['title']; ?></h2>
<form class="survey" name="take_survey" action="./take_survey" method="post">
<table class="table table-hover">
<input type="hidden" name="survey" value="<?php echo $survey['id'] ?>" />
<tr>
<th>Question</th>
<th>Response</th>
</tr>
<?php foreach ($questions as $question) : ?>
<tr>
<td><?php echo $question['question']; ?></td>
<td></td>
</tr>
<?php foreach ($answers as $answer) : ?>
<?php if ($answer['question_id'] == $question['id']) : ?>
<tr>
<td> <?php echo $answer['answer']; ?></td>
<td><input type="radio" name="question_<?php echo $question['id']?>" value="<?php echo $answer['id'] ?>"></td>
</tr>
<?php endif; ?>
<?php endforeach; ?>
<?php endforeach; ?>
</tr>
</table>
<div class="eventButtons">
<input class="btn btn-default" type="submit" name="submit" id="submit" value="Save">
<input class="btn btn-default" type="reset" name="reset" id="reset" value="Clear" class="btn">
</div>
</form>
</div>
</div>

Related

How to send a form in a foreach loop using PHP

I want to send an Id by the form in foreach loop but when I click on each button it sends me the first form.
It means when I click on the second button it sends me the value of the first form the value of input hidden is right but the first value every time sent.
<?PHP
if ($transactions) {
foreach ($transactions as $transaction) {
?>
<td class="text-left">
<?php echo $transaction['invoice_id']; ?>
</td>
<td class="text-left">
<?php echo $transaction['invoice_type']; ?>
</td>
<td class="text-left">
<?php echo $transaction['ref_id']; ?>
</td>
<td class="text-left">
<?php echo $transaction['gateway']; ?>
</td>
<td class="text-left">
<?php echo $transaction['payment_date']; ?>
</td>
<td class="text-right">
<?php
if($transaction['payment_status'] == 'not paid') {
?>
<form
method="POST"
action="<?php echo $gate_url; ?>"
>
<input type="hidden" name="invoiceId" value="<?php echo $transaction['invoice_id']; ?>">
<button class="btn btn-md btn-primary" type="submit" >
<?php echo $pay; ?>
</button>
</form>
<?php
}
?>
</td>
<?php
}
You have an array of the values, so this:
<input type="hidden" name="invoiceId" value="<?php echo $transaction['invoice_id']; ?>">
replace with:
<input type="hidden" name="invoiceId[]" value="<?php echo $transaction['invoice_id']; ?>">

Paypal payment with sandbox accounts, TESTING

I implemented a shopping cart to checkout with PayPal. For a single item, it works, but for multiple items, it does not. Pressing the paypal Pay Now! button I got an error like this => "https://www.sandbox.paypal.com/webapps/shoppingcart/error?flowlogging_id=3451c32fea2df&code=LACK_OF_BASIC_PARAMS"
"Things don't appear to be working at the moment. Please try again later."
wow, nice! missing something? showing a little code here, 'checkout.php':
<?php require_once('../resources/includes/initialize.php'); ?>
<?php
$products = Product::find_product_items();
$count = count($products);
$items = 0;
$total = 0;
?>
<?php include(TEMPLATE_FRONT.DS."header.php"); ?>
<!-- Page Content -->
<div class="container">
<!-- /.row -->
<div class="row">
<h1>Checkout</h1>
<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
<table id="myTable" class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>Product</th>
<th>Price</th>
<th>Quantity</th>
<th>Sub-total</th>
</tr>
</thead>
<?php
$i = 0;
foreach($products as $product){
$i = ++$i;
?>
<tbody id="<?php echo "t".$i ?>">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="upload" value="1">
<input type="hidden" name="business" value="agardo#business.example.com">
<input type="hidden" name="item_name_<?php echo $i; ?>" value="<?php echo $product->title; ?>">
<input type="hidden" name="item_number_<?php echo $i; ?>" value="<?php echo $product->id; ?>">
<input type="hidden" name="quantity_<?php echo $i; ?>" value="<?php echo $product->quantity; ?>">
<input type="hidden" name="amount_<?php echo $i; ?>" value="<?php echo $product->price; ?>">
<input type="hidden" name="currency_code_<?php echo $i; ?>" value="USD">
<!--<input type="hidden" name="return" value="https://localhost/public/thank_you.php">-->
<tr>
<td><?php echo $product->id; ?></td>
<td><?php echo $product->title; ?></td>
<td id="<?php echo "p".$i ?>"><?php echo "$".format_number($product->price); ?></td>
<td id="<?php echo "q".$i ?>"><?php echo $product->quantity; ?></td>
<td id="<?php echo "s_t".$i ?>"><?php echo "$".format_number($product->price*$product->quantity); ?></td>
<td>
<span class="btn btn-warning glyphicon glyphicon-minus" data-id="<?php echo $product->id; ?>"></span>
<span class="btn btn-success glyphicon glyphicon-plus" data-id="<?php echo $product->id; ?>"></span>
<span class="btn btn-danger glyphicon glyphicon-remove" data-id="<?php echo $product->id; ?>"></span>
</td>
</tr>
</tbody>
<?php $items+=$product->quantity;
$total+=$product->price*$product->quantity;
} ?>
</table>
<input type="image" src="http://www.paypal.com/en_US/i/btn/x-click-but01.gif" name="submit" alt="Make payments with PayPal - it's fast, free and secure!">
</form>
<!-- ***********CART TOTALS*************-->
<div class="col-xs-4 pull-right ">
<h2>Cart Totals</h2>
<table class="table table-bordered" cellspacing="0">
<tr class="cart-subtotal">
<th>Items:</th>
<td><span id="count" class="amount"><?php echo $items; ?></span></td>
</tr>
<tr class="shipping">
<th>Shipping and Handling</th>
<td>Free Shipping</td>
</tr>
<tr class="order-total">
<th>Order Total</th>
<td><strong><span id="total" class="amount"><?php echo "$".format_number($total); ?></span></strong> </td>
</tr>
</tbody>
</table>
</div><!-- CART TOTALS-->
</div><!--Main Content-->
<?php include(TEMPLATE_FRONT.DS."footer.php"); ?>
Any particular reason you are trying to integrate a form post to /cgi-bin/webscr in 2020? Please use https://developer.paypal.com/demo/checkout/#/pattern/client , or the server version there with two server side routes if you need to record the result in a database.
( If you do need to record the result in a database, you'll need a route for 'Set Up Transaction' and one for 'Capture Transaction', documented here: https://developer.paypal.com/docs/checkout/reference/server-integration/ -- and pair these routes with the above JavaScript approval flow )
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="upload" value="1">
<input type="hidden" name="business" value="agardo#business.example.com">
must be outside of the loop! END of it! :)

how to display contents from another table (codeigniter)?

I want to do crud on my page and I have to get content from another table and display it in a select tag. my plan was to make an update page wherein it will get the data from another table to display (in this case the category_name, cat_id was the foreign key at the menu table).
I already tried to join because that's what other forums have said but I think I was wrong.
help would really be appreciated.
this is my model:
function inputMenuToUpdate($menu_id){
$this->db->select('cat_id, category.category_id, category.category_name AS category_name');
$this->db->from('menu');
$this->db->join('category', 'menu.cat_id = category.category_id');
$this->db->where('menu_id',$menu_id);
return $query->row();
}
this is my controller:
public function inputToUpdate($menu_id){
$data['row'] = $this->adminMenuModel->inputMenuToUpdate($menu_id);
$this->load->view('adminEdit', $data);
}
this is my view:
<tbody>
<!-- $result is from the $data in CrudController-->
<?php foreach($result as $row){?>
<tr>
<td><?php echo $row->menu_name; ?></td>
<td><?php echo $row->category_name; ?></td>
<td><?php echo $row->price; ?></td>
<td><?php echo $row->description; ?> </td>
<td>
<i class="fa fa-pencil"></i>
</td>
</tr>
<?php } ?>
</tbody>
and this is my update page:
<div class="container" style="width: 40rem;">
<form method="POST" action="<?php echo site_url('adminMenuController/update'); ?> / <?php echo $row->category_id; ?>">
<button type="button" class="btn btn-link"> Go Back </button>
<h1> UPDATE MENU </h1>
<div class="border-bottom"></div>
<br>
<br>
<label for="upMName">New Drink Name</label>
<input type="text" name="mn" class="form-control" id="upMName" value="<?php echo $row->menu_name ?>"></input>
<!-- <label for="cat"> Category </label>
<select id="cat" class="form-control">
<option> <?php echo $row->category_name; ?> </option>
</select> -->
<label for="upPrice">New Price</label>
<input type="number" name="price" class="form-control" id="upPrice" value="<?php echo $row->price ?>"> </input>
<label for="updesc">New Description </label>
<input type="text" name="desc" class="form-control" id="updesc" value="<?php echo $row->description ?>"> </input>
<br>
<button type="submit" class="btn btn-info"> Update </button>
<button type="reset" class="btn btn-danger"> Cancel </button>
</form>
</div>

My php table changed the false row in my db

I'm a PHP beginner & have a question xd
Whenever I click on the submit button, the data of the last user in the PHP table are changed.
Could somebody check on it?
<?php
foreach ($db->results() as $unpaid){
?>
<form method="POST">
<tr><td>
<?=$unpaid->id?>
<input type="hidden" name="user" value="<?=$unpaid->id?>">
</td>
<td><?=$unpaid->username?>
</td>
<td><?=$unpaid->bitcoinadress?></td>
<td><?=$unpaid->points?></td>
<td><?=$unpaid->requestdate?></td>
<td><?=$unpaid->status?></td>
<td>
<input type="submit" class="btn btn-warning" name="submit" value="Submit" /><br/>
</td>
</tr>
<?php } ?>
<?php
if(isset($_POST['submit']))
$id = $POST_['user'];
$db->update("payment_request", $id, ["status"=>"Paid"]);
?>
</form>
</table>
You can't put a <form> around a <tr>. You need to put the form inside one of the <td> tags.
<table>
<?php
foreach ($db->results() as $unpaid){
?>
<tr>
<td> <?=$unpaid->id?> </td>
<td><?=$unpaid->username?> </td>
<td><?=$unpaid->bitcoinadress?></td>
<td><?=$unpaid->points?></td>
<td><?=$unpaid->requestdate?></td>
<td><?=$unpaid->status?></td>
<td>
<form method="POST">
<input type="hidden" name="user" value="<?=$unpaid->id?>">
<input type="submit" class="btn btn-warning" name="submit" value="Submit" /><br/>
</form>
</td>
</tr>
<?php } ?>
<?php
if(isset($_POST['submit']))
$id = $POST_['user'];
$db->update("payment_request", $id, ["status"=>"Paid"]);
?>
</table>

I can't log in as admin

i have created a login system where i can login as a admin, or as a user (employer). If i'm logged in as a admin, on the next page i can update, delete or create a new employer, and if i'm logged in as a employer a didn't have that options. This works fine, but today when i'm logged in as admin a don't have that permission and i got a message: You can't create a new user (this is the message that i have posted in else statement).
This is my homepage.php script. Any help?
<?php
require_once("connection.php");
$user_id = $_SESSION['userid'];
$role = $_SESSION['role'];
$sql = "SELECT * FROM login";
$stmt = $conn->query($sql);
?>
<html>
<head>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<nav class="navbar navbar-default">
<div class="navbar-header">
<a class="navbar-brand" href="#"><?php echo $role ?></a>
</div>
<div class="container">
<?php if($role == 'admin') { ?>
<button type="button" class="btn btn-default navbar-btn">New Employee</button>
<?php } else { }?>
<button type="button" class="btn btn-default navbar-btn">Employee</button>
<button type="button" class="btn btn-default navbar-btn">Sign Out</button>
</div>
</nav>
<div class="box">
<div class="col-sm-4 col-md-6 col-md-offset-2">
<table class="table table-hover table-striped table bordered" border="1">
<thead>
<tr>
<th style="width:250px">UserName</th>
<th style="width:250px">User Password</th>
<th style="width:250px">User Role</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php foreach($stmt as $row){ ?>
<tr>
<td><?php echo $row['user_name'] ?></td>
<td>*******</td>
<td><?php echo $row['user_role'] ?></td>
<?php if($role == "admin"){ ?>
<td>
<form action="delete.php" name="deleteForm" method="post">
<input type="hidden" name="delete_id" value="<?php echo $row['user_id'] ?>"/>
<button type="submit" name="delete_user" class="btn btn-info">Delete</button>
</form>
</td>
<td>
<form action="update.php" name="updForm" method="post">
<input type="hidden" name="update_id" value="<?php echo $row['user_id'] ?>"/>
<button type="submit" name="update_user" class="btn btn-info">Update</button>
</form>
</td>
<?php } ?>
</tr>
<?php } ?>
<?php if($role == "admin"){ ?>
<form action="add_user.php" method="POST" name="add_user">
<tr>
<td><input type="text" name="username" class="form-control"/></td>
<td><input type="text" name="password" class="form-control"/></td>
<td>
<select name="select_role" class="form-control">
<option value="admin">Admin</option>
<option value="emp">Employee</option>
</selet>
</td>
<td>
<button type="submit" name="sub_user" class="btn btn-danger">Add User</button>
</td>
</tr>
<?php } else{ ?>
<tr>
You can't add a new user!
</tr>
<?php } ?>
</form>
</tbody>
</table>
</div>
</div>
</body>
</html>
where is session_start();
function because without this function how can you get the session variables so please first put the session_start() at top of the file and then check.

Categories