Button inside echo - php

Here is my php codeigniter view to show Time(If it is null,it will shows ERROR).
<input type="text" class="form-control timepicker" id="checkInTime" name="checkInTime" value="<?php if ($result->checkInTime == '0'||'NULL'){ echo 'ERROR';} else{ echo date('H:i:s',strtotime($result->checkInTime));}?>" required parsley-maxlength="6" placeholder="checkInTime" disabled/>
How can i change the ERROR into a button or link(Which is inside the echo).

The simplest way is to use if condition outside the input:
<?php
if ($result->checkInTime == 0 || $result->checkInTime == NULL) {
// here echo your button or link
} else { ?>
<input type="text" class="form-control timepicker" id="checkInTime" name="checkInTime" value="<?php echo date('H:i:s',strtotime($result->checkInTime));}?>" required parsley-maxlength="6" placeholder="checkInTime" disabled/>
<?php } ?>

In the sawe way that you echo ERROR, echo the HTML you want to render, within quotes (balance the quotes and escape them appropriately).
You could use something like this -
<input type="text" class="form-control timepicker" id="checkInTime" name="checkInTime" value="<?php if ($result->checkInTime == '0'||'NULL'){ echo "<button class=\"button-class\" value=\"button\"></button>" ;} else{ echo date('H:i:s',strtotime($result->checkInTime));}?>" required parsley-maxlength="6" placeholder="checkInTime" disabled/>

Related

Is it possible to store variable in input box in HTML or PHP

I would like to create an input box that is only readable and have the title of the book, is it possible that the input box can store a variable?
P.S The one I want to change is the id, now I successfully disabled the input box, but the output is $id instead of the variable that I use the $_GET method.
My code is as follow
<?php
include_once 'header.php';
$id = mysqli_real_escape_string($conn, $_GET['bookid']);
$title = mysqli_real_escape_string($conn, $_GET['title']);
?>
<section class="signup-form">
<h2>Pre-order</h2>
<div class="signup-form-form">
<form action="preorder.inc.php" method="post">
<input type="text" disabled="disabled" name="id" value= $id >
<input type="text" name="uid" placeholder="Username...">
<input type="text" name="BRO" placeholder="BookRegistrationCode...">
<button type="submit" name="submit">Pre-order</button>
</form>
</div>
<?php
// Error messages
if (isset($_GET["error"])) {
if ($_GET["error"] == "emptyinput") {
echo "<p>Fill in all fields!</p>";
}
else if ($_GET["error"] == "wronginfo") {
echo "<p>Wrong information!</p>";
}
else if ($_GET["error"] == "stmtfailed") {
echo "<p>Something went wrong!</p>";
}
else if ($_GET["error"] == "none") {
echo "<p>Success!</p>";
}
}
?>
</section>
Put <?php echo $var; ?> inside the value attribute.
<input type="text" value="<?php echo $var; ?>" />
EDIT
As per #arkascha's comment, you can use alternative php short tags:
<input type="text" value="<?= $var; ?>" />
As per the docs: https://www.php.net/manual/en/language.basic-syntax.phptags.php

PHP variables keep resetting

