I have a rental inspection page I wrote for my work. All the selected
work except anything with more than one. Like bedroom1,bathroom1, fireplace1
works fine as does all the others selections. But when bedroom2,3,4,5,
bathroom2,3,4,5 etc. are selected they do not work at all. I have a work around, just click on the first one then the 2nd etc. uses it.
I have included the bedroom.items.php file at the bottom.
Thanks in advance for any help.
<script type="text/javascript">
function fillSecondCombo()
{
var combo1 = document.getElementById('Combobox1');
var combo2 = document.getElementById('Combobox2');
var selected = combo1.options[combo1.options.selectedIndex].value;
if (selected == "attic")
{
<?php include_once "item.lists/attic.item.php"; ?>
}
else
if (selected == "basement")
{
<?php include_once "item.lists/basement.item.php"; ?>
}
else
if (selected == "bathroom1")
{
<?php include_once "item.lists/bathroom.items.php"; ?>
}
else
if (selected == "bathroom2")
{
<?php include_once "item.lists/bathroom.items.php"; ?>
}
else
if (selected == "bathroom3")
{
<?php include_once "item.lists/bathroom.items.php"; ?>
}
else
if (selected == "bathroom4")
{
<?php include_once "item.lists/bathroom.items.php"; ?>
}
else
if (selected == "bathroom5")
{
<?php include_once "item.lists/bathroom.items.php"; ?>
}
else
if (selected == "bedroom1")
{
<?php include_once "item.lists/bedroom.items.php"; ?>
}
else
if (selected == "bedroom2")
{
<?php include_once "item.lists/bedroom.items.php"; ?>
}
else
if (selected == "bedroom3")
{
<?php include_once "item.lists/bedroom.items.php"; ?>
}
else
if (selected == "bedroom4")
{
<?php include_once "item.lists/bedroom.items.php"; ?>
}
else
if (selected == "bedroom5")
{
<?php include_once "item.lists/bedroom.items.php"; ?>
}
else
if (selected == "cooling")
{
<?php include_once "item.lists/cooling.item.php"; ?>
}
else
if (selected == "diningroom")
{
<?php include_once "item.lists/diningroom.item.php"; ?>
}
else
if (selected == "electric")
{
<?php include_once "item.lists/electric.item.php"; ?>
}
else
if (selected == "water")
{
<?php include_once "item.lists/water.item.php"; ?>
}
else
if (selected == "gas")
{
<?php include_once "item.lists/gas.item.php"; ?>
}
else
if (selected == "exterior")
{
<?php include_once "item.lists/exterior.item.php"; ?>
}
else
if (selected == "foundation")
{
<?php include_once "item.lists/foundation.item.php"; ?>
}
else
if (selected == "garage")
{
<?php include_once "item.lists/garage.item.php"; ?>
}
else
if (selected == "heating")
{
<?php include_once "item.lists/heating.item.php"; ?>
}
else
if (selected == "interior")
{
<?php include_once "item.lists/interior.item.php"; ?>
}
else
if (selected == "kitchen")
{
<?php include_once "item.lists/kitchen.item.php"; ?>
}
else
if (selected == "laundry")
{
<?php include_once "item.lists/laundry.item.php"; ?>
}
else
if (selected == "roof")
{
<?php include_once "item.lists/roof.item.php"; ?>
}
else
if (selected == "waterhtr")
{
<?php include_once "item.lists/waterhtr.item.php"; ?>
}
else
if (selected == "livingroom")
{
<?php include_once "item.lists/livingroom.item.php"; ?>
}
else
if (selected == "fireplace1")
{
<?php include_once "item.lists/fireplace.items.php"; ?>
}
else
if (selected == "fireplace2")
{
<?php include_once "item.lists/fireplace.items.php"; ?>
}
else
if (selected == "fireplace3")
{
<?php include_once "item.lists/fireplace.items.php"; ?>
}
else
if (selected == "fireplace4")
{
<?php include_once "item.lists/fireplace.items.php"; ?>
}
else
if (selected == "fireplace5")
{
<?php include_once "item.lists/fireplace.items.php"; ?>
}
else
if (selected == "fireplace10")
{
<?php include_once "item.lists/fireplace.items.php"; ?>
}
else
if (selected == "poolspa")
{
<?php include_once "item.lists/poolspa.item.php"; ?>
}
else
if (selected == "other")
{
<?php include_once "item.lists/other.item.php"; ?>
}
else
{
combo2.options.length = 0;
}
}
</script>
</head>
<body>
<div id="space"><br></div>
<div id="container">
<button id="AdvancedButton1" type="button" style="position:absolute;left:19px;top:119px;width:137px;height:44px;z-index:12;" onclick="window.location='main.php'" name="Your Account" value="">
<div style="text-align:center"><span style="color:#FFFFFF;font-family:Arial;font-size:16px"><b>Main Menu</b></span></div>
</button>
<button id="AdvancedButton2" type="button" style="position:absolute;left:176px;top:119px;width:124px;height:44px;z-index:13;" onclick="window.location='logout.php'" name="Logout" value="">
<div style="text-align:center"><span style="color:#FFFFFF;font-family:Arial;font-size:16px"><b>Logout</b></span></div>
</button>
<div id="wb_Form1" style="position:absolute;left:20px;top:209px;width:302px;height:574px;z-index:14;">
<form name="Form1" method="post" action="make.all.php" enctype="multipart/form-data" id="Form1" onsubmit="return ValidateForm1(this)">
<textarea name="a4" id="TextArea1" style="position:absolute;left:1px;top:275px;width:279px;height:143px;z-index:0;" rows="7" cols="32" tabindex="4"></textarea>
<div id="wb_Text5" style="position:absolute;left:4px;top:249px;width:152px;height:16px;z-index:1;text-align:left;">
<span style="color:#000000;font-family:Arial;font-size:13px;">Notes: 300 Chars Max.</span></div>
<div id="wb_Text4" style="position:absolute;left:0px;top:164px;width:170px;height:16px;z-index:2;text-align:left;">
<span style="color:#000000;font-family:Arial;font-size:13px;">Passed-Needs Repare-N/A:</span></div>
<select name="a2" size="1" id="Combobox2" style="position:absolute;left:0px;top:109px;width:280px;height:43px;z-index:3;" tabindex="2">
</select>
<div id="wb_Text3" style="position:absolute;left:4px;top:85px;width:140px;height:16px;z-index:4;text-align:left;">
<span style="color:#000000;font-family:Arial;font-size:13px;">Item Being Inspected:</span></div>
<select name="a1" size="1" id="Combobox1" onchange="fillSecondCombo();return false;" style="position:absolute;left:1px;top:32px;width:279px;height:43px;z-index:5;" tabindex="1">
<option value="<?php include "$dirPath/all.areas.php"; ?>"></option>
</select>
<div id="wb_Text2" style="position:absolute;left:4px;top:0px;width:142px;height:16px;z-index:6;text-align:left;">
<span style="color:#000000;font-family:Arial;font-size:13px;">Area Being Inspected:</span></div>
<div id="wb_Text1" style="position:absolute;left:4px;top:436px;width:59px;height:16px;z-index:7;text-align:left;">
<span style="color:#000000;font-family:Arial;font-size:13px;">Photo:</span></div>
<input type="file" id="FileUpload1" style="position:absolute;left:2px;top:452px;width:279px;height:41px;line-height:41px;z-index:8;" name="userfile[]" tabindex="5">
<input type="submit" id="Button1" name="" value="Submit" style="position:absolute;left:44px;top:513px;width:102px;height:30px;z-index:9;" tabindex="6">
<select name="a3" size="1" id="Combobox3" style="position:absolute;left:0px;top:188px;width:280px;height:43px;z-index:10;" tabindex="3">
<option selected value="Passed">Passed</option>
<option value="Needs Repair">Needs Repair</option>
<option value="N/A">N/A</option>
</select>
</form>
</div>
<div id="wb_TextArt1" style="position:absolute;left:18px;top:8px;width:285px;height:100px;z-index:15;">
<img src="images/img0018.png" id="TextArt1" alt="Enter Inspection Data" title="Enter Inspection Data" style="width:285px;height:100px;"></div>
</div>
</body>
</html>
bedroom.items.php
combo2.options.length = 9;
combo2.options[0] = new Option("Electrical Wall Plugs", "Electrical Wall Plugs");
combo2.options[1] = new Option("Walls and Ceiling", "Walls and Ceiling");
combo2.options[2] = new Option("Floor Covering", "Floor Covering");
combo2.options[3] = new Option("Windows (curtains, blinds, etc...)", "Windows (curtains, blinds, etc...)");
combo2.options[4] = new Option("Doors", "Doors");
combo2.options[5] = new Option("Lighting/Light Fixtures", "Light Fixtures");
combo2.options[6] = new Option("Closet(s)", "Closet(s)");
combo2.options[7] = new Option("Smoke Detector(s)", "Smoke Detector(s)");
combo2.options[8] = new Option("Heating/Cooling", "Heating/Cooling");
Related
I have to learn this from Youtube, PHP quiz using AJAX. I want to display question number navigation on the sidebar when I click any question number that question should display And Suppose I answered question no 1 and click on next then the background color of question number navigation should be green. The problem is when I click on question number navigation I get that question but when I select the option and click on the next button it previous question displays where it was And don't worry about SQL injection I will use a prepared statement, this is only a rough script for learning purposes. I think responseText is not working.
Please see this what I want to do Image
Question.php page to where I display question
<div class="col-12 question-Number question_navigation" >
<?php
$category=$_SESSION['exam_category'];
$sql = "SELECT * FROM questions WHERE category=? ORDER BY question_no=? ASC";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param("ss",$category,$question_no);
$stmt->execute();
$res = $stmt->get_result();
while($row=mysqli_fetch_array($res)){
$ROW= $row['question_no'];
for($i=$ROW; $i<=$ROW; $i++){
?>
<p type='button' id="quest-number" class='next_QUESTION' class="display" data-id="<?php echo $i ?>" ><?php echo $i ?></p>
<?php
}
}
?>
</div>
<!--------------------------------------------------->
<script type="text/javascript">
function load_total_que(){
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("total_que").innerHTML=xmlhttp.responseText;
}
};
xmlhttp.open("GET", "ajax/load_total_que.php", true);
xmlhttp.send(null);
}
</script>
<!--------------------------------------------------->
<script type="text/javascript">
var questionno="1";
load_questions(questionno);
function load_questions(questionno){
document.getElementById("current_que").innerHTML=questionno;
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
if(xmlhttp.responseText == 1){
window.location="result.php";
}else{
document.getElementById("load_questions").innerHTML=xmlhttp.responseText;
load_total_que();
}
}
};
xmlhttp.open("GET", "ajax/load_questions.php?questionno="+ questionno,true);
xmlhttp.send(null);
}
</script>
<!--------------------------------------------------->
<script type="text/javascript">
function load_previous(){
if(questionno=="1"){
load_questions(questionno);
}else{
questionno=eval(questionno)-1;
load_questions(questionno);
}
}
function load_next(){
questionno=eval(questionno)+1;
load_questions(questionno);
}
$(document).on('click', '.next_QUESTION', function(){
var questionno = $(this).data("id");
load_questions(questionno);
});
</script>
<!------------------------SAVE----ANSWER---------------------->
<script>
function radioclick(radiovalue, questionno){
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
}
};
xmlhttp.open("GET", "ajax/save_answer_in_session.php?questionno="+ questionno + "&value1=" + radiovalue, true);
xmlhttp.send(null);
}
</script>
This is the Question_load.php page
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
?>
<?php
session_start();
include_once("../../configure/config.php");
$question_no="";
$question="";
$opt1="";
$opt2="";
$opt3="";
$opt4="";
$answer="";
$count=0;
$ans="";
$queno=$_GET["questionno"];
if(isset($_SESSION["answer"][$queno]))
{
$ans=$_SESSION["answer"][$queno];
}
$res=mysqli_query($mysqli,"select * from questions where category='$_SESSION[exam_category]' AND question_no=$_GET[questionno]");
$count=mysqli_num_rows($res);
if($count==0)
{
echo "over";
}
else
{
while($row=mysqli_fetch_array($res))
{
$question_no=$row["question_no"];
$question=$row["question"];
$opt1=$row["opt1"];
$opt2=$row["opt2"];
$opt3=$row["opt3"];
$opt4=$row["opt4"];
}
?>
<br>
<table>
<h2><?php echo " ( ". $question_no ." ) ". $question; ?></h2>
<table>
<tr>
<input type="radio" name="r1" id="r1" value="<?php echo $opt1; ?>" onclick="radioclick(this.value,<?php echo $question_no; ?>)"
<?php
if($ans==$opt1){
echo "checked";
}
?>>
<?php
if(strpos($opt1,'image/')!=false)
{
?>
<img src="admin/<?php echo $opt1?>" height="30" width="30">
<?php
}
else
{
echo $opt1;
}
?>
</tr>
<br>
<input type="radio" name="r1" id="r1" value="<?php echo $opt2; ?>" onclick="radioclick(this.value,<?php echo $question_no; ?>)"
<?php
if($ans==$opt2){
echo "checked";
}
?>>
<?php
if(strpos($opt2,'image/')!=false)
{
?>
<img src="admin/<?php echo $opt2?>" height="30" width="30">
<?php
}
else
{
echo $opt2;
}
?>
</tr>
<br>
<input type="radio" name="r1" id="r1" value="<?php echo $opt3; ?>" onclick="radioclick(this.value,<?php echo $question_no; ?>)"
<?php
if($ans==$opt3){
echo "checked";
}
?>>
<?php
if(strpos($opt3,'image/')!=false)
{
?>
<img src="admin/<?php echo $opt3?>" height="30" width="30">
<?php
}
else
{
echo $opt3;
}
?>
</tr>
<br>
<input type="radio" name="r1" id="r1" value="<?php echo $opt4; ?>" onclick="radioclick(this.value,<?php echo $question_no; ?>)"
<?php
if($ans==$opt4){
echo "checked";
}
?>>
<?php
if(strpos($opt4,'image/')!=false)
{
?>
<img src="admin/<?php echo $opt4?>" height="30" width="30">
<?php
}
else
{
echo $opt4;
}
?>
</tr>
<br>
</table>
<?php
}
?>
The problem is with the response from php file echo "over"; is a simple string as output to ajax so if(xmlhttp.responseText=="over") will not work there.
Solution 1 :-
Use === so if(xmlhttp.responseText==='over') might work.
Solution 2 :-
echo out integer as some success code to ajax.
for eg:-
if($count==0)
{
echo "1";
}
then if(xmlhttp.responseText == 1)
Solution 3:-
Send Json encoded response from php and parse it at ajax side.
if($count==0)
{
$res = array("status" => "over");
echo json_encode($res);
}
then
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var res = JSON.parse(xmlhttp.responseText);
if (res.status == "over"){
window.location="final.php";
}
else{
document.getElementById("load_questions").innerHTML=xmlhttp.responseText;
load_total_que();
}
}
For any queries comment down.
Im still a newbie learning things day by day. I have posted my question at the OpenCart forum website looking for help and now here in StackOverflow. I need your help guys.
Hi there, Im a newbie getting used to Opencart environment. I need help with product bands as I have attached in picture below.
I need the coming_soon option to overwrite the out_of_stock option settings. Its not working correctly as Im confused with the logic that has been made. Out_of_stock option works when there is only "0" quantity products. When we have a new product that is Coming_soon we would make that product quantity "0" as well but something is wrong here and the Coming_soon is not overwriting the Out_of_stock setting. I need some help here.
catalog/controller/product/category.php
/*OOS Band start*/
$optionsQty = 0;
$oosQty = 0;
foreach ($this->model_catalog_product->getProductOptions($result['product_id']) as $option) {
if ($option['type'] == 'select' || $option['type'] == 'radio' || $option['type'] == 'checkbox' || $option['type'] == 'image') {
foreach ($option['option_value'] as $option_value) {
$optionsQty++;
if ($option_value['quantity'] <=0) $oosQty++;
}
}
}
if ($optionsQty == $oosQty && $optionsQty > 0 ) {
$band_oos = true;
}
else $band_oos = false; /*OOS Band end*/
catalog/view/product/category.tpl
<div class="for_infine">
<?php if ($product['band_oos']) { ?>
<span class="band-oos"> </span>
<?php } ?>
<?php if ($product['band_free_shipping'] == 'Yes') { ?>
<span class="band-free-shipping"> </span>
<?php } ?>
<?php if ($product['band_new'] == 'Yes') { ?>
<span class="band-new"> </span>
<?php } ?>
<?php if ($product['band_coming_soon'] == 'Yes') { ?>
<span class="band-coming-soon"> </span>
<?php } ?>
<br />
catalog/controller/product/product.php
$this->data['options'] = array();
$optionsQty = 0;
$oosQty = 0;
foreach ($this->model_catalog_product->getProductOptions($this->request->get['product_id']) as $option) {
if ($option['type'] == 'select' || $option['type'] == 'radio' || $option['type'] == 'checkbox' || $option['type'] == 'image') {
$option_value_data = array();
foreach ($option['option_value'] as $option_value) {
$optionsQty++;
if ($option_value['quantity'] <=0) $oosQty++;
if ((float)$product_info['special']) {
$price_original = $product_info['special'];
}else {
$price_original = $product_info['price'];
}
catalog/view/product/product.tpl
<div class="left">
<div class="heading" style="line-height: 27px; font-size: 25px;"><?php echo $heading_title; ?></div>
<?php if ($thumb) { ?>
<div class="image">
<?php if ($band_oos ) { ?>
<div class="oos_tab_large top-right-cnr"></div>
<?php } ?>
<?php if ($band_free_shipping == 'Yes') { ?>
<div class="free_shipping_tab_large top-right-cnr"></div>
<?php } ?>
<?php if ($band_new == 'Yes') { ?>
<div class="new_tab_large top-right-cnr"></div>
<?php } ?>
<?php if ($band_coming_soon == 'Yes') { ?>
<div class="coming_soon_tab_large top-right-cnr"></div>
<?php } ?>
Opencart out of stock warning can be disabled in settings:
Settings -> Your Store -> Edit -> Options -> Show Out Of Stock Warning
If you need both Stock Warning and Bands to work together you need to rethink new products adding with 0 quantity.
I have a problem
I hope you understand I write English badly:
I created a search field that contains these two files
index.php
<?php
include('db.php');
$keySesso = $_GET['sesso'];
$keyEta = $_GET['eta'];
$keyRegione = $_GET['regione'];
$keyFoto = $_GET['foto'];
//sesso
if(isset($keySesso)){
if ($keySesso == 2) {
$FemminaE="selected";
}else if ($keySesso == 1){
$MaschioE="selected";
}else if ($keySesso == 26){
$Gay="selected";
}else if ($keySesso == 27){
$Lesbica="selected";
}else if ($keySesso == 29){
$FemminaB="selected";
}else if ($keySesso == 28){
$MaschioB="selected";
}else{
$sessoN="";
}
}
//for total count data
$countSql = "SELECT COUNT(last_activity) FROM _core_members INNER JOIN _core_pfields_content ON _core_members.member_id = _core_pfields_content.member_id $whereSQL $keyRegione $CondizioneEta $CondizioneFoto ";
$tot_result = mysqli_query($conn, $countSql);
$row = mysqli_fetch_row($tot_result);
$total_records = $row[0];
$total_pages = ceil($total_records / $limit);
//for first time load data
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * $limit;
$sql = "SELECT * FROM _core_members INNER JOIN _core_pfields_content ON _core_members.member_id = _core_pfields_content.member_id $whereSQL $keyRegione $CondizioneEta $CondizioneFoto ORDER BY last_activity DESC LIMIT $start_from, $limit";
$rs_result = mysqli_query($conn, $sql);
?>
<!DOCTYPE html>
<head>
</head>
<body >
<div class="BloccoBaseBO">
<div class="container" id="BOcontent">
<div class="tab-content">
<div role="tabpanel" class="tab-pane active" id="tab1">
<form method='GET' id='FormChattaCon' class='FormBase' action=''>
<select id="SelectedSesso" name="sesso">
<option value="" <?php echo $sessoN; ?>>Tutti i generi</option>
<option value="2" <?php echo $FemminaE; ?>>femmine (Etero)</option>
<option value="1" <?php echo $MaschioE; ?>>maschi (Etero)</option>
<option value="26" <?php echo $Gay; ?>>Gay</option>
<option value="27" <?php echo $Lesbica; ?>>Lesbica</option>
<option value="29" <?php echo $FemminaB; ?>>Femmina (Bisex)</option>
<option value="28" <?php echo $MaschioB; ?>>Maschio (Bisex)</option>
</select>
<div class='ConFoto'>Con Foto: <input id="checkBox" name="foto" type="checkbox" onclick="javascript: submitform()" <?php echo $check;?>/> </div>
</form>
<table class="table table-bordered table-striped">
<tbody id="target-content">
<?php
while ($row = mysqli_fetch_assoc($rs_result)) {
echo "<div class='bloccoUserOnlineBO'></div><li class='NomeBloccoB'>$MemberName</li>";
};
?>
</tbody>
</table>
<nav><ul class="pagination">
<?php if(!empty($total_pages)):for($i=0; $i<=$total_pages; $i++):
if($i == 0):?>
<li class='active' id="<?php echo $i;?>"><a href='pagination.php?page=<?php echo $i;?>'><?php echo $i;?></a></li>
<?php else:?>
<li id="<?php echo $i;?>"><a href='pagination.php?page=<?php echo $i;?>'><?php echo $i;?></a></li>
<?php endif;?>
<?php endfor;endif;?>
</ul>
</nav>
</div>
</div>
</div>
<script type="text/javascript">
$(document).ready(function(){
$('.pagination').pagination({
items: <?php echo $total_records;?>,
itemsOnPage: <?php echo $limit;?>,
cssStyle: 'light-theme',
currentPage : 0,
onPageClick : function(pageNumber) {
jQuery("#target-content").html('loading...');
jQuery("#target-content").load("pagination.php?page=" + pageNumber);
}
});
});
</script>
<!-- tab -->
<script src="http://netdna.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="js/jquery.scrolling-tabs.js"></script>
<script>
$('.nav-tabs').scrollingTabs();
</script>
<!-- FlexSlider -->
<script src="http://www..it/info-user-global/js/jquery.flexslider-min.js"></script>
<script src="http://www..it/info-user-global/js/main.js"></script>
the second where it is recalled from the pages
pagination.php
<?php
include('db.php');
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * $limit;
$sql = "SELECT * FROM ILFREE_core_members INNER JOIN ILFREE_core_pfields_content ON ILFREE_core_members.member_id = ILFREE_core_pfields_content.member_id $whereSQL $keyRegione $CondizioneEta $CondizioneFoto ORDER BY last_activity DESC LIMIT $start_from, $limit";
$rs_result = mysqli_query($conn, $sql);
?>
<?php
while ($row = mysqli_fetch_assoc($rs_result)) {
echo "<div class='bloccoUserOnlineBO'></div><li class='NomeBloccoB'>$MemberName</li>";
};
?>
The problem is this
When I compile the form, the result appears only on the index.php page
When I push the buttons on the bottom pages linked to the pagination.php page, I reset the form
enter image description here
I can not fix it because the form data will stop at the index page but when I call the pagination.php file through this:
<script type="text/javascript">
$(document).ready(function(){
$('.pagination').pagination({
items: <?php echo $total_records;?>,
itemsOnPage: <?php echo $limit;?>,
cssStyle: 'light-theme',
currentPage : 0,
onPageClick : function(pageNumber) {
jQuery("#target-content").html('loading...');
jQuery("#target-content").load("pagination.php?page=" + pageNumber);
}
});
});
</script>
I reset the form
Please help me
I hope I was clear
I also leave you an email, I am willing to pay for help
IlfreeIF#gmail.com
This is exactly what AJAX is for (AJAX can do the same .load() function can, but with more configurability):
http://www.seguetech.com/ajax-technology/
jQuery Ajax POST example with PHP
I am using Laravel framework and some jquery code.
The problem is that, when i submit the data, it is successfully saved in session. when i get back to previous page, My select box shows the selected value but select-box is disabled. i am totally confused. what shall i do here?
Here is my HTML code:-
<div class="one-row">
<?php foreach ($services as $key => $allservices){
if($key <= 3){
if(!empty($data['services'])){
if(in_array($allservices['id'],$data['services'])) {
$checked = "checked";
} else {
$checked = "";
}
} else {
$checked = "";
}
?>
<div class="div_img_part-2">
<span class="img_part_class-2">
<img src="{{
asset('images/ServiceImages/'. $allservices['image'])}}"></span>
<span class="text_part_class-2">
<span class="check-box">
<input type="checkbox" name="services[]" value="
<?php echo $allservices['id']; ?>" <?= $checked; ?>><?php echo $allservices['name']; ?></span>
</span>
<select name="services[<?php echo $allservices['name'];?>]"class="selectpicker">
<option value="">Select Your Sevice</option>
<option value="Salon" <?php if(!empty($data['services'][$allservices['name']])){ if($data['services'][$allservices['name']] == "Salon") {?> selected
<?php } } ?> >Salon</option>
<option value="Mobile beautician" <?php if(!empty($data['services'][$allservices['name']])){ if($data['services'][$allservices['name']] == "Mobile beautician") {?> selected
<?php } }?> >Mobile beautician</option>
<option value="Both" <?php if(!empty($data['services'][$allservices['name']])){ if($data['services'][$allservices['name']] == "Both") {?> selected
<?php } } ?>>Both</option>
</select>
</div>
<?php } } ?>
</div>
And my Jquery Code looks:-
<script>
$(document).ready(function () {
$(".div_img_part-2 .selectpicker").attr("disabled",true);
$('.selectpicker').selectpicker('refresh');
$(".check-box input[type='checkbox']").on("change", function () {
if ($(this).prop("checked") == true) {
$(this).parents(".div_img_part-2").find(".selectpicker").removeAttr("disabled");
$('.selectpicker').selectpicker('refresh');
}else{
$(this).parents(".div_img_part-2").find(".selectpicker").attr("disabled",true);
$('.selectpicker').selectpicker('refresh');
}
});
});
</script>
Firstly Select
After Select(Edit)
Note:- when user selects the checkbox, the selectbox will be active at first time of registration. All my data are saved in session in Laravel please help me :(
You need to run this once the page is loaded:
$(".check-box input[type='checkbox']").trigger("change");
So the checkbox values will be recognised by the page.
The code (below) works fine when the user chooses a dropdown option, but the first option is populated by php based on previous pages. How can I get the onchange event for the select to happen when the page loads, picking up the php added value and acting on it?
<?php
$value = $_GET["value"];
if (!$value){
$value = 'Y';
}
?>
<html>
<head>
<style>
.inv {
display: none;
}
</style>
<script>
function jb(){
document.getElementById("target").value = "Y";
document.getElementById("target").onchange();
}
window.onload = jb;
</script>
</head>
<body>
See more? 
<select name='see_more' id='target'>
<option value='<?php echo $value; ?>'><?php echo $value; ?></option>
<option value='Y'>Y</option>
<option value='N'>N</option>
</select>
<div id="Y" class='inv'>
Additional content goes here
</div>
<script>
document
.getElementById('target')
.addEventListener('change', function () {
'use strict';
var vis = document.querySelector('.vis'),
target = document.getElementById(this.value);
if (vis !== null) {
vis.className = 'inv';
}
if (target !== null ) {
target.className = 'vis';
}
});
</script>
</body>
</html>
I managed to find a solution using php:
<?php
$value = $_GET["value"];
if (!$value){
$value = 'Y';
}
?>
<html>
<head>
<style>
.inv {
display: none;
}
</style>
</head>
<body>
See more? 
<select name='see_more' id='target'>
<option value='<?php echo $value; ?>'><?php echo $value; ?></option>
<option value='Y'>Y</option>
<option value='N'>N</option>
</select>
<?php
if ($value == 'Y'){
echo "<!--";
}
?>
<div id="Y" class='inv'>
<?php
if ($value == 'Y'){
echo "-->";
}
?>
<?php
if ($value == 'N'){
echo "<!--";
}
?>
<div id="Y" class='vis'>
<?php
if ($value == 'N'){
echo "-->";
}
?>
<br>
Additional content goes here
</div>
<script>
document
.getElementById('target')
.addEventListener('change', function () {
'use strict';
var vis = document.querySelector('.vis'),
target = document.getElementById(this.value);
if (vis !== null) {
vis.className = 'inv';
}
if (target !== null ) {
target.className = 'vis';
}
});
</script>
</body>
</html>
If you are using jQuery, you could just do $('#target').change();. So:
$(document).ready(function () {
$('#target').change();
});