checkboxes when checked, delete checked messages - php

How to make that at the push of a button
"<input type ='submit' name='delete' value='Delete messages'>"
deleted messages selected by a checkbox? All messages and checkboxes appear through the counter "for" as messages are received. What im doing wrong? I think i have mistake with foreach and form action. Please, help me
my code is:
<php function display_list($auth_user, $accountid, $messageid, $fullheaders) {
if(!$accountid) {
echo "<p style=\"padding-bottom: 100px\">No mailbox selected.</p>";
} else {
$imap = open_mailbox($auth_user, $accountid);
if($imap) {
$headers = imap_headers($imap);
$messages = count($headers); ?>
<div class="view-mailbox-block">
<div class="new-message">
<form action="index.php?action=new-message" method="post">
<input type ='submit' value='Write a letter'>
</form>
</div>
<form action="" method="post">
<input type ='submit' name='delete' value='Delete messages'>
</form>
</div>
<div class="incoming-sent-messages-mail">
<p>Почта</p>
</div>
<div class="incoming-sent-messages">
<div class="incoming-sent-messages-p">
<p>
<?php echo "<a href='index.php?action=view-mailbox'>"; ?> View </a>
</p>
<p>
<?php echo "<a href='index.php?action=view-mailbox-sent'>"; ?> Sent </a>
</p>
</div>
</div>
<div>
<div class="senders overflow">
<form class="sender-checkbxes">
<?php echo '<input type="checkbox" name="deleteall" value="checkbox-all">'; ?>
<?php for ($i = 0; $i < $messages; $i++) {
echo "<div><div>";
echo '<input type="checkbox" name="delete[]" value="<?php echo $delete ?>">';
echo "</form>";
echo "</div></div>\n";
} ?>
<form action='index.php?action=delete&messageid='<?php echo $check;?>'>
<?php if(!empty($_POST['delete'])) {
foreach($_POST['delete'] as $check) {
imap_delete($imap, $check);
imap_expunge($imap);
imap_close($imap);
return true;
}
} ?>
</form>
</div>

Related

my other if else statement is executing php

I have 3 forms with 3 radios each but executed one at a time because of random generator. If I click a radio, it will go to next page and will be checked by the if else statement. Now my problem is, when it gets answered and it's correct, it will output yes. However, my other if else statement of my other radio executes. Since it's not answered, it assumed that it was wrong, so it outputted no.
here's my code:
<!doctype html>
<html>
<head>
<title>Question and Answer</title>
</head>
<body>
<?php
//Creating random numbers
$rid = rand(1,3);
?>
<?php
if ($rid == 1){
echo "
<form action='answer.php?id=1' method='post' id='quizForm' id='1'>
<ol>
<li>
<h3>What does HTML Stands For ?</h3>
<div>
<input type='radio' name='answerOne' id='answerOne' value='A' />
<label for='answerOneA'>A) Hyper text markup language</label>
</div>
<div>
<input type='radio' name='answerOne' id='answerOne' value='B' />
<label for='answerOneB'>B) Hyper turn mark lingo</label>
</div>
<div>
<input type='radio' name='answerOne' id='answerOne' value='C' />
<label for='answerOneC'>C) Happy tissue mahatma life</label>
</div>
</li>
<input type = 'submit' name = 'submit1' value = 'Choose..'>
</form>";
}
if ($rid == 2){
echo "
<form action='answer.php?id=1' method='post' id='quizForm' id='1'>
<ol>
<li>
<h3>What does CSS Stands For ?</h3>
<div>
<input type='radio' name='answer2' id='answer2' value='A' />
<label for='answer2A'>A) College Computer Studies</label>
</div>
<div>
<input type='radio' name='answer2' id='answer2' value='B' />
<label for='answer2B'>B) Cascading Style Sheet</label>
</div>
<div>
<input type='radio' name='answer2' id='answer2' value='C' />
<label for='answer2C'>C) Cascaded Style Sheet</label>
</div>
</li>
<input type = 'submit' name = 'submit1' value = 'Choose..'>
</form>";
}
if ($rid == 3){
echo "
<form action='answer.php?id=1' method='post' id='quizForm' id='1'>
<ol>
<li>
<h3>What does PHP Stands For ?</h3>
<div>
<input type='radio' name='answer3' id='answer3' value='A' />
<label for='answerOneA'>A) Hyper text markup language</label>
</div>
<div>
<input type='radio' name='answer3' id='answer3' value='B' />
<label for='answerOneB'>B) Hyper turn mark lingo</label>
</div>
<div>
<input type='radio' name='answer3' id='answer3' value='C' />
<label for='answerOneC'>C) Happy tissue mahatma life</label>
</div>
</li>
<input type = 'submit' name = 'submit1' value = 'Choose'>
</form>";
}
?>
</body>
</html>
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
<!doctype html>
<html>
<head>
<title>Question and Answer</title>
</head>
<body>
<?php
error_reporting(E_ALL ^ E_NOTICE);
$answer = array('A','B','C');
$answer1= $_POST['answerOne'];
$answer2= $_POST['answer2'];
if($answer1 == $answer[0]){
echo 'yes';
}
else if ($answer1 != $answer[0]){
echo 'no';
}
else{
}
if($answer2 == $answer[1]){
echo 'yes';
}
else if ($answer2 != $answer[1]){
echo 'no';
}
?>
</body>
</html>
Here is a cleaner way to do it. Inside is_correct function, it check if particular answer exist in $_POST, if so it returns if the answer is correct:
function is_correct()
{
$answer = array(
'answerOne' => 'A',
'answer2' => 'B',
'answer3' => 'C',
);
foreach ($answer as $k => $v) {
if (array_key_exists($k, $_POST)) {
return $v == $_POST[$k];
}
}
return false;
}
if (is_correct()) {
echo "yes";
} else {
echo "no";
}

