Put the value as title in text field - php

i want to get the partcular text value as tittle in that field
How can i do..
<input type="text"
<?php if($name_one != '') { ?> value="<?php echo $name_one;?>" <?php echo set_value('name1'); ?>
<?php } else { ?> value="<?php echo set_value('name1'); ?>" <?php } ?>
name="act1" id="act1" title="<?php echo set_value('name1'); ?>">

You can try like this
<input type="text"
value="<?php echo (!empty($name_one)) ? $name_one : $name1 ; ?>"
Single line if condition

Related

How to post only variable with values and ignore blank inputs in PHP?

Forms input value variables are the same changing only labels for it from DB. When form selected other ones is hidden with "style.display = "none".
The problem that I face how to prevent sending empty values from none visible input fields?
Tried a trim function but didn't work for me. Maybe didn't use it correctly.
I'm trying to accomplish that input value would stay the same regardless of what user would select and only labels would change.
dump output
<?php
if (isset($_POST["save"])) {
if (empty($_POST['price'])) {
$error["price"] = "Price Required";
} else {
$price = $_POST["price"];
}
if (array_filter($error)) {
echo "Input error";
} else {
$price = ($_POST['price']);
$value1 = ($_POST['value1']) ?? null;
$value2 = $_POST['value2'] ?? null;
$value3 = $_POST['value3'] ?? null;
}
}
// insert size
$sql = "INSERT INTO valueSize(value1, value2, value3) values('$value1', '$value2, '$value3')";
$stmt = $pdo->prepare($sql);
$stmt->execute();
?>
<!DOCTYPE html>
<html lang="en">
<form action="">
<?php foreach ($categoriesTypeId as $key): ?>
<div class="dropItem invisible" id="<?php echo $key['id'] ?>">
<label for="value1"><?php echo $key['value1Type'] ?>:</label>
<input type="text" name="value1[]" value="<?php echo $value1 ?>">
<div class="error"><?php echo $error['value1'] ?></div>
<?php if (isset($key['value2Type'])) {?>
<div class="inputCheck">
<label for="value2"><?php echo $key['value2Type'] ?>
:</label>
<input type="text" name="value2" value="<?php echo $value2 ?>">
<div class="error"><?php echo $error['value2'] ?></div>
</div>
<?php
}
;
?>
<?php if (isset($key['value3Type'])) {?>
<div class="inputCheck">
<label for="value3"><?php echo $key['value3Type'] ?>
:</label>
<input type="text" name="value3" value="<?php echo $value3 ?>">
<div class="error"><?php echo $error['value3'] ?></div>
</div>
<?php
} else {
}
?>
<?php endforeach;?>
</form>
</html>

Checkboxes Do Not Get Processed Even Without Pagination