I am trying to use PHP to update an SQL table using HTML forms.
I want the user to be able to search for a VCR name and display it details in a while loop and then an update form will appear for the user to change its details in the database.
However every time i press the update button on the update form, the variables that hold the new details empty and become undefined.
<?php require('connect.php'); ?>
<?php require('headerPrivate.php'); ?>
<?php require('session.php');?>
<?php
//SEARCH PHP CODE
//THIS WORKS FINE AND ALL THE DETAILS APPEAR
if(isset($_POST["search"]))
{
//CREATE VARIABLES
$username=$_SESSION['username'];
echo "username: ".$username;
echo '<br>';
$vcrName=$_POST['name'];
echo "VCR Name: ".$vcrName;
echo '<br>';
echo '<br>';
//SELECT * FROM PRODUCT
$sql="SELECT *
FROM product
INNER JOIN user
ON product.owner_ID=user.user_ID
WHERE username='$username' AND name='$vcrName'";
echo "SQL SELECT 1: ".$sql;
echo '<br>';
echo '<br>';
//$vcrName=$_POST['name'];
$result = mysqli_query($con,$sql);
echo '<div class="row">';
echo '<div class="col-xs-6 col-md-4">';
while ($row_all = mysqli_fetch_assoc($result))
{
echo '<form method="post">';
echo "<u>Title: ".$row_all["name"].'</u>';
echo '<br>';
echo '<small>';
echo " Price: ".$row_all["price"];
echo '</small>';
echo '<br>';
echo "<p><u>Short Description:</u> ".$row_all["short_descripton"]."</p>";
echo '<br>';
echo "<p><u>Long Description:</u> ".$row_all["long_description"]."</p>";
echo '<br>';
echo '<hr>';
echo '</form>';
echo '<div>';
}
echo '</div>';
}
?>
<content>
<!--SEARCH FOR VCR NAME-->
<form class="form" method="post">
<label for="name" class="sr-only">VCR Name</label>
<input type="text" name="name" class="form-control" placeholder="VCR Name" required="" autofocus="" autocomplete="off">
<button name="search" type="search" class="btn btn-success btn-block">Search</button>
</form>
<?php
//This is where i run into issues. The old name in the variable $vcrName is empty and i need it for the update SQL statement.
//UPDATE PHP
if(isset($_POST["alter"]))
{
//CREATE A SESSION VARIABLE FOR THE CUSTOMER ID
$customer_ID=$_SESSION['customer_ID'];
echo "Customer ID: ".$customer_ID;
echo '<br>';
//CREATE VARIABLES
$changeTitle=$_POST["titleChange"];
$changesDescChange=$_POST["sDescChange"];
$changelDescChange=$_POST["lDescChange"];
$changepriceChange=$_POST["priceChange"];
$vcrName=$_POST['name'];
//UPDATE SQL
$sql_update="UPDATE product
SET
name='$changeTitle',
short_descripton='$changesDescChange',
long_description='$changelDescChange',
price='$changepriceChange'
WHERE
owner_ID='$customer_ID' AND name='$vcrName'";
echo "SQL Update 0: ".$sql_update;
echo '<br>';
echo '<br>';
echo "Updated Name: ".$changeTitle;
echo '<br>';
echo '<br>';
echo "SQL Update 1: ".$sql_update;
return $sql_update;
echo '<br>';
echo '<br>';
$result_update = mysqli_query($con,$sql_update);
if($result_update){
echo "Update Successful!";
}
else {
echo "Update Unsuccessful";
}
}
?>
<!--UPDATE FORM-->
<form class="form" method="post">
<label for="titleChange" class="sr-only">VCR Name</label>
<input type="text" name="titleChange" class="form-control" placeholder="VCR Name" required="" autofocus="" autocomplete="off">
<label for="sDescChange" class="sr-only">Short Description</label>
<input type="text" name="sDescChange" class="form-control" placeholder="Short Description" required="" autofocus="" autocomplete="off">
<label for="lDescChange" class="sr-only">Long Description</label>
<input type="text" name="lDescChange" class="form-control" placeholder="Long Description" required="" autofocus="" autocomplete="off">
<label for="priceChange" class="sr-only">Price</label>
<input type="text" name="priceChange" class="form-control" placeholder="Price" required="" autofocus="" autocomplete="off">
<button name="alter" type="submit">Change</button>
</form>
</content>
</body>
</html>
Your PHP code refers to a POST variable that doesn't exist:
$vcrName=$_POST['name'];
In your update form, you need to pass it as a hidden value:
<input type="hidden" name="name" value="<?=htmlspecialchars($vcrName)?>"/>
It's not clear if these are two separate PHP scripts (or if so, why they are) so you may need to put in a database call to get that value. From a user interface point of view, one is typically given the existing values when updating a record anyway. This would mean getting the data and giving each form element a value attribute.

How can I remove an empty space in php output?

<input type="hidden" name="status" class="status" value="
<?php if (!empty ($status)) {
echo $status;
} else {
echo "default";
} ?>"
/>
My result is:
<input type="hidden" name="status" class="status" value=" default">
My problem is: There is an empty space in value=" default"
And I cannot remove it. I cannot see, why it is there...
Change
<input type="hidden" name="status" class="status" value="
<?php if (!empty ($status)) {
echo $status;
} else {
echo "default";
} ?>"
/>
To:
<input type="hidden" name="status" class="status" value="<?php if (!empty ($status)) {
echo $status;
} else {
echo "default";
} ?>"
/>
Line breaks are validated as a space if in HTML scope. So you could, for example, place the opening PHP tag directly after value=". Anyway, let me make this suggestion to improve your code:
<input (...) value="<?php echo !empty($status) ? $status : 'default'; ?>">
Shorthand if statements are pretty useful for these cases because they are compact and are, if you get used to them, very readable.
Please note, that since HTML5 you don't need the self-escaping (/>) for tags that are marked as self-closing. the input tag is one of them.

form variables not passing

It seems my code is correct, however the posted variables in the form do not echo in the update user settings page in the form. I have echoed the posted ids from the input in the database but I cannot get the variables to show. I have done this in Codeigniter fine but am trying to do it in pure php with my own framework.
$users = new Users($db);
comes from my init.php file that is called at the beginning of the file below.
when I do
<?php var_dump($user['first_name'])?>
I get Null
<input type="text" name="first_name" value="<?php if (isset($_POST['first_name']) )
{echo htmlentities(strip_tags($_POST['first_name']));} else { echo
$user['first_name']; }?>">
Try $_REQUEST['first_name'] first before submitting it to array.
<?=$_REQUEST['first_name']?>
<input type="text" name="first_name" value="<?php if (isset($_POST['first_name']) )
{echo htmlentities(strip_tags($_POST['first_name']));} else { echo
$user['first_name']; }?>">
try this code
Is your form using get or post method? If it is using get method $_POST will be null. If you use $_REQUEST, it will show the result regardless of the http method used.
try it i think its work fine and solve your problam
<?php
if(isset('name of submit button ')) // example $_POST['submit'];
{
?>
<input type="text" name="first_name" value="<?php echo htmlentities(strip_tags($_POST['first_name'])) ?> " >
<?php
}
else
{
?>
<input type="text" name="first_name" value="<?php echo $user['first_name']; ?> " >
<?php
}
?>
try it
<?php
$first_name = $_POST['first_name'];
?>
<input type="text" name="first_name" value="<?php if (isset($first_name)){ echo htmlentities(strip_tags($first_name));} else { echo $users['first_name']; } ?> ">

