How can I remove an empty space in php output? - php

<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.

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 The value captured from the radio button is "On"

I'm trying to capture question ids and their answers. The problem
is that the value is "ON" when I print the array, it has the
question number correctly in the first cell but then the value in the
second cell is "ON". Here is my code:
function get_questions($quiz_id)
{
include'connection.php';
$stmt = $conn->prepare("select id,question,option1,option2,option3,option4,answer from questions where quiz_id=?");
$stmt->bind_param("i",$quiz_id);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($qid,$question,$option1,$option2,$option3,$option4,$answer);
$num_of_rows = $stmt->num_rows;
while($stmt->fetch()) {
echo $question;
echo "<br/>";
?>
<form method="POST"action="">
<input type="radio" name="radio[<?php echo $qid; ?>]" ]value="x"><?php echo $option1;?><br/>
<input type="radio" name="radio[<?php echo $qid; ?>]" ]value="y"><?php echo $option2;?><br/>
<input type="radio" name="radio[<?php echo $qid; ?>]" ]value="z"><?php echo $option3;?><br/>
<input type="radio" name="radio[<?php echo $qid; ?>]" ]value="f"><?php echo $option4;?><br/>
<!--<input type="radio" name="radio[<?php echo $qid; ?>]" ]value="<?php echo $option2;?>"><?php echo $option2;?><br/>
<input type="radio" name="radio[<?php echo $qid; ?>]" ]value="<?php echo $option3;?>"><?php echo $option3;?><br/>
<input type="radio" name="radio[<?php echo $qid; ?>]" ]value="<?php echo $option4;?>"><?php echo $option4;?><br/>
-->
<?php
}
?>
<input type="submit" name="submit" value="submit" >
</form>
<?php
if(isset($_POST['submit'])) {
$answers=$_POST['radio'];
print_r($answers);
// Iterate through each answer
}
}
Your problem is a simple syntax error:
]value="x">
This square bracket is changing the name of the attribute from value to ]value, so there is no value to be captured upon submission.
Also, make sure to move the opening form tag outside of your loop:
while($stmt->fetch()) {
...
<form method="POST"action=""> // <- this here opens a new tag with each iteration
because you close the tag outside the loop, meaning you're not creating valid html.
And while we're here, I'd suggest the alternative syntax when you have mixed php and html code:
<form method="POST" action="">
<?php while ($stmt->fetch()): ?>
<input type="radio"...
...
<?php endwhile ?>
<input type="submit" name="submit" value="submit" >
</form>
It makes it more readable and easier to follow than having to match curly braces.

Button inside echo

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/>

"Double Load" Issue with PHP isset() and $_POST data

I know I am doing something wrong by using a combination of isset(), $_POST and $_GET but I am wondering what would be the easiest and painless way to tackle my issue.
The issue arises when I submit a HTML form... It reloads the page with the post data. I capture the submit with a php isset() function, process the $_POST data and then run a window.location to refresh the page with some $_GET data.
For example...
//example.php
...
<form action="" method="post">
<input name="stage1name" type="text" class="textfield" size="22" value="<?php echo $ClaimRow['ClaimantName']; ?>">
<input name="VehicleEngine" type="text" class="textfield" size="20" value="<?php echo $vehiclerow['VehicleEngine']; ?>">
<input name="VehicleFuel" type="text" class="textfield" size="20" value="<?php echo $vehiclerow['VehicleFuel']; ?>">
<input name="submitInfo" type="submit" value="<?php echo $LANG_Claims_Change_Info; ?>" />
</form>
...
<?php
if (isset($_POST['submitInfo']))
{
$stage1name= mysqli_real_escape_string($db, $_POST['stage1name']);
$VehicleEngine= mysqli_real_escape_string($db, $_POST['VehicleEngine']);
$VehicleFuel= mysqli_real_escape_string($db, $_POST['VehicleFuel']);
mysqli_query($db, "DO SOME COOL SQL HERE");
//I've done what I need to do so lets reload the page with the updated data
echo "<script>window.location='example.php?vehicle=" . $vehicleID . "&claimTab=Personal'</script>";
}
?>
Like I said before, this method works fine however the user gets the effect of a "double load" and is very epiletic fit inducing.
Any ideas how best to combat this?
Thanks
EDIT - Additional Example
I realised that this one example might not make complete sense so I put together another example which hopefully will.
...
<form action="" method="post">
<select name="addresstype[]" id="multiselectfrom" onchange='this.form.submit();' size="9" style="width:110px; text-align:center;">
<option style="<?php if ($AddType == 'Claimant') { echo'background-color:#9AD3F1 !important;'; } ?>" value="Claimant"><?php echo $LANG_Claims_Claimant; ?></option>
<option style="<?php if ($AddType == 'Vehicle') { echo'background-color:#9AD3F1 !important;'; } ?>" value="Vehicle"><?php echo $LANG_Claims_Vehicle; ?></option>
<option style="<?php if ($AddType == 'Repairer') { echo'background-color:#9AD3F1 !important;'; } ?>" value="Repairer"><?php echo $LANG_Claims_Repairer; ?></option>
<option style="<?php if ($AddType == 'Insurer') { echo'background-color:#9AD3F1 !important;'; } ?>" value="Insurer"><?php echo $LANG_Claims_Insurer; ?></option>
<option style="<?php if ($AddType == 'Fleet') { echo'background-color:#9AD3F1 !important;'; } ?>" value="Fleet"><?php echo $LANG_Claims_Fleet; ?></option>
</select>
</form>
...
<?php
if (isset($_POST['addresstype']))
{
foreach ($_POST['addresstype'] as $addresstype) {
$addresstype2 = $addresstype;
}
echo "<script>window.location='claims.php?claimID=" . $claim . "&claimTab=Addresses&AddressType=" . $addresstype2 . "'</script>";
}
?>
The above example is supposed to take the result of the form and change the window.location depending on the form result. It does work however it loads the page twice in doing so.
Repalace:
echo "<script>window.location='example.php?vehicle=" . $vehicleID . "&claimTab=Personal'</script>";
With:
echo "<META HTTP-EQUIV = 'Refresh' Content = '0; URL =example.php?vehicle=" . $vehicleID . "&claimTab=Personal'>";
After much testing. It turns out CMorriesy was correct. However for future reference you will need to turn output_buffering = on in php.ini and also add <?php ob_start(); ?> to the very first line of your code.
header('Location: example.php?vehicle=' . $vehicleID . '&claimTab=Personal'); die();

Checkbox and Isset to update Mysql with PHP

I can't get my update checkbox function to work. I need to be able to remove or add a value which I have chosen to call checked in to my table. The code looks like following.
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Name <input type"text" name="inputName" value="<?php echo $hemsida['Namn']; ?>" /> </br>
Commentar <input type"text" name="inputComment" value="<?php echo $hemsida['Comment']; ?>" />
<br/>
</br><input type="checkbox" name="all" value="<?php echo $hemsida['All']; ?>"
<?php if($hemsida['All'] == 'checked') echo " checked"; ?> /> Alla
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" />
<input type="submit" name="submit" value="Redigera">
</form>
and the Update PHP looks like this
if(isset($_POST['submit'])) {
$all = ($_POST['All'] == 1) ? "checked" : "";
$u = "UPDATE hemsida SET `Namn`='$_POST[inputName]', `Comment`='$_POST[inputComment]', `ALL`=$all WHERE ID = $_POST[id]";
mysql_query($u) or die(mysql_error());
echo "User has been modified";
header("Location: ..//sokh.php");
}
The error is Undefined variable: hemsida on all parts. And also You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE ID = 33' at line 1. But I have no problem getting the data in to the Modifier or what I should call it but unable to get it out.
ANSWER !!!
I got it to work but cant answer my own question so i write it down here , i added and removed code until everything broke down. Remove the "$all = ($_POST['All'] == 1) ? checked : ;" part and now it works. I will copy the code underneath it there is an interest
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Name <input type"text" name="inputName" value="<?php echo $hemsida['Namn']; ?>" /> </br>
Commentar <input type"text" name="inputComment" value="<?php echo $hemsida['Comment']; ?>" />
<br/>
<input type="checkbox" name="all" value="checked" <?php if($hemsida['All'] == 'checked') echo "checked=\"checked\""; ?>/> Alla
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" />
<input type="submit" name="submit" value="Redigera">
</form>
the new php
if(isset($_POST['submit'])) {
$u = "UPDATE hemsida SET `Comment`='$_POST[inputComment]', `Namn`='$_POST[inputName]', `All`='$_POST[all]' WHERE ID = $_POST[id]";
mysql_query($u) or die(mysql_error());
echo "User has been modified";
header("Location: ..//sokh.php");
}
It seems that $hemsida is not available at that point in your code, but you are using it.
An input type="checkbox" does notshow up in $_POST when it is not checked. , use isset.
sidenote: you're using XHTML? the correct checked type = checked="checked"

Categories