I'm aware that with pagination, if a checkbox is clicked and you move onto another page, that checked checkbox is no longer recognized, so I made my php pull depending on a category chosen by the user, and each row spits out in a w3css w3-third div:
while($rowtbl = mysqli_fetch_array($resulttbl)) {
?>
<div id="parent">
<div class="<?php echo $rowtbl['brand']?>">
<section class="allitems">
<div class="w3-third w3-margin-bottom" id="itemsToFilter" data-type="<?php echo $rowtbl['brand']?>">
<ul class="w3-ul w3-border w3-center w3-hover-shadow" >
<li class="w3-padding-16 w3-green">Desc: <b><?php echo htmlspecialchars($rowtbl['description']) ?></b>
<input type="hidden" name="descriptionid<?php echo $i; ?>" value="<?php echo $rowtbl['description']; ?>" /></li>
<li class="w3-padding-4">Brand: <b><?php echo htmlspecialchars($rowtbl['brand']) ?></b>
<input type="hidden" name="brandid<?php echo $i; ?>" value="<?php echo $rowtbl['brand']; ?>" /></li>
<li class="w3-padding-4">Category: <b><?php echo htmlspecialchars($rowtbl['category']) ?></b>
<input type="hidden" name="categoryid<?php echo $i; ?>" value="<?php echo $rowtbl['category']; ?>" /></li>
<li class="w3-padding-4">Item#: <b><?php echo htmlspecialchars($rowtbl['itemNu']) ?></b>
<input type="hidden" name="itemid<?php echo $i; ?>" value="<?php echo $rowtbl['itemNu']; ?>" /></li>
<li class="w3-padding-2">Pack Size: <b><?php echo htmlspecialchars($rowtbl['packsz']) ?></b>
<input type="hidden" name="packszid<?php echo $i; ?>" value="<?php echo $rowtbl['packsz']; ?>" /></li>
<?php if ($rowtbl['bc'] == "Y") { ?>
<li class="w3-padding-10">Broken Cs: <b><input type="checkbox" value="checkedup" name="bcase<?php echo $i; ?>" /></b></li>
<?php }
else { ?>
<li class="w3-padding-10">Broken Cs: <b><input type="checkbox" value="notcheckedup" name="bcase<?php echo $i; ?>" disabled /></b></li>
<?
}
?>
<li><input type="checkbox" name="checkbox<?php echo $i; ?>" /></li>
<input type="hidden" name="roses" id="roses" value="<?php echo $i; ?>" />
<input type="hidden" name="filler<?php echo $i; ?>" id="filler" />
</ul>
</div>
</section>
</div>
</div>
<?php $i++; //increment counter variable
} ?>
Everything is fine in regards to adding any item(s) to the db, until I get to any div passed 142. After that, when i click add, the page refreshes as if it was processed, but nothing gets added to the db.
What am I doing wrong?
Thanks in advance!
Edit: showing the php that adds items to the db:
<?php
if(isset($_POST['submitnew'])){
for ($i=1;$i<=$_POST['roses'];$i++)
{
if(isset($_POST['checkbox'.$i]))
{
$custnum = $_SESSION['customerNumber'];
$category = $_POST['categoryid'.$i];
$brand = $_POST['brandid'.$i];
$description = $_POST['descriptionid'.$i];
$pksz = $_POST['packszid'.$i];
$itemnum = $_POST['itemid'.$i];
if($_POST['bcase'.$i] == "notcheckedup")
{
$bcc = "n";
$eaa = 0;
} elseif ($_POST['bcase'.$i] == "checkedup" && isset($_POST['bcase'.$i]))
{ $bcc = "y";
$eaa = 1;
} else
{
$bcc = "y";
$eaa = 0;
};
$ordNew = "INSERT INTO `abc123` (`CustNo`,`category`, `brand`, `description`, `pksz`, `ea`, `itemNu`, `bc`) values('$custnum', '$category', '$brand', '$description', '$pksz', '$eaa', '$itemnum', '$bcc')";
mysqli_query($link, $ordNew);
}
header("Location: index.php");
}
}
?>

Passing checkbox values into an array?

