How to work this if condition in form and codeigniter - php

i am using this condition for check box value in codeigniter i want if $v_d value take go in 3rd condition ,and value $v_v then go in 2nd condition and if avail both value then go 1st condition.
print_r($this->input->post());
$id =$this->input->post('id');
$v_v=$this->input->post('d_v');
$v_d=$this->input->post('d_d');
if(isset($v_d) && isset($v_v)){
echo"hwqq";
}
elseif(isset($v_v)){
echo"hello1";
}
elseif(isset($v_d)){
echo"he2";
}
HTML CODE iS THIS
<form action="<?php base_url()?>index.php/index/home_email_update" method="POST">
<input type="hidden" name="id" value="<?php echo $id; ?>"/>
<h5><?php echo $msg;?></h5>
<?php if($f['subscription_discount_voucher']== 1){?>
<div class="email"><input type="checkbox" name="d_v" value="0" id="textfield"> Unsubscribe from Discount Voucher Emails.</div>
<?php }else{?>
<div class="email"><input type="checkbox" name="d_v" value="1" id="textfield"> Subscribe from Discount Voucher Emails.</div>
<?php }
if($f['subscribe_daily_deal']==1){ ?>
<div class="email"><input type="checkbox" name="d_d" value="0" id="textfield"> Unsubscribe from Daily Deal Emails. </div>
<?php } else{?>
<div class="email"><input type="checkbox" name="d_d" value="1" id="textfield"> Subscribe from Daily Deal Emails. </div>
<?php }?>
<div class="email_1"><button type="submit" class="submit_new">Change</button></div>
</form>

Try this,
$id =$this->input->post('id');
$v_v=$this->input->post('d_v');
$v_d=$this->input->post('d_d');
if(isset($v_d) && !isset($v_v)){
echo "hwqq";
}
else if(!isset($v_d) && isset($v_v)){
echo "hello1"
}
else if(isset($v_d) && isset($v_v)){
echo "he2";
}

Since $this->input->post() returns FALSE if there aren't any matches in the $_POST superglobal, isset check is not going to be an appropriate check here.
Instead you could try using empty:
if(!empty($v_d) && !empty($v_v)){
echo"hwqq";
}
elseif(!empty($v_v)){
echo"hello1";
}
elseif(!empty($v_d)){
echo"he2";
}
Please make sure to check Return values of empty for the list of things considered empty.

Related

How do i call an ajax file in laravel

I have an ajax file saved as ajax.php which contains the following code:
<?php
// Start the session
session_start();
$con=mysqli_connect("localhost","root","","quiz"); // change here to your data
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Check the number of all questions, if next_id is more than last question, back to first or whatever you want;
$response=mysqli_query($con,"select * from questions");
$number_of_all_questions = mysqli_num_rows($response);
if($_POST['next_id'] == 0){
// reset to default
$_SESSION["correct_score"] = 0;
$_SESSION["not_correct_score"] = 0;
}
if($number_of_all_questions <= $_POST['next_id']){
// Quiz finished, show results
echo"<div>
<h2>Results:</h2>
<p>Correct answers: {$_SESSION['correct_score']}</p>
<p>Wrong answers: {$_SESSION['not_correct_score']}</p>
</div>";
}else{
// query next question
$response=mysqli_query($con,"select * from questions WHERE id =(select min(id) from questions where id > {$_POST['next_id']})");
?>
<?php while($result=mysqli_fetch_array($response,MYSQLI_ASSOC)){ ?>
<div id="question_<?= $result['id'] ?>" class='question' data-next-question="<?= $_POST['next_id'] ?>"> <!--check the class for plurals if error occurs-->
<h2><?= $result['id'].".".$result['question_name'] ?></h2>
<div class='align'>
<input type="radio" value="1" id='radio1' name='1'>
<label id='ans1' for='radio1'><?= $result['answer1'] ?></label>
<br/>
<input type="radio" value="2" id='radio2' name='2'>
<label id='ans2' for='radio2'><?= $result['answer2'] ?></label>
<br/>
<input type="radio" value="3" id='radio3' name='3'>
<label id='ans3' for='radio3'><?= $result['answer3'] ?></label>
<br/>
<input type="radio" value="4" id='radio4' name='4'>
<label id='ans4' for='radio4'><?= $result['answer4'] ?></label>
</div>
<br/>
<?php /*<input type="button" data-next-question="<?= $_POST['next_id'] ?>" id='next' value='Next!' name='question' class='butt'/> */?>
</div>
<?php }?>
<?php }?>
<?php mysqli_close($con); ?>
the ajax file contains the code for fetching questions from my database "quiz". I did this using PHP and HTML and it worked fine. now I am shifting it to laravel. the problem is,only plain html is showing in view blade. the questions are not shown in the blade. how can i call this ajax file inside my view blade
you should move your php codes in your controller and access it by routes , however you should use eloquent ORM to fetch data from your database and just prepare variables to your blade views and show them.
here is a tutorial
laravel docs is another choice

How to make a 5 checkbox to be marked with values from databases