How to create a list of html radio buttons from php array?

I have following code (it's piece of bigger code):
<?php
include_once 'init/init.funcs.php';
$x = $_SESSION['answering']['index'];
echo $_SESSION['answering']['questions'][$x-1];
$result4 = mysql_query('SELECT kysimus_id FROM katse_kysimused
where kysimus= "' .$_SESSION['answering']['questions'][$x] . '"');
$question_id = mysql_result($result4, 0);
$result5 = mysql_query('SELECT * from katse_valik_vastused
where kysimus_id="'. $question_id . '"');
if($result5 === FALSE) {
die(mysql_error());
}
while($row = mysql_fetch_assoc($result5)) {
$options[] = $row['vasuts'];
}
//foreach($options as $option=>$option_value) {
//echo $option_value;
$count=count($options);
?>
<html>
<br>
<form method="post" action="answering.php">
<input type="radio" name="1"><?php echo $options[0]?><br>
<input type="radio" name="2"><?php echo $options[1]?><br>
<input name= "submit" type="submit" value="Vasta">
</form>
</html>
Right now there are two fixed radio buttons. But I want it to have as many buttons, as many elements are in array "options" and each of them to have a value of one element written next to it. How could I do it?
Use a for loop for this: http://www.php.net/manual/en/control-structures.for.php
for ($i = 1; $i < count($options); $i++) {
?>
<input type="radio" name="<?php echo $i; ?>"><?php echo $options[$i]?><br>
<?php
}
Try like this:
<?php
while($row = mysql_fetch_assoc($result5)) {
$options[] = $row['vasuts'];
}
?>
<html>
<br>
<form method="post" action="answering.php">
<?php
foreach($options as $option=>$option_value) {
?>
<input type="radio" name="<?= $option; ?>"><?php echo $option_value?><br>
<?php }?>
<input name= "submit" type="submit" value="Vasta">
</form>
<html>
<br>
<form method="post" action="answering.php">
<?php
foreach ($options as $index=>$option) {
echo "<input type='radio' name='{$index}'>{$option}<br>";
}
?>
<input name= "submit" type="submit" value="Vasta">
</form>
</html>
Try using the below code.
<html>
<br>
<form method="post" action="answering.php">
<?php
foreach ($options as $key => $value) {
?>
<input type="radio" name="<?php echo $key; ?>"><?php echo $options[$key] ?><br>
<?php
}
?>
<input name= "submit" type="submit" value="Vasta">
</form>
</html>
you can do this using for each, like this:
<form method="post" action="answering.php">
<?php foreach ($options as $key => $value): ?>
<input type="radio" name="<?php echo $key; ?>" /><?php echo $value; ?><br />
<?php endforeach; ?>
<input name= "submit" type="submit" value="Vasta">
</form>

get array of checkbox values from post

Here is my code
<?php
if(isset($_POST['type'])){
if (is_array($_POST['type'])) {
echo "IS ARRAY!!!!!!!!";
}
else {
echo "IS NOT ARRAY!!!";
}
}
?>
and..
<div id="player" class="group">
<form action=<?php echo $_SERVER['SCRIPT_NAME']; ?> id="playerform" method="post">
<?php
for($j = 0; $j < sizeof($_SESSION['playercharacter']->defendAgainst); $j++) {
?>
<input type="checkbox" name="type[]" value=<?php echo $_SESSION['playercharacter']->
defendAgainst[$j]; ?> />
<?php
}
?>
</form>
</div>
Thing is....$_POST['type'] is just a single value rather than an array.. How do i get ALL checked values? Thanks for your time...
I hope it will help you
<form action="" method="post" >
<?php
for($i=0;$i<10;$i++){
echo '<input type="checkbox" name="type[]" value="'.$i.'">'.$i.'<br/>';
}
?>
<input type="submit" name="submit" >
</form>
if(isset($_POST['submit'])){
$arr=array();
foreach($_POST['type'] as $key=>$value)
{
$arr[$key]=$value;
}
var_dump($arr);
}

Form data not posting to uri

Can you tell me why when I click call my data is not posting to my action?
<body>
<?php
$login = '1236567';
$password = '10152930';
$officeNumber = array('0212177899','027899899','09111');
?>
<div id="wrapper">
<form method="POST" action="https://live.domain.co.nz/call.php?login=<?php echo $login; ?>&password=<?php echo $password; ?>&aparty=<?php echo $number; ?>phone&bparty=<?php echo $number;?>">
<?php
echo '<label for="officeNumbers">Office Number: </label>';
echo '<select name="officeNumbers">';
foreach($officeNumber as $number)
{
echo '<option value="'.$number.'">'.$number.'</option>';
}
echo '</select>';
?>
<label for="callTo">Call: </label><input type="text" id="callTo">
<input type="submit" value="Call">
</form>
</div>
</body>
</html>
Your text input:
<input type="text" id="callTo">
… has no name attribute, so it cannot be a successful control (and thus submit any data).
Give it a name attribute.

html form does not work inside fieldsets

I have a field set and inside it i have a form . it does not work . i mean to say . when i see the tags using firebug , the form tags will not be there at all..how do u i get over it.
this is how the code goes...its a php code..
<div id="dialog-form_surg_couns" title=" Surgical Counselling">
<?php
$surgCount = 0;
foreach($this->surgery as $surgery) {
$surgCount++;
$newId = str_replace(' ','',$surgery->getSurgeryname());
?>
<div class='fieldreq1Pct'>
<div class='fieldItemLabel'>
<label for=''><?php echo $surgery->getSurgeryname() ?></label>
</div>
<div class='fieldItemValue'>
<input type='checkbox' class='surg_couns_tests' id="<?php echo $newId ?>" name='surg_couns_tests' value="<?php echo $surgery->getSurgeryname() ?>" <?php echo (($showValue && strstr($visitRecord->getSurgcounstests(),$surgery->getSurgeryname())) ? 'checked' : "" ); ?> onClick="javascript:showBlock(this.id);">
</div>
</div>
<?php
if(($surgCount % 3) == 0)
{
?>
<div class='clear'></div>
<?php
}
}
?>
<div class='clear'></div>
<hr/>
<?php
foreach($this->surgery as $surgery) {
$newId = str_replace(' ','',$surgery->getSurgeryname());
$fieldCount = 0;
?>
<div id='<?php echo $newId ?>_block' style='display:none;' class='check_block'>
<form method='POST' action ='' id ='<?php echo $newId ?>_form'>
<table border='0' class='surg_table'>
<?php
foreach($this->surgeryTemplate as $surgerytemplate) {
if($surgery->getSurgeryid() == $surgerytemplate->getSurgeryid())
{
$fieldCount++;
$fieldName = 'field'.$fieldCount;
$fieldId = $surgerytemplate->getFieldid();
if($surgerytemplate->getRequired() == 'Y')
{
$required = 'required';
}
else
{
$required = '';
}
if($surgerytemplate->getType() == 'AN')
{
$validation = 'alpha';
}
else
{
$validation = '';
}
?>
<tr>
<td>
<?php echo $surgerytemplate->getFieldname(); ?>
</td>
<td>
<?php
if($surgerytemplate->getType() == 'B')
{
echo '<input type=\'radio\' name=\''.$fieldName.'\' value=\'Yes\'>Yes';
echo '<input type=\'radio\' name=\''.$fieldName.'\' value=\'No\'>No';
}
else
{
echo '<input type=\'text\' name=\''.$fieldName.'\' id=\''.$fieldName.'\' class=\''.$required.' '.$validation.'\' onblur="checkValid(this.id)"><div id=\''.$fieldName.'error\'></div>';
}
?>
</td>
</tr>
<?php
}
}
?>
</table>
<center><input type='button' name='submit' value='submit' onclick='javascript:submitSurgeryForm("<?php echo $newId ?>")'></center>
</form>
</div>
<?php
}
?>
</div>
You can't have a form tag inside another form. The following HTML is invalid:
<form>
<fieldset>
<form>
<input>
</form>
</fieldset>
</form>
The browser will silently ignore the second form, and instead will interpret your page as:
<form>
<fieldset>
<input>
</fieldset>
</form>

Categories