Iam a beginner in php and codeigniteran I have a problem concerning a survey vote form Iam creating in codeigniter. I have no idea how many answers the creater of a survey writes so I must code a dynamic page where the user sees as many radio buttons as answeres.
controller:
public function vote($frage_id){
if(!$this->ion_auth->logged_in()){
redirect('auth/login');
}
$antworten = $this->antwort_model->getbyFrageid($frage_id);
$frage = $this->fragen_model->get($frage_id);
foreach($antworten as $antwort){
$this->form_validation->set_rules('antworten',$antwort['inhalt']);
}
if($this->form_validation->run() === FALSE){
$data = array(
'frage' => $frage,
'antworten' => $antworten
);
$this->load->view('templates/header');
$this->load->view('antwort/vote', $data);
$this->load->view('templates/footer');
print_r($antworten);
}
else{
$antwort_id;
foreach($antworten as $antwort){
if($antwort['inhalt'] = $this->input->post('antworten')){
$antwort_id = $antwort['id'];
}
}
$user_id = $this->ion_auth->get_user_id();
$this->antwort_model->set($antwort_id,$user_id);
redirect('umfrage/display/'.$frage['umfrage_id']);
}
}
}
view;
<?php echo validation_errors(); ?>
<h1><?php echo $frage['inhalt'];?></h1>
<?php echo form_open('antwort/vote/'.$frage['id']); ?>
<?php foreach($antworten as $antwort){ ?>
<label for="antworten" ><?php echo $antwort['inhalt'];?></label>
<input type="radio" name="antworten" value = "<?php $antwort['inhalt'];?>"<?=set_radio('antworten',$antwort['inhalt'])?> /><br />
<?php } ?>
<input type="submit" name="submit" value="Submit" />
</form>
for some reason the form_validation() always returns false.
pls help
thx in advance
IF you look at your HTML Source on the browser to see what HTML is neing generated you should see you have to find a few more 'echo' commands.
<?php echo validation_errors(); ?>
<h1><?php echo $frage['inhalt'];?></h1>
<?php echo form_open('antwort/vote/'.$frage['id']); ?>
<?php foreach($antworten as $antwort){ ?>
<label for="<?php $antwort['inhalt'];?>" ><?php echo $antwort['inhalt'];?></label>
<input type="radio" name="<?php $antwort['inhalt'];?>" value = "<?php $antwort['inhalt'];?>" /><br />
<?php } ?>
<input type="submit" name="submit" value="Antwort abgeben" />
</form>
Needs a few more echo statements but its is better to use <?= than <?php echo ( as some folks forget the echo...)
<?= validation_errors(); ?>
<h1><?= $frage['inhalt'];?></h1>
<?= form_open('antwort/vote/'.$frage['id']); ?>
<?php foreach($antworten as $antwort){ ?>
<label for="<?= $antwort['inhalt'];?>" ><?= $antwort['inhalt'];?></label>
<input type="radio" name="<?= $antwort['inhalt'];?>" value = "<?= $antwort['inhalt'];?>" /><br />
<?php } ?>
<input type="submit" name="submit" value="Antwort abgeben" />
</form>
And we could add a little less confusion with curly braces in HTML by using https://www.php.net/manual/en/control-structures.alternative-syntax.php
<?= validation_errors(); ?>
<h1><?= $frage['inhalt'];?></h1>
<?= form_open('antwort/vote/'.$frage['id']); ?>
<?php foreach($antworten as $antwort): ?>
<label for="<?= $antwort['inhalt'];?>"><?= $antwort['inhalt'];?></label>
<input type="radio" name="<?= $antwort['inhalt'];?>" value = "<?= $antwort['inhalt'];?>"><br>
<?php endforeach; ?>
<input type="submit" name="submit" value="Antwort abgeben">
</form>
Related
I am trying to update a database record using a submit button, however the form that information is being taken from has itself been populated by another submit button.
The PHP is below:
if (isset($_POST['edit']))
{
$editUser = User::locate($_POST['userSelect']);
$setCompany = Company::findCompany();
$exCompanyId = $editUser->user_company_id;
$isAdmin = $editUser->user_admin;
$eUserFirstname = $editUser->user_firstname;
$eUserLastname = $editUser->user_lastname;
$eUserUsername = $editUser->user_username;
$eUserEmail = $editUser->user_email;
$eUserCompany = $editUser->user_company;
$adminArray = array("Yes","No");
if ($exCompanyId > NULL)
{
$exCompany = Company::locateId($exCompanyId);
$companyValue = $exCompany->company_name;
}
else
{
$companyValue = "";
}
if ($isAdmin > 0)
{
$userAdmin = $adminArray[0];
}
else
{
$userAdmin = $adminArray[1];
}
}
if (isset($_POST['confirm']))
{
$updateUserRecord = User::locate($eUserUsername);
$newCompanyValue = $_POST['setCompany'];
$isAdmin = $_POST['userAdmin'];
$newCompany = Company::locate($newCompanyValue);
$companyId = $newCompany->company_id;
$updateUserRecord->user_company_id = $companyId;
$updateUserRecord->user_admin = $isAdmin;
$editUser->updateUserAdmin();
$message = "You have successfully updated the user account";
echo "<script type='text/javascript'>alert('$message');</script>";
}
The HTML code is below:
<form action="" method="post">
<p>
<label>First Name:</label>
<label><?php echo $eUserFirstname ?></label>
</p>
<p>
<label>Last Name:</label>
<label><?php echo $eUserLastname ?></label>
</p>
<p>
<label>Username:</label>
<label><?php echo $eUserUsername ?></label>
</p>
<p>
<label>Email Address:</label>
<label><?php echo $eUserEmail ?></label>
</p>
<p>
<label>User Company:</label>
<label><?php echo $eUserCompany ?></label>
</p>
<p>
<label>Set Company:</label>
<select name="setCompany">
<option><?php echo $companyValue ?></option>
<?php
foreach ($setCompany as $srow)
{
?>
<option id="<?=
$srow->company_id
?>">
<?=
$srow->company_name
?>
</option>
<?php
}
?>
</select>
</p>
<p>
<label>Administrator:</label>
<select name="userAdmin">
<option><?php echo $userAdmin ?></option>
<?php
foreach ($adminArray as $arow)
{
?>
<option>
<?=
$arow
?>
</option>
<?php
}
?>
</select>
</p>
<input type="submit" name="cancel" value="Cancel">
<input type="submit" name="confirm" value="Confirm">
</br>
</form>
From my investigations so far the variables aren't transferring to the 2nd if statement, and I'm not sure how to make them available to it.
Store the first form's submission in hidden input fields alongside your echo statements. For example: <input type="hidden" name="field_name" value="<?php echo htmlspecialchars($field); ?>">
Or, alternatively, update the DB after first form posts.
iam trying to populate my dropbox from my database and then retrieve it in my php code...is this possible?
<form method="POST">
<select>
while ($fethc_items = mysql_fetch_assoc($items))
{
$item_id= $fethc_items['item_id'];
$itemname = $fethc_items['item_name'];
?>
<option value="<? echo $itemname;?>">Volvo</option>
<?}?>
<input type="submit" value="Submit" />
</form>
<?
if (isset($_POST['?']))
{
}
?>
Yes you can..
Try this code. I hope its will help you.
<form method="POST">
<select name="item">
<?php
while ($fethc_items = mysql_fetch_array($items))
{
$item_id= $fethc_items['item_id'];
$itemname = $fethc_items['item_name'];
?>
<option value="<?php echo $item_id;?>"><?php echo $itemname;?> </option>
<?php } ?>
</select>
<input type="submit" value="Submit" />
</form>
<?php
if (isset($_POST['item'])) {
}
?>
Thank You!
I am trying to create a form where I need to allow user to select different values (from select tag) against some labels.
I have two different array in php.
the standard array that contains the labels for select.
a set of values to be selected against the labels.
My problem is when I press the submit button, the form is submitted but $_POST does not show any value selected by the select tag. I want to get the selected values against the labels.
here is my code:
<?php
$data = array ('name', 'phone', 'address');
$values = array('a','2344','xyz');
?>
<html>
<head></head>
<body>
<form action="<?php $_SERVER["PHP_SELF"] ?>" method="post">
<?php for($i = 0; $i < count($data); $i++){ ?>
<label for='<?php $data[$i]?>'> <?php echo $data[$i]?></label>
<select name='<?php $data[$i]?>' id = '<?php $data[$i]?>'>
<?php foreach($values as $val){ ?>
<option value='<?php $val ?>'> <?php echo $val ?> </option>
<?php } ?>
</select>
<?php } ?>
<button type="submit" name="submit" value="submit">Submit</button>
<br>
</form>
</body>
</html>
<?php
if(isset($_POST['submit'])){
echo$_POST['name'];
}
?>
When I press the submit button the error I get is "Notice: Undefined index: name". I have extensively searched in the questions already posted about multiple select statements but none of the answers matched my criteria. Thanks for the help.
You are forgot to print variables
<html>
<head></head>
<body>
<form action="<?php $_SERVER["PHP_SELF"] ?>" method="post">
<?php for($i = 0; $i < count($data); $i++){ ?>
<label for='<?php echo $data[$i]?>'> <?php echo $data[$i]?></label>
<select name='<?php echo $data[$i]?>' id = '<?php echo $data[$i]?>'>
<?php foreach($values as $val){ ?>
<option value='<?php echo $val ?>'> <?php echo $val ?> </option>
<?php } ?>
</select>
<?php } ?>
<button type="submit" name="submit" value="submit">Submit</button>
<br>
</form>
</body>
</html>
<?php
if(isset($_POST['submit'])){
echo$_POST['name'];
}
?>
You have not written echo before each variable in selectbox:
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
<?php for($i = 0; $i < count($data); $i++) { ?>
<label for='<?php echo $data[$i]; ?>'> <?php echo $data[$i]?></label>
<select name='<?php echo $data[$i]; ?>' id = '<?php echo $data[$i]; ?>'>
<?php foreach($values as $val){ ?>
<option value='<?php echo $val; ?>'> <?php echo $val ?> </option>
<?php } ?>
</select>
<?php } ?>
<button type="submit" name="submit" value="submit">Submit</button>
</form>
I'm trying to check a checkbox based on its database value, i have tried the following but no joy, any suggestions?
I am using Kohana framework.
public static function defaultdistance(){
$result = DB::select('value')->from('mytable')->where('key', '=', 'default-distance')->execute();
$distancestores = $result->as_array();
foreach($distancestores as $distancestore)
{
echo 'Value: '.$distancestore['value'];
}
}
<label class="shortLabel"><input type="radio" name="distance" value="10" <?php if ($distancestore['value'] == '10') echo "checked='checked'"; ?> /> 10 <?php echo $dict->transl('distance_km'); ?></label>
<label> </label><label class="shortLabel"><input type="radio" name="distance" value="15" <?php if ($distancestore['value'] == '15') echo "checked='checked'"; ?> /> 15 <?php echo $dict->transl('distance_km'); ?></label>
May be you need like this
$distancestores = $result->as_array();
foreach($distancestores as $distancestore)
{
?>
<label class="shortLabel">
<input type="radio" name="distance" value="10"
<?php if ($distancestore['value'] == '10') echo "checked='checked'"; ?> />
10 <?php echo $dict->transl('distance_km'); ?>
</label>
<?php
}
try this i think you have to use ternary operator to resolve this issue.
<input type="radio" name="distance" value="10"
<?php $select = $distancestore['value']=='10'?'checked':'';
echo $select; ?>
10 <?php echo $dict->transl('distance_km'); ?>
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.