I want to print a text field 6 times, however if all of them are filled then the loop should not continue. Here's my code:
<fieldset class="keywords">
<?php
$fkeywords = get_the_terms($pid, 'fkeywords');
if (is_array($fkeywords)) {
foreach ($fkeywords as $keyword) {
echo '<input type="text" name="fkeywords[]" id="'.$keyword->slug.'" value="'.$keyword->slug.'">';
}
}
?>
<ol>
<?php for ($i=0; $i<6; $i++ ){ ?>
<li><input type="text" size="20" name="foodir_keywords[]" /></li>
<?php } ?>
</ol>
</fieldset>
For HTML:
<input type="text" size="20" name="keywords[]" id="valueCheck" />
You can use this function for checking purpose:
function checkInput() {
var valueCheck = document.getElementById('valueCheck').value;
if(!valueCheck.match(/\S/)) {
alert ('EMPTY');
return false;
} else {
alert("Filled");
return true;
}
}
I somehow figured it out myself:
$keyCount = count($fkeywords);
for ($i=0; $i<6-$keyCount; $i++)
Related
I have a list a checkboxes with accompanying input text fields. If the user checks a box, the accompanying text field will be add to an array.
I am new to PHP and was wondering if anyone can help me in the right direction.
Should I use a for loop, foreach, while, unique "name" for each input, or something else?
Below is what I have so far.
<?php
if(isset($_POST['submit'])){
$array = array();
while(isset($_POST['check'])){
if(!isset($_POST[$some_text]) || empty($_POST[$some_text])){
echo "Please include your text for each checkbox you selected.";
exit();
}
$array[] = $_POST['some_text];
}
}
?>
<form>
<input type="checkbox" name="check"><input type="text name="some_text">
<input type="checkbox" name="check"><input type="text name="some_text">
<input type="checkbox" name="check"><input type="text name="some_text">
<!-- I might have around 100 of these -->
<!-- submit button here -->
</form>
You first need a way to associate your checkboxes with their corresponding text fields, and a way to tell them apart. For example:
<form>
<input type="checkbox" name="check[]" value="1"><input type="text name="text1">
<input type="checkbox" name="check[]" value="2"><input type="text name="text2">
<input type="checkbox" name="check[]" value="3"><input type="text name="text3">
</form>
Now you can loop through it as follows:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST['check']) && is_array($_POST['check']) && !empty($_POST['check'])) {
$array = array();
foreach ($_POST['check'] as $value) {
$text_field = 'text' . $value;
if (!isset($_POST[$text_field]) || trim($_POST[$text_field]) == '') {
echo "Please include your text for each checkbox you selected.";
exit;
}
$array[] = $_POST[$text_field];
}
}
}
Try this:
$select = array();
foreach($_POST['check'] as $key => $selected){
$select[$key] = $selected;
}
Please try this:
<?php
if(isset($_POST['submit'])){
for($i=0; $i<100; $i++)
{
if($_POST['check_'.$i])
{
$array[] = $_POST['input_'.$i];
}
}
}
?>
#################### HTML
<form method="post" action="">
<?php for($i=0; $i<100; $i++) { ?>
<input type="checkbox" name="check_<?php echo $i ?>"><input type="text" name="input_<?php echo $i ?>">
<?php } ?>
<input type="submit" name="submit">
</form>
What I'm basically looking for is the ability to remove a certain item from the array when that item is clicked, for this instance... If I hit "Two", it will disappear.
Demo: http://query.notesquare.me
CODE:
<form method="post">
<input type="text" id="input-create-playlist" placeholder="Playlist Name" name="create_playlist" />
<input type="submit" id="button-create-playlist" value="Create Playlist" />
</form>
<?php
ini_set("session.save_path", "/home/kucerajacob/public_html/query.notesquare.me/test-sessions");
session_start();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$create_playlist = $_POST['create_playlist'];
$_SESSION['user_playlists'][] = $create_playlist;
}
$playlists = array("One", "Two", "Three");
if (isset($_SESSION['user_playlists'])) {
for ($i = 0; $i < count($_SESSION['user_playlists']); $i++) {
array_unshift($playlists, $_SESSION['user_playlists'][$i]);
}
}
$_SESSION['main'] = $playlists;
for ($i = 0; $i < count($playlists); $i++) {
echo $playlists[$i] . "<br />";
}
?>
Its possible, you'll need to handle that request as well. If you want the click posted, a simple <button> next to that should suffice.
Upon rendering the markup, (of course using the session array) use the key which can be used in unsetting the values.
<?php
// initialization
if(empty($_SESSION['user_playlists'])) {
$_SESSION['user_playlists'] = array("One", "Two", "Three");
}
if(isset($_POST['add'], $_POST['create_playlist'])) {
// handle additions
$_SESSION['user_playlists'][] = $_POST['create_playlist'];
}
if(isset($_POST['remove'])) {
// handle remove
$key = $_POST['remove'];
unset($_SESSION['user_playlists'][$key]);
}
?>
<form method="post">
<input type="text" id="input-create-playlist" placeholder="Playlist Name" name="create_playlist" />
<input type="submit" id="button-create-playlist" name="add" value="Create Playlist" />
<hr/>
<?php foreach($_SESSION['user_playlists'] as $k => $p): ?>
<?php echo $p; ?> <button type="submit" name="remove" value="<?php echo $k; ?>">Remove</button><br/>
<?php endforeach; ?>
</form>
Sample Demo
Try below:
for ($i = 0; $i < count($playlists); $i++) {
// echo $playlists[$i] . "<br />";
printf('%1$s<br/>', $playlists[$i]);
}
if ($_GET['query'])
{
unset($playlists['query']);
}
in my code, when we click ok button . It's will echo 3
I want to apply to count only input that have value only
How to do ?
<?php
if(isset($_POST["submit"]))
{
echo count($_POST["to_more"]);
}
?>
<form name="f1" method="post">
<input type="text" name="to_more[]">
<input type="text" name="to_more[]">
<input type="text" name="to_more[]">
<input type="submit" name="submit" value="OK">
</form>
try this
<?php
if(isset($_POST["submit"]))
{
echo count(array_filter($_POST["to_more"]));
}
?>
How about
if(isset($_POST["submit"])){
$count = 0 ;
foreach($_POST["to_more"] as $data){
if($data != '') $count++;
}
if($count > 0)
echo $count;
}
array_filter should help
<?php
$ar = isset($_POST["to_more"]) ? $_POST["to_more"] : array();
$ar = array_filter($ar, function($el){ return !empty($el);});
echo count($ar);
?>
Should be quicker than foreach, btw.
UPD: Oh, seems, NLSaini posted the precise solution, check it.
I'm doing form in php but I have some problem.
First I will have 3 different form in the same page.
What I want is only 1 form appear and then with the answer a second form will appear and so on.
The answer of the form will be display on the same page.
For now my first form work and after get the answer go to the 2nd form but I want to submit the 2nd form the problem appear.
It delete the answer of my first form and don't do anything (everything start like I am in my first form).
I try to find the problem but can't have idea about how to solve it.
Here is my code:
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
Q1?
<input type="number" name="nbtemplate" min="1" max="30">
<input type="submit" name="submitbutton1" value="Confirm!">
</form>
<?php
if(!isset($submitbutton1)) {
if (!empty($_POST['nbtemplate']) != "") {
echo "<b>{$_POST['nbtemplate']}</b> !\n";
echo "<br />";
$Nnbtemplate = $_POST['nbtemplate'];
$result = mysql_query("UPDATE tb SET day='$Nnbtemplate'") or die(mysql_error());
?>
<form action='<?php echo $_SERVER['PHP_SELF'];?>' method='post'>
Q2? <br>
<?php
for ($i = 1; $i <= $Nnbtemplate; $i++) { // start loop
echo "Template ";
echo $i;
?>
<input type="number" name="nbtime" min="1" max="96">
<?php
}
echo '<input type="submit" name="submitbutton2" value="Confirm!">';
echo '</form>';
if(isset($submitbutton1) && !isset($submitbutton2)) {
if (!empty($_POST['nbtime']) != "") {
echo "<b>{$_POST['nbtime']}</b> !\n";
echo "<br />";
$nbtime = $_POST['nbtime'];
for ($j = 1; $j <= $nbtime; $j++) {
echo "Time";
echo $j;
?>
Q3:
<input type="time" name="starttime"> To <input type="time" name="endtime">
<?php
}
echo '<input type="submit">';
echo '</form>';
}
}
}
}
?>
That is some gnarly code you got there, brother. This is a really simple task when handled with some javascript. Then you can have a single post to your php. I like using the jQuery framework so here's a couple links I found quickly: this one and this one
Example code in response to comment about building form elements dynamically:
<html>
<head>
<!-- load jquery library -->
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
</head>
<body>
<form action="toyourpage.php">
How Many?:
<input type="text" name="number" id="number">
<div id="add"></div>
</form>
<!-- javascript go -->
<script type="text/javascript">
$(document).ready(function()
{
$('input#number').keyup(function()
{
var num = $(this).val(); // get num
if(!isNaN(num)) // check if number
{
$('div#add').html(''); // empty
for(i = 1; i <= num; i++) // add
{
$('div#add').append('New Field ' + i + ': <input type="text" name="next_' + i + '" id="next' + i + '"><br>');
}
}
else
{
alert('Valid number required');
}
});
});
</script>
</body>
</html>
I did some changes on Your code, and have tested, it works.
You had any mistakes in your {} brackets and if conditions. Also as I commented I added extract($_POST).
<?php
extract ( $_POST );
if (! isset ( $submitbutton1 ) && !isset($submitbutton2)) {
?>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
Q1? <input type="number" name="nbtemplate" min="1" max="30"> <input
type="submit" name="submitbutton1" value="Confirm!">
</form>
<?php ;
}
if (isset ( $submitbutton1 )) {
if (! empty ( $_POST ['nbtemplate'] ) != "") {
echo "<b>{$_POST['nbtemplate']}</b> !\n";
echo "<br />";
$Nnbtemplate = $_POST ['nbtemplate'];
$result = mysql_query("UPDATE tb SET day='$Nnbtemplate'") or
die(mysql_error());
?>
<form action='<?php echo $_SERVER['PHP_SELF'];?>' method='post'>
Q2? <br>
<?php
for($i = 1; $i <= $Nnbtemplate; $i ++) { // start loop
echo "Template ";
echo $i;
?>
<input type="number" name="nbtime" min="1" max="96">
<?php
}
echo '<input type="submit" name="submitbutton2" value="Confirm!">';
echo '</form>';
}
}
if ( isset ( $submitbutton2 )) {
if (! empty ( $_POST ['nbtime'] ) != "") {
echo "<b>{$_POST['nbtime']}</b> !\n";
echo "<br />";
$nbtime = $_POST ['nbtime'];
for($j = 1; $j <= $nbtime; $j ++) {
echo "Time";
echo $j;
?>
Q3:
<input type="time" name="starttime"> To <input type="time"
name="endtime">
<?php
}
echo '<input type="submit">';
echo '</form>';
}
}
?>
I have this code, I have removed some code that has nothing to do with my problem.
while ($row_pl = mysql_fetch_array($res_pl))
{
// Uurtarief
$sql_uur = "SELECT
aantal_kop,
tarief_min,
tarief_max,
tijd_min,
tijd_max,
tijd_extra
FROM uurtarief
WHERE machine = '".$row_pl['machine']."'
ORDER BY aantal_kop ASC";
if(!$res_uur = mysql_query($sql_uur))
{
include('includes/errors/database_error.php');
}
else
{
while ($row_uur = mysql_fetch_array($res_uur))
{
?>
<input type="text" name="tar_kop[]" size="8" value="<?php echo $row_uur['aantal_kop']; ?>" />
<input type="text" name="tar_tarief[]" size="8" value="<?php echo $uurtarief; ?>" />
<input type="text" name="tar_tijd_extra[]" size="8" value="<?php echo $row_uur['tijd_extra']; ?>" />
<br />
<?php
}
}
?>
<tr>
<td>
<input type="text" name="pl_aantal_kop[]" size="3" onChange="uur_tarief(this, <?php echo $i ?>)" value="" />
</td>
<td>
<input type="text" name="pl_tarief_ph[]" size="4" value="" />
</td>
</tr>
<?php
}
With javascript I'm trying to find the value in pl_aantal_kop[] and send the corresponding parameter to pl_tarief_ph[]
function uur_tarief(selectVeld, nr)
{
if(document.getElementsByName('pl_aantal_kop[]')[nr].value == 1)
{
document.getElementsByName('pl_tarief_ph[]')[nr].value = document.getElementsByName('tar_tarief[]')[0].value;
document.getElementsByName('pl_snijtijd_extra[]')[nr].value = document.getElementsByName('tar_tijd_extra[]')[0].value;
}
if(document.getElementsByName('pl_aantal_kop[]')[nr].value == 2)
{
document.getElementsByName('pl_tarief_ph[]')[nr].value = document.getElementsByName('tar_tarief[]')[1].value;
document.getElementsByName('pl_snijtijd_extra[]')[nr].value = document.getElementsByName('tar_tijd_extra[]')[1].value;
}
if(document.getElementsByName('pl_aantal_kop[]')[nr].value == 3)
{
document.getElementsByName('pl_tarief_ph[]')[nr].value = document.getElementsByName('tar_tarief[]')[2].value;
document.getElementsByName('pl_snijtijd_extra[]')[nr].value = document.getElementsByName('tar_tijd_extra[]')[2].value;
}
if(document.getElementsByName('pl_aantal_kop[]')[nr].value == 4)
{
document.getElementsByName('pl_tarief_ph[]')[nr].value = document.getElementsByName('tar_tarief[]')[3].value;
document.getElementsByName('pl_snijtijd_extra[]')[nr].value = document.getElementsByName('tar_tijd_extra[]')[3].value;
}
if(document.getElementsByName('pl_aantal_kop[]')[nr].value == 5)
{
document.getElementsByName('pl_tarief_ph[]')[nr].value = document.getElementsByName('tar_tarief[]')[4].value;
document.getElementsByName('pl_snijtijd_extra[]')[nr].value = document.getElementsByName('tar_tijd_extra[]')[4].value;
}
if(document.getElementsByName('pl_aantal_kop[]')[nr].value == 6)
{
document.getElementsByName('pl_tarief_ph[]')[nr].value = document.getElementsByName('tar_tarief[]')[5].value;
document.getElementsByName('pl_snijtijd_extra[]')[nr].value = document.getElementsByName('tar_tijd_extra[]')[5].value;
}
}
The problem that occurred is that the javascript keeps starting counting at the first line of the while ($row_uur = mysql_fetch_array($res_uur)) loop instead of the field that belongs to the first loop. Below an screenshot of this loop.
Any suggestions?
You can condense your JS to this:
function uur_tarief(selectVeld, nr)
{
var index = document.getElementsByName('pl_aantal_kop[]')[nr].value;
document.getElementsByName('pl_tarief_ph[]')[nr].value = document.getElementsByName('tar_tarief[]')[index - 1].value;
document.getElementsByName('pl_snijtijd_extra[]')[nr].value = document.getElementsByName('tar_tijd_extra[]')[index - 1].value;
}
Can you try this code and post back? Where are you initializing and calling the JavaScript code? The code you posted looks fine (although bloated severely), so it might be an issue elsewhere.