i want to make a form with 5 checkbox that will be retrieved from my table genres that contain(ID, name) of books (from database)-this works, and i want to mark old genres from my table book_genre, this contain(ID,ID_book, ID_genre) and then to make an update with new genres - this did not work.
I did this:
<div>
<input type="checkbox" name="ID_gen<? echo $id=$row_genre_new['ID']; ?>"
value="1" <? if($row_genre_old['ID_genre'] ) echo 'checked="checked"'; ?> />
<? echo $row_genre_new['name']; ?>
</div>
<!--
<div>
<input type="checkbox" name="ID_gen<? echo $id=$row_genre_old['ID']; ?>"
value="1" checked="checked" />
<? echo $row_genre_old['name']; ?>
</div>
-->
<? } ?>
You would loop through database results echoing a checkbox for each database entry with their id as the value and check whether it has been assigned in this case and if so echo checked

Show whether dynamic radio button is checked or not

<?php $result2 = db_getsub( array('campaigns_id' => $SYS_campaign) );
if (!is_null($result1)){
while ( $row = $result2->fetch_object() )
{
?>
<div class="item">
<div style="text-align: left;">
<div class="choicetitle">
<input type="radio" id="Radio1" checked="checked" title="" name="subtype" value="<?php echo $row->subid; ?>" /><?php echo $row->sub_name; ?></div>
<div class="choicedesc descrip">
<?php echo $row->gavdescrep; ?> </div>
<!-- <div class="choicelesmer">
Les mer
</div>-->
<div class="choicedesc1">
<?php echo $row->gavdescrep; ?> </div>
</div>
<span><?php
$value=$row->price;
if($value != 0)
{
echo $row->price; ?>,-
<?php }else { echo ""; } ?> </span>
<span class="bgcolor discprice">
<?php echo $row->pricediscount; ?>,-
</span>
</div>
<?php } } ?>
I want to show default 1st radio button checked now its showing last button checked as it is coming dynamically any help???
You are using checked="checked" for all your radio buttons in the loop. So, last assigned radio button retains the checked property. To solve the problem, Conditionally set checked="checked" only for the first looping
if(loopingFirstTime){
$checked = ' checked="checked" ';
}else{
$checked = ' ';
}
and then ....
<input type="radio" name="radio1" '.$checked.' />
Not tested this .... please check the syntax
Use checked=checked property to make a radio button checked by default:-
Blue
http://www.w3schools.com/jsref/prop_radio_defaultchecked.asp
And to handle that with jquery:-
jQuery("#radio_1").attr('checked', 'checked');
How to check a radio button with jQuery ?
Like the other guys have already said, you have only implemented one radio button.
You can try jsfiddle.net for example to try out you code and quickly see your changes.
Btw: I'm not quite sure what you're trying to do in you last row: Is that a leftover or those this belong to the wrapped-around code?

PHP check boxes and URL rewriting

Hope someone can help me with my code. i am using rewritten URL's and have this piece of code on a form. When the page first loads mypage.htm, the type_24 checkbox get's checked by default. If they check the type_12 box, i want the type_24 box to uncheck.
My problem is, if i check the type_12 box, the page refreshes and both the type_12 and type_24 boxes are checked.. which is not what i want. I think it's because i'm reloading my rewritten URL in my action because it works fine if i just have php file as the action.
Any ideas how i can fix my code so that it only has the type_12 checked when i check the type_24 box?
<form name="frmrefresh" id="frmrefresh" method="post" action="mypage.htm">
<input type="checkbox" name="type_12" id="type_12" <?php if(isset($_POST['type_12']) && $_POST['type_12']=="12"){?> checked="checked"<?php }?> value="12" onClick="uncheck24(this);" /> <label>12</label>
<input type="checkbox" name="type_24" id="type_24" <?php if(isset($_GET['id']) && $_GET['id']!=''){?>checked="checked"<?php }?><?php if(isset($_POST['month_24']) && $_POST['type_24']=="24"){?> checked="checked"<?php }?> value="24" onClick="uncheck12(this);"/> <label>24 Months</label>
<input type="hidden" name="id" value="<?php echo $_REQUEST['id'];?>" />
</form>
In the header i have the functions:
function uncheck12(obj)
{
if (obj.checked == true)
{
document.getElementById("type_12").checked = false;
document.frmrefresh.submit();
}
}
function uncheck24(obj)
{
if (obj.checked == true)
{
document.getElementById("type_24").checked = false;
document.frmrefresh.submit();
}
}
A number of remarks:
Consider using radio buttons instead of checkboxes
I think you mean isset($_POST['type_24']) instead of isset($_POST['month_24'])
As to your problem, I bet your rewrite rule is something like:
RewriteRule ^mypage.htm$ mypage.php?id=5
This means that when the form gets submitted via POST, PHP will still set the $_GET['id'] variable for you since it's in the query string. And since you check the '24' option whenever $_GET['id'] is set, the second checkbox will always get checked.
To fix this you could consider adding the check $_SERVER['REQUEST_METHOD'] == 'GET':
<form name="frmrefresh" id="frmrefresh" method="post" action="mypage.htm">
<input type="checkbox" name="type_12" id="type_12" <?php if(isset($_POST['type_12']) && $_POST['type_12']=="12"){?> checked="checked"<?php }?> value="12" onClick="uncheck24(this);" /> <label>12</label>
<input type="checkbox" name="type_24" id="type_24" <?php if(isset($_GET['id']) && $_GET['id']!='' && $_SERVER['REQUEST_METHOD']=='GET'){?>checked="checked"<?php }?><?php if(isset($_POST['type_24']) && $_POST['type_24']=="24"){?> checked="checked"<?php }?> value="24" onClick="uncheck12(this);"/> <label>24 Months</label>
<input type="hidden" name="id" value="<?php echo $_REQUEST['id'];?>" />
</form>