I have set up a group of checkboxes. They are dynamic, so the number of checkboxes will be different dependent on the person using the site. The structure of how the checkboxes are created is:
<label for="plabackformat-holder-label">Format</label>
<div class=" playbackformat-holder-<?php echo $num; ?> playbackformat-holder">
<div class="playback-format-radio-buttons">
<label for="notset-<?php echo $num; ?>">
<input type="checkbox" class="playbackformat-holder-radiobutton" value="notset" name="playback_format[<?php echo $second_num; ?>]" id="notset-<?php echo $num; ?>" <?php if($field['playback_format'] == 'notset') { echo 'checked'; } ?>>None
</label>
<label for="dvd-<?php echo $num; ?>">
<input type="checkbox" class="playbackformat-holder-radiobutton" value="dvd" name="playback_format[<?php echo $second_num; ?>]" id="dvd-<?php echo $num; ?>" <?php if($field['playback_format'] == 'dvd') { echo 'checked'; } ?>>DVD
</label>
<label for="bluray-<?php echo $num; ?>">
<input type="checkbox" class="playbackformat-holder-radiobutton" value="bluray" name="playback_format[<?php echo $second_num; ?>]" id="bluray-<?php echo $num; ?>" <?php if($field['playback_format'] == 'bluray') { echo 'checked'; } ?>>Bluray
</label>
<label for="3d-<?php echo $num; ?>">
<input type="checkbox" class="playbackformat-holder-radiobutton" value="3d" name="playback_format[<?php echo $second_num; ?>]" id="3d-<?php echo $num; ?>" <?php if($field['playback_format'] == '3d') { echo 'checked'; } ?>>3d
</label><br />
</div>
</div>
My save function is:
$new = array();
for ( $i = 0; $i < $count; $i++ ) {
$new[$i]['playback_format'] = $playbackFormats[$i];
}
I've been reading up on this issue and it seems its because my input fields do not contain unique names. I'm trying to store the data into an array, so it would be ['playback_format'] => dvd,3d,bluray or something similar.
Right now its only storing the last checked value. Is there a way I can use a forloop or something to iterate over the checked values and push them into my array??
You can just get rid of the "$second_num" in each <input name="playback_format[]"/> html tag. This will put everything into an array for you once you submit the form. You can check this by adding this line to the page as a test.
<?php print_r($_REQUEST['playback_format']); ?>
Generally, You want to avoid any loop if they aren't required.
Hope that helps with what you are doing.
What is $second_num? Does it need to be a part of the input name?
You can get PHP to recognise the submitted values as an array if you do it this way:
<input name="playback_format[<?php echo $second_num; ?>][]">
Or if you don't need $second_num as part of the name, just:
<input name="playback_format[]">
$_POST['playback_format'] will then be an array containing all the selected options.
There is a section in the PHP docs specifically about this behaviour.
Checkboxes have all the same name in your example. Name it differently like :
<label for="plabackformat-holder-label">Format</label>
<div class=" playbackformat-holder-<?php echo $num; ?> playbackformat-holder">
<div class="playback-format-radio-buttons">
<label for="notset-<?php echo $num; ?>">
<input type="checkbox" class="playbackformat-holder-radiobutton" value="notset" name="playback_format_notset" id="notset-<?php echo $num; ?>" <?php if($field['playback_format_notset'] == 'notset') { echo 'checked'; } ?>>None
</label>
<label for="dvd-<?php echo $num; ?>">
<input type="checkbox" class="playbackformat-holder-radiobutton" value="dvd" name="playback_format_dvd" id="dvd-<?php echo $num; ?>" <?php if($field['playback_format_dvd'] == 'dvd') { echo 'checked'; } ?>>DVD
</label>
<label for="bluray-<?php echo $num; ?>">
<input type="checkbox" class="playbackformat-holder-radiobutton" value="bluray" name="playback_format_bluray" id="bluray-<?php echo $num; ?>" <?php if($field['playback_format_bluray'] == 'bluray') { echo 'checked'; } ?>>Bluray
</label>
<label for="3d-<?php echo $num; ?>">
<input type="checkbox" class="playbackformat-holder-radiobutton" value="3d" name="playback_format_3d" id="3d-<?php echo $num; ?>" <?php if($field['playback_format_3d'] == '3d') { echo 'checked'; } ?>>3d
</label><br />
</div>
</div>
And Try this in PHP :
//WHen you want to see what is send in Post :
var_dump($_POST);
//So, for get result :
$tab = array();
foreach($_POST as $key =>$value){
$tab[$key] = $value;
//Display it
echo $key . "=" . $value;
}

php input class error handling?

I have a login div with two input fields. Every input field will be checked for errors.
When I submit the data there are some problems. Here is the HTML:
<div class="small">
<?php if (!isset($errors['a'])):?>
<input type="text" id="a" name="a" value="<?php echo isset ($_POST['a'])? $_POST['a'] : 'Login'; ?>"/>
<?php endif;?>
<?php if (isset($errors['a'])):?>
<input class="log-error" type="text" id="a" name="a" value="<?php echo isset ($_POST['a'])? $_POST['a'] : 'Login'; ?>"/>
<?php endif;?>
</div>
<?php if (isset($errors['a'])):?>
<?php echo "<div id='errordiv'></div><div class='error'>";
echo $errors['a'][0];
echo "</div>";
endif;?>
<div class="small">
<?php if (!isset($errors['b'])):?>
<input type="text" id="b" name="b" value="<?php echo isset ($_POST['b'])? $_POST['b'] : 'Password'; ?>"/>
<?php endif;?>
<?php if (isset($errors['b'])):?>
<input class="log-error" type="text" id="b" name="b" value="<?php echo isset ($_POST['b'])? $_POST['b'] : 'Password'; ?>"/>
<?php endif;?>
</div>
<?php if ( ( isset($errors['b']) ) && (empty($errors['a'][0]) == true) ):?>
<?php echo "<div id='errordiv'></div><div class='error'>";
echo $errors['b'][0];
echo "</div>";
endif;?>
When submitting the data, it won't display both errors at the same time for the input class. It seems like the value-tag is causing this problem.
When I remove the value-tag from both input fields, it will display the error input class at the same time for both.
To me it seems like there is logic misunderstanding.
If you want to display both errors at the time - replace this line
<?php if ( ( isset($errors['b']) ) && (empty($errors['a'][0]) == true) ):?>
with this
<?php if (isset($errors['b'])):?>

