Welcome Forum residents,
My goal is to display MySQL data in input cards and the cards aligned side by side in bootstrap 4 rows.
Reading MySQL data works fine, but the input cards are not juxtaposed by the Bootstrap 4 grid system. (If I do not read data from MySQL, I only use plain HTML, the Grid system works.)
The code:
...
<?php include_once('includes/head.tpl'); ?>
</head>
<body>
<?php include_once('includes/php/navbar.php'); ?>
<form method="GET" action="action2.php">
<?php
$result = mysqli_query($conn, "SELECT * FROM plans");
while($row = mysqli_fetch_array($result)) {
?>
<div class="container">
<div class="row">
<div class="col-sm-4">
<label>
<input type="radio" name="plan_id" id="plan_id" value="<?php echo $row['id']; ?>" class="card-input-element" />
<div class="card-input">
<?php echo $row['plan_name']; ?><br>
<?php echo $row['plan_cpu']; ?><br>
<?php echo $row['plan_memory']; ?><br>
<?php echo $row['plan_disk']; ?><br>
</div>
</label>
</div>
</div>
<?php
}
?>
<input type="submit" name="btn-submit">
</form>
</div>
...
The end result:
What could be the problem?
Why is Grid system not working when reading MySQL?
Thanks in advance for the answers!
The problem looks like you create the bootstrap container div and row element for each row of the data, this should be outside of the loop where you retrieve the rows...
<div class="container">
<div class="row">
<?php
$result = mysqli_query($conn, "SELECT * FROM plans");
while($row = mysqli_fetch_array($result)) {
?>
<div class="col-sm-4">
<label>
<input type="radio" name="plan_id" id="plan_id" value="<?php echo $row['id']; ?>" class="card-input-element" />
<div class="card-input">
<?php echo $row['plan_name']; ?><br>
<?php echo $row['plan_cpu']; ?><br>
<?php echo $row['plan_memory']; ?><br>
<?php echo $row['plan_disk']; ?><br>
</div>
</label>
</div>
<?php
}
?>
</div>
</div>
If you have more plans that fit on a row, you would need to create the div's for the row each time you get to the split point.
Related
Why I can't insert into 1 field to my database table?
I loop the data of my other table to display each data and put the result to href tag so it clickable and concatenate with it's id so it will put the id result after the link like this:
<a href="scheduling.php?CID=<?php echo $rows['docID']; ?>">
Here is how I loop my data using php on page1:
<?php
$sel = "SELECT * FROM doctors ORDER BY docID";
$result = $conn->query($sel);
if($result->num_rows>0)
{
while($rows = $result->fetch_array())
{
?>
<a href="scheduling.php?CID=<?php echo $rows['docID']; ?>">
<div class="doc_item" style="width:310px; border:4px solid #009973;display:inline-block; margin-top:40px; margin-right:20px;">
<img src="images/ft-img.png" style="width: 300px;">
<div class="item-lvl" style="width: 100%; background:#009973; color:#fff; height:70px; padding-top:10px;">
<h4 style="font-size:20px; font-weight:bold;">Dr. <?php echo $rows['docFname']; ?></h4>
<span><?php echo $rows['docType']; ?></span>
</div>
<div style="width: 100%; background:#00664d;padding-top:15px; padding-bottom:20px;">
<h3 style="color:#fff;">Make a Schedule!</h3>
</div>
</div>
</a>
<?php
}
}
?>
Then I want to add the value of CID to my database table using this code on page 2:
<?php
if (isset($_POST['btn-submit'])) {
$cusID = $customerID;
$docID = $_GET['CID'];
$checkupType = $_POST['checkupType'];
$schedTime = $_POST['schedTime'];
$contact = $_POST['contact'];
$ins = "INSERT INTO schedule
(customerID, docID, checkupType, schedTime, contact)
VALUE ('$cusID','$docID','$checkupType','$schedTime','$contact')";
if($conn->query($ins)===TRUE) {
header('location:success_sched.php');
}else{
echo "SQL Query: " . $ins . "->Error: " . $conn->error;
}
}
Now, this code insert data to my database table except $docID which is the value is $_GET['CID'];
docID column is intiger.
please correct my code, this is my school project.
Form code on page 2:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<div class="sign-u">
<div class="sign-up1">
<h4>Form of Checkup* :</h4>
</div>
<div class="sign-up2">
<label>
<input type="radio" value="Virtual" name="checkupType" required>
Virtual Checkup
</label>
<label>
<input type="radio" value="Facetoface" name="checkupType" required>
Face to Face Checkup
</label>
</div>
<div class="clearfix"> </div>
</div>
<p style="color:gray;">*Take note that virtual checkup will require a you stable internet for better communication.</p>
<div class="sign-u">
<div class="sign-up1">
<h4>Date and Time* :</h4>
</div>
<div class="sign-up2">
<input type="date" name="schedTime" id="date" required>
</div>
<div class="clearfix"> </div>
</div>
<p style="color:gray;">*Please provide Skype id as a contact information if <span style="color:#ff5c33">Virtual Checkup</span> is your choice.</p>
<div class="sign-u">
<div class="sign-up1">
<h4>Contact Information :</h4>
</div>
<div class="sign-up2">
<input type="text" name="contact">
</div>
<div class="clearfix"> </div>
</div>
<div class="sub_home" style="text-align: right;">
<input style="background: #339966;" type="submit" name="btn-submit" value="Submit">
<div class="clearfix"> </div>
</div>
</form>
Please understand that when you submit a form, that is a new request to the server. It has a new URL. Your page 2 form submits to $_SERVER['PHP_SELF']; - which is the current URL but without the querystring variables. Therefore your GET parameter is not passed in the form submit. (If you use the "View Source" feature of your browser to examine the "action" attribute of the form after it's rendered, you'll see exactly what URL it makes the request to when submitting. You can also see the same thing by looking at your browser's Network tool, or your webserver's access logs.)
There are a couple of ways around that, but I'd suggest adding the value as a hidden field in the form, and populating it with the value of the GET parameter during the previous request.
e.g.
<input type="hidden" value="<?php echo $_GET["CID]; ?>" name="CID">
and then in the PHP, retrieve it via $_POST like all the other variables:
$docID = $_POST['CID'];
Finally, please ensure you update your code to use prepared statements and parameters, as recommended in the comments, so that you are protected against both SQL injection attacks and unexpected syntax errors. You can get more info and examples here
I got a website in testing, how it works is
Step 1) Form to key in Details in page1.php
Step 2) with the POST function, the keyed in form values appears in next page2.php in read only form format. Sort of like a summary of the information they keyed in. at the bottom they are to click "confirm" and an email is supposed to be sent with the full details of that page2.php.
My problem:
I can get it to sent email from page1.php, in the form page and send email upon clicking submit. but that is not what i want, i want it to send email after they confirm their details from page2.php. there is where i get the error.
my page2.php file has this as the code below. the form-to-email.php is the action to send email which works when its sent directly from a form but not from this page2.php read only page to confirm the details.
What am i doing wrong?
<form action="form-to-email.php" method="post" class="box readonly">
<h3>Vehicle Details</h3>
<div class="f-row">
<div class="one-fourth">Type</div>
<div class="three-fourth">Transport Service</div>
</div>
<div class="f-row">
<div class="one-fourth">Vehicle</div>
<div class="three-fourth">14 Foot Lorry</div>
</div>
<h3>Customer Details</h3>
<div class="f-row">
<div class="one-fourth">Name</div>
<div class="three-fourth"><?php $name = $_POST["name"]; echo $name; ?></div>
</div>
<div class="f-row">
<div class="one-fourth">Phone</div>
<div class="three-fourth"><?php $name = $_POST["number"]; echo $name; ?></div>
</div>
<div class="f-row">
<div class="one-fourth" label for='email'>Email</div>
<div class="three-fourth"><?php $name = $_POST["email"]; echo $name; ?></div>
</div>
<h3>Job Details</h3>
<div class="f-row">
<div class="one-fourth">Date</div>
<div class="three-fourth"><?php $name = $_POST["date"]; echo $name; ?></div>
</div>
<div class="f-row">
<div class="one-fourth">Time</div>
<div class="three-fourth"><?php $name = $_POST["time"]; echo $name; ?></div>
</div>
<div class="f-row">
<div class="one-fourth">Pick Up</div>
<div class="three-fourth"><?php $name = $_POST["pick"]; echo $name; ?></div>
</div>
<div class="f-row">
<div class="one-fourth">Drop Off</div>
<div class="three-fourth"><?php $name = $_POST["drop"]; echo $name; ?></div>
</div>
<div class="f-row">
<div class="one-fourth">Items</div>
<div class="three-fourth"><?php $name = $_POST["items"]; echo $name; ?></div>
</div>
<div class="f-row">
<div class="one-fourth">Remarks</div>
<div class="three-fourth"><?php $name = $_POST["remarks"]; echo $name; ?></div>
</div>
<h3 align="center">TOTAL: $80 SGD</h3><input type="submit" class="btn medium color" value="Confirm (Cash Payment)"></form>
help.
The main problem is that you don't have any inputs in <form> on page2.php. So what you submit is empty.
There are at least 2 ways to solve it:
1 - Store your values in php session:
Add the following lines at top of your page2.php script:
<?php
session_start();
$_SESSION['post_values'] = $_POST;
...
?>
And the following lines at top of your form-to-email.php:
<?php
session_start();
$_POST = $_SESSION['post_values'];
$_SESSION['post_values'] = array(); // this will clear the values, so they wouldn't get resubmitted on page refresh. You could probably use `unset($_SESSION['post_values'])` but that sometimes doesn't work, so it's safer to redefine it as empty array.
...
?>
Storing values in php session will allow you to pass those values to other scripts in same domain. Please note that every time you want to use $_SESSION object, you got to start session first with session_start(). This has to be done before outputting anything.
2 - Add hidden inputs to your code like this (for every field):
<div class="f-row">
<div class="one-fourth">Name</div>
<div class="three-fourth"><input type="hidden" name="name" value="<?php json_encode($_POST["name"]);?>/><?php echo $_POST["name"];?></div>
</div>
<div class="f-row">
<div class="one-fourth">Phone</div>
<div class="three-fourth"><input type="hidden" name="number" value="<?php json_encode($_POST["number"]);?>/><?php echo $_POST["number"];?></div>
</div>
... etc.
That will create invisible inputs so the values could be send again. Please note that I am using json_encode() function so it wouldn't break the HTML syntax if value would contain for example ".
I am trying to update records in 'pantry-info' table. Code goes to the if loop instead of else
The code is mentioned below, what am I doing wrong?
DB connection is:
require 'config/connectDB.php';
session_start();
$id = $_GET['id'];
$sql = 'SELECT * FROM temp WHERE'. " pan_id = '$id'";
$result = mysqli_query($conn,$sql);
Trying to read DB table values in form as follows:
<?php
if(mysqli_num_rows($result1) == 0)
{
echo '<h2>This record already exists. Do you want to delete it?</h2>';
} else { ?>
<h4>Edit the record here:</h4>
<br>
<form name="pantryinfo" id="pantryForm" method = "post" action="update.php" data-toggle="validator" role="form">
<?php while ($row = mysqli_fetch_array($result1,MYSQLI_ASSOC)) { ?>
<div class="control-group form-group">
<div class="controls">
<input type="hidden" class="form-control" id="panid" name="panid" value="<?php echo $row['pan_id'];?>">
<p class="help-block"></p>
</div>
</div>
<div class="control-group form-group">
<div class="controls">
<label>Name</label>
<input type="text" class="form-control" id="name" name="name" value="<?php echo $row['pname'];?>" required>
<p class="help-block"></p>
</div>
</div>
</form>
<?php
}
}
?>
Similar code works fine on a different page retrieving values from another DB table. Please help. Thanks in advance.
I resolved the issue. In the SQL query I used $result as a variable and in the if-else code I was using result1.
Thank you Fred for pointing it out.
I have been trying to execute the code for long time but rest then my image all other variables are working. My images are stored in server folder called "uploaded".
I have two pages called "show.php" and "readmore.php", and I want to pass the image from show.php to readmore.php but other then image all the variables are displaying in readmore.php.
My code for show.php
<a class="btn" href="readmore.php?ad_title=<?php echo $rec['ad_title']; ?> & price= <?php echo $rec['price']; ?> & address= <?php echo $rec['address']; ?> & phone= <?php echo $rec['m_number']; ?> & description= <?php echo $rec['description']; ?> & contact_name= <?php echo $rec['contact_name'] ; ?> & photo_name= <?php echo $rec['photo_name']?>;">
My code for readmore.php (I am displaying here two of the variables along with the image)
<div class="control-group">
<label class="control-label">About the book</label>
<div class="controls">
<label class="checkbox">
<?php echo $_GET['description'];?>
</label>
</div>
</div>
<div class="control-group">
<label class="control-label">About the book</label>
<div class="controls">
<label class="checkbox">
<td id="pic" width="300px"><img src= "../uploaded/<?php echo $_GET['photo_name'];?>" style="border-radius:5px; padding:3px; "/></td>
</label>
</div>
</div>
Learn to debug:
On first lines of readmore.php dump the superglobal variable $_GET and see whether value of $_GET['photo_name'] is coming or not.
Eg.
echo "<pre>";
print_r($_GET);
echo "</pre>";
P.S. <pre> tag is just for formatting the output. print_r dumps the values of $_GET.
If you see value of 'photo_name' index in the GET array,
there should be some errors in your image path.
I have read many post like this but have failed to find my particular situation.Trying to delete the selected checkbox. right now you can submit the form and it takes you to all the right pages except it doesn't actually delete anything.
Here is my controller info
function deleteFolder() {
if(array_key_exists('deleteMe',$_POST)) {
$checkbox = $this->input->post['checkbox'];
$this->index_model->deleteFolder($checkbox);
}
$this->folderdeleted();
}
Here is my Model
function deleteFolder($checkbox) {
$this->db->where('folderName', 'folderName');
$this->db->delete('senior', $checkbox);
return;
}
Here is my View
<!DOCTYPE html>
<?php $this->load->view('partials/page_head'); ?>
<body>
<div id="container">
<div id="top">
<div class="topcenter">
<h2><a class="homebtn" href="<?php echo base_url();?>">Home</a></h2>
</div>
<div class="navdescription"><span>Delete Page</span></div>
</div>
<div class="projectFolders">
<?php foreach($foldername as $row) { ?>
<div class="folder">
<button><?php echo $row->folderName; ?></button>
<div class="delete">
<form name="delete" method="post" action="<?php echo base_url(); ?>index.php/home/folderdeleted">
<p>
<input type = "checkbox" id = "check_<?php echo $row->folderName; ?>"/>
<?php echo form_submit('deleteFolder', 'Delete'); ?>
</p>
</form>
</div>
</div>
<?php } ?>
</div>
</div><!-- End of container div -->
</body>
</html>
There are several errors in your code. I'm not sure whether I've found all of them and whether the code will work.
The controller should be:
function deleteFolder() {
if($this->input->post('checkbox') !== false) {
$checkbox = $this->input->post('checkbox');
$this->index_model->deleteFolder($checkbox);
}
$this->folderdeleted();
}
The model should be:
function deleteFolder($checkbox) {
$this->db->where('folderName', $checkbox);
$this->db->delete('senior');
return;
}
The input tag in the view should be:
<input name="checkbox" value="<?php echo $row->folderName; ?>" type = "checkbox" id = "check_<?php echo $row->folderName; ?>"/>
A little warning for checkboxes: if they aren't checked, you won't find anything in the $_POST variable.