How to remember checkbox input in PHP Forms

For usability purposes I like to set up my form fields this way:
<?php
$username = $_POST['username'];
$message = $_POST['message'];
?>
<input type="text" name="username" value="<?php echo $username; ?>" />
<textarea name="message"><?php echo $message; ?></textarea>
This way if the user fails validation, the form input he entered previously will still be there and there would be no need to start from scratch.
My problem is I can't seem to keep check boxes selected with the option that the user had chosen before (when the page refreshes after validation fails). How to do this?
My first suggestion would be to use some client-side validation first. Maybe an AJAX call that performs the validation checks before continuing.
If that is not an option, then try this:
<input type="checkbox" name="subscribe" <?php echo (isset($_POST['subscribe'])?'checked="checked"':'') ?> />
So if subscribe is = 1, then it should select the box for you.
For Example, consider the following code for checkbox :-
<label for="course">Course:</label>
PHP<input type="checkbox" name="course[]" id="course" <?php if ((!empty($_POST["course"]) && in_array("PHP", $_POST["course"]))) {
echo "checked";
} ?> value="PHP" />
Then, this would remember the checkbox of "PHP" if it is checked, even if the validation for the page fails and so on for "n" number of checkboxes as shown below:-
<label for="course">Course:</label>
PHP<input type="checkbox" name="course[]" id="course" <?php if ((!empty($_POST["course"]) && in_array("PHP", $_POST["course"]))) {
echo "checked";
} ?> value="PHP" />
HTML<input type="checkbox" name="course[]" id="course" <?php if ((!empty($_POST["course"]) && in_array("HTML", $_POST["course"]))) {
echo "checked";
} ?> value="HTML" />
CSS<input type="checkbox" name="course[]" id="course" <?php if ((!empty($_POST["course"]) && in_array("CSS", $_POST["course"]))) {
echo "checked";
} ?> value="CSS" />
Javascript<input type="checkbox" name="course[]" id="course" <?php if ((!empty($_POST["course"]) && in_array("Javascript", $_POST["course"]))) {
echo "checked";
} ?> value="Javascript" />
And most importantly, do not forget to declare the "course" variable as an array at the start of the code as shown below :-
$course = array();
I have been battling how to create sticky check box (that is able to remember checked items any time you visit the page). Originally, I get my values from a database table. This means that my check box value is entered to a column on my db table.
I created the following code and it works just fine. I did not want to go through that whole css and deep coding, so...
CODE IN PHP
$arrival = ""; //focus here.. down
if($row['new_arrival']==1) /*new_arrival is the name of a column on my table that keeps the value of check box*/
{$arrival="checked";}// $arrival is a variable
else
{$arrival="";};
echo $arrival;
<b><label for ="checkbox">New Arrival</label></b>
<input type="checkbox" name ="$new_arrival" value="on" '.$arrival.' /> (Tick box if product is new) <BR><BR>
<input type="checkbox" name="somevar" value="1" <?php echo $somevar ? 'checked="checked"' : ''; ?>/>
Also, please consider sanitising your inputs, so instead of:
$somevar = $_POST['somevar'];
...it is better to use:
$somevar = htmlspecialchars($_POST['somevar']);
When the browser submits a form with a checked checkbox, it sends a variable with the name from the name attribute and a value from the value attribute. If the checkbox is not checked, the browser submits nothing for the checkbox. On the server side, you can handle this situation with array_key_exists(). For example:
<?php
$checkedText = array_key_exists('myCheckbox', $_POST) ? ' checked="checked"' : '';
?>
<input type="checkbox" name="myCheckbox" value="1"<?php echo $checkedText; ?> />
Using array_key_exist() avoids a potential array index undefined warning that would be issued if one tried to access $_POST['myCheckbox'] and it didn't exist.
You may add this to your form:
<input type="checkbox" name="mycheckbox" <?php echo isset($_POST['mycheckbox']) ? "checked='checked'" : "" ?> />
isset checks if a variable is set and is not null. So in this code, checked will be added to your checkbox only if the corresponding $_POST variable has a value..
My array has name="radioselection" and value="1", value="2", and value="3" respectively and is a radio button array... how to I check if the radio value is selected using this code
I tried:
<?php echo (isset($_POST['radioselection']) == '1'?'checked="checked"':'') ?> />

Categories