Dynamically Generated Radio button form when submitted does not display data

I am trying to create a quiz with PHP/Mysql...
I have created a form with radio buttons for answers which displays data pulled from the database as values for the radio buttons. I tried to submit the form but the result page does not show anything.
My quiz code goes as follows:
<form method="post" action="insertscore.php" name="cssCheckbox" id = "cssCheckbox">
<?php $query = "SELECT * FROM questions WHERE (`topics` = '.NET' OR `topics` = 'PHP') ORDER BY Rand() LIMIT 5"; $result = mysql_query($query);
if ($result && mysql_num_rows($result)) {
$numrows = mysql_num_rows($result);
$count =1;
while ($row = mysql_fetch_array($result))
{
?>
<div class="group">
<input type="hidden" name="<?php echo $row['key_id']; ?>"><?php $row['key_id']; ?></input>
<span class="test_question"><strong><?php echo $count;?>) <?php echo $row['question']; ?>
</strong><br />
<?php if($row['answer1'] != NULL){ ?>
<input type = "radio" name="answers" value="<?php echo $row['answer1']; ?>" id="chkLimit_1" ></input>
<label for="chkLimit_1" ><?php echo $row['answer1']; echo "<br />"; } else {} ?></label>
<?php if($row['answer2'] != NULL){ ?>
<input type = "radio" name="answers" value="<?php echo $row['answer2']; ?>" id="chkLimit_2" ></input>
<label for="chkLimit_2" ><?php echo $row['answer2']; echo "<br />"; } else {} ?></label>
<?php if($row['answer3'] != NULL){ ?>
<input type = "radio" name="answers" value="<?php echo $row['answer3']; ?>" id="chkLimit_3" ></input>
<label for="chkLimit_3" ><?php echo $row['answer3']; echo "<br />"; } else {} ?></label>
<?php if($row['answer4'] != NULL){ ?>
<input type = "radio" name="answers" value="<?php echo $row['answer4']; ?>" id="chkLimit_4" ></input>
<label for="chkLimit_4" ><?php echo $row['answer4']; echo "<br />"; } else {} ?></label>
<?php if($row['answer5'] != NULL){ ?>
<input type = "radio" name="answers" value="<?php echo $row['answer5']; ?>" id="chkLimit_5" ></input>
<label for="chkLimit_5" ><?php echo $row['answer5']; echo "<br />"; } else {} ? ></label>
<?php if($row['answer6'] != NULL){ ?>
<input type = "radio" name="answers" value="<?php echo $row['answer6']; ?>" id="chkLimit_6" ></input>
<label for="chkLimit_6" ><?php echo $row['answer6']; echo "<br />"; } else {} ?></label>
<?php if($row['answer7'] != NULL){ ?>
<input type = "radio" name="answers" value="<?php echo $row['answer7']; ?>" id="chkLimit_7" ></input>
<label for="chkLimit_7" ><?php echo $row['answer7']; echo "<br />"; } else {} ?></label>
<?php if($row['answer8'] != NULL){ ?>
<input type = "radio" name="answers" value="<?php echo $row['answer8']; ?>" id="chkLimit_8" ></input>
<label for="chkLimit_8" ><?php echo $row['answer8']; echo "<br />"; } else {} ?></label>
<input type="hidden" name="<?php echo $row['right_answer']; ?>"><?php $row['right_answer']; ?></input>
</div>
<input name="Submit" type="submit" value="Submit Your Answers" class="submit">
</form>
Code on submitted Page looks like:
<?php
if(isset($_POST['Submit'])){
$key_id=$_POST['key_id']; echo $key_id;
$question=$_POST['question']; echo $question;
$answers=$_POST['answers']; echo $answers;
$correctanswer=$_POST['correctanswer']; echo $correctanswer;
}
foreach($_POST as $key => $val)
{
echo "$key --> $val<br />";
}
//var_dump($_POST);
?>
Please let me know if anything is not clear or if I am missing anything....
Thanks,
Shank
I would:
remove comments to //var_dump($_POST); and move this line at the top of the code on submitted Page.
if you still don't see anything, I think the code on submitted page is not in a file called insertscore.php or such file is not in same folder of your form page.

Categories