HTML form not sending correct data, PHP pagination

I have recently used a PHP pagination tutorial, Pagination - what it is and how to do it, to display record information from a MySQL database. The problem is that the page only sends out the information sent in the latest form, and I am not quite sure how to fix the problem.
The code for the form output is shown below.
$musicitems = getmusicitems($pagenumber,$prevpage,$lastpage,$nextpage);
$count = ($musicitems==NULL) ? 0 : mysql_num_rows($musicitems);
for ($i=0;$i<$count;$i++)
{
$records = mysql_fetch_assoc($musicitems);
print'
<label for="deleteMusicItem'.$records['m_id'].'" id="deleteMusicItemLabel'.$records['m_id'].'">Delete Music Record:</label>
<input type="checkbox" name="deleteMusicItem" id ="deleteMusicItem'.$records['m_id'].'" value="delete" />
<br/>
<label for="artistname'.$records['m_id'].'" id="artistLabel'.$records['m_id'].'">Artist Name:</label>
<input type="text" size="30" name="artistname" class="artistname1" id ="artistname'.$records['m_id'].'" value="'.$records['artistname'].'" />
<br/>
<label for="recordname'.$records['m_id'].'" id="recordnameLabel'.$records['m_id'].'">Record Name:</label>
<input type="text" size="30" name="recordname" class="recordname1" id ="recordname'.$records['m_id'].'" value="'.$records['recordname'].'"/>
<br/>
<label for="recordtype'.$records['m_id'].'" id="recordtypeLabel'.$records['m_id'].'">Record type:</label>
<input type="text" size="20" name="recordtype" class="recordtype1" id ="recordtype'.$records['m_id'].'" value="'.$records['recordtype'].'"/>
<br/>
<label for="format'.$records['m_id'].'" id="formatLabel'.$records['m_id'].'">Format:</label>
<input type="text" size="20" name="format" class="format1" id ="format'.$records['m_id'].'" value="'.$records['format'].'"/>
<br/>
<label for="price'.$records['m_id'].'" id="priceLabel'.$records['m_id'].'">Price:</label>
<input type="text" size="10" name="price" class="price1" id ="price'.$records['m_id'].'" value="'.$records['price'].'"/>
<br/><br/>
';
$musicfiles=getmusicfiles($records['m_id']);
for($j=0; $j<2; $j++)
{
$mus=mysql_fetch_assoc($musicfiles);
if(file_exists($mus['musicpath']))
{
echo ''.$mus['musicname'].'<br/>';
}
else
{
echo '<label for="musicFile'.$records['m_id'].'" id="musicFileLabel'.$records['m_id'].'">Music:</label> <input type="file" size="40" name="musicFile1" id="musicFile'.$records['m_id'].'"/><br/>';
}
}
$pictures=getpictures($records['m_id']);
for($j=0;$j<2;$j++)
{
$pics=mysql_fetch_assoc($pictures);
if(file_exists($pics['picturepath']))
{
echo '<img src="'.$pics['picturepath'].'" width="150" height="150"><br/>';
}
else
{
echo '<label for="pictureFile'.$records['m_id'].'" id="pictureFileLabel'.$records['m_id'].'">Picture:</label><input type="file" size="40" name="pictureFile1" id="pictureFile'.$records['m_id'].'"/><br/>';
}
}
}
echo'<input type="submit" value="Submit" name="modfiymusicitem" id="modfiymusicitem" /> ';
if ($pagenumber == 1) {
echo " FIRST PREV ";
}
else {
echo " <a href='{$_SERVER['PHP_SELF']}?pagenumber=1'>FIRST</a> ";
$prevpage = $pagenumber-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenumber=$prevpage'>PREV</a> ";
}
echo "(Page $pagenumber of $lastpage)";
if ($pagenumber == $lastpage) {
echo " NEXT LAST ";
}
else {
$nextpage = $pagenumber+1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenumber=$nextpage'>NEXT</a> ";
echo " <a href='{$_SERVER['PHP_SELF']}?pagenumber=$lastpage'>LAST</a> ";
}
You have to pass the form data to the next page manually. This, most important part, is always forgotten by tutorial writers.
You have to pass to other pages, not only the page number, but the whole form data.
I hope your form uses the GET method, as it should be, so, you have your data either in the $_SERVER['QUERY_STRING'] as a string or the $_GET array. So, you can either do regexp pagenumber in the QUERY_STRING, or assemble another QUERY_STRING from the $_GET array, like this:
$_GET['pagenumber']=$nextpage;
$query_string=http_build_query($_GET);
echo " <a href='{$_SERVER['PHP_SELF']}?$query_string'>NEXT</a> ";

Categories