Get PHP Last Loop Counter and Apply to jQuery Initial Counter Value - php

I am new to PHP & jQuery and I was constructing a repeater field for my Wordpress site but I will skip how I build it. Right now, I am trying to figure out an answer on how to get last loop counter in PHP foreach loop and apply that last counter number as initial counter value for jQuery variable. I'm not sure if this is possible because what I am trying to do is using the server-side data for client-side processing.
That's all for the intro, now here is my example PHP code:-
$x = -1;
foreach ($something as $some){
$x++; // Let say $some has total of 3, so $x will provide counter such as 0, 1 & 2
}
And I am trying to apply the last PHP counter value as jQuery initial counter value so that it will continue the count:-
var x = *the last php counter which is 2*;
var max = 20;
if(x < max){
x++;
// The 'x' will continue the count as 3, 4, 5,...
}
I've been trying many methods before this and it was futile. Right now I think the thing that I need to figure out for first is how to retrieve the PHP loop counter into outside the loop, I guess... But while I am figuring it out, I would really be thankful to you guys if you guys able to help me as I am new to this stuff.
EDIT:-
It seems that using '<?php echo $x; ?>'; for displaying the value in my jQuery makes my jQuery execution does not work at all.
Here's the full HTML-PHP code and jQuery code with '<?php echo $x; ?>'; inserted:-
HTML-PHP:-
<?php
$user_edu = unserialize(base64_decode($current_user->education));
$x = -1;
foreach(rsort($user_edu,SORT_NUMERIC) as $edu);
foreach ($user_edu as $edu){
$x++;
echo "
<div>
<div class='col-sm-6 col-sm-offset-3 col-xs-12 field-label-size' id='p-custom-color'>
School Name
</div>
<div class='col-sm-6 col-sm-offset-3 col-xs-12 form-group-2 p-custom'>
<input type='text' name='education[" . $x . "][school]' class='form-control field-custom' placeholder='Your School Name' value='" . $edu['school'] . "'/>
</div>
<div class='col-sm-6 col-sm-offset-3 col-xs-12 field-label-size' id='p-custom-color'>
Concentration
</div>
<div class='col-sm-6 col-sm-offset-3 col-xs-12 form-group-2 p-custom'>
<input type='text' name='education[" . $x . "][concentration]' class='form-control field-custom' placeholder='Your Course/Program Name' value='" . $edu['concentration'] . "'/>
</div>
<div class='col-sm-6 col-sm-offset-3 col-xs-12 field-label-size' id='p-custom-color'>
Period
</div>
<div class='col-sm-6 col-sm-offset-3 col-xs-12 field-label-size form-group-2 p-custom'>
<div class='field-spacing'>
<div class='col-sm-6 col-xs-6' id='col-padding-left'>
<select name='education[" . $x . "][start_year]' class='form-control field-custom'>
<option value='' disabled>Start Year</option>
<option value='" . $edu['start_year'] . "' selected>" . $edu['start_year'] . "</option>
<option value='2016'>2016</option>
<option value='2015'>2015</option>
<option value='2014'>2014</option>
<option value='2013'>2013</option>
<option value='2012'>2012</option>
<option value='2011'>2011</option>
<option value='2010'>2010</option>
<option value='2009'>2009</option>
<option value='2008'>2008</option>
<option value='2007'>2007</option>
<option value='2006'>2006</option>
<option value='2005'>2005</option>
<option value='2004'>2004</option>
<option value='2003'>2003</option>
<option value='2002'>2002</option>
<option value='2001'>2001</option>
<option value='2000'>2000</option>
<option value='1999'>1999</option>
<option value='1998'>1998</option>
<option value='1997'>1997</option>
<option value='1996'>1996</option>
<option value='1995'>1995</option>
<option value='1994'>1994</option>
<option value='1993'>1993</option>
<option value='1992'>1992</option>
<option value='1991'>1991</option>
<option value='1990'>1990</option>
<option value='1989'>1989</option>
<option value='1988'>1988</option>
<option value='1987'>1987</option>
<option value='1986'>1986</option>
<option value='1985'>1985</option>
<option value='1984'>1984</option>
<option value='1983'>1983</option>
<option value='1982'>1982</option>
<option value='1981'>1981</option>
<option value='1980'>1980</option>
<option value='1979'>1979</option>
<option value='1978'>1978</option>
<option value='1977'>1977</option>
<option value='1976'>1976</option>
<option value='1975'>1975</option>
<option value='1974'>1974</option>
<option value='1973'>1973</option>
<option value='1972'>1972</option>
<option value='1971'>1971</option>
<option value='1970'>1970</option>
<option value='1969'>1969</option>
<option value='1968'>1968</option>
<option value='1967'>1967</option>
<option value='1966'>1966</option>
<option value='1965'>1965</option>
<option value='1964'>1964</option>
<option value='1963'>1963</option>
<option value='1962'>1962</option>
<option value='1961'>1961</option>
<option value='1960'>1960</option>
<option value='1959'>1959</option>
<option value='1958'>1958</option>
<option value='1957'>1957</option>
<option value='1956'>1956</option>
<option value='1955'>1955</option>
<option value='1954'>1954</option>
<option value='1953'>1953</option>
<option value='1952'>1952</option>
<option value='1951'>1951</option>
<option value='1950'>1950</option>
<option value='1949'>1949</option>
<option value='1948'>1948</option>
<option value='1947'>1947</option>
</select>
</div>
<div class='col-sm-6 col-xs-6' id='col-padding-right'>
<select name='education[" . $x . "][end_year]' class='form-control field-custom'>
<option value='' disabled>Start Year</option>
<option value='" . $edu['end_year'] . "' selected>" . $edu['end_year'] . "</option>
<option value='2015'>2015</option>
<option value='2014'>2014</option>
<option value='2013'>2013</option>
<option value='2012'>2012</option>
<option value='2011'>2011</option>
<option value='2010'>2010</option>
<option value='2009'>2009</option>
<option value='2008'>2008</option>
<option value='2007'>2007</option>
<option value='2006'>2006</option>
<option value='2005'>2005</option>
<option value='2004'>2004</option>
<option value='2003'>2003</option>
<option value='2002'>2002</option>
<option value='2001'>2001</option>
<option value='2000'>2000</option>
<option value='1999'>1999</option>
<option value='1998'>1998</option>
<option value='1997'>1997</option>
<option value='1996'>1996</option>
<option value='1995'>1995</option>
<option value='1994'>1994</option>
<option value='1993'>1993</option>
<option value='1992'>1992</option>
<option value='1991'>1991</option>
<option value='1990'>1990</option>
<option value='1989'>1989</option>
<option value='1988'>1988</option>
<option value='1987'>1987</option>
<option value='1986'>1986</option>
<option value='1985'>1985</option>
<option value='1984'>1984</option>
<option value='1983'>1983</option>
<option value='1982'>1982</option>
<option value='1981'>1981</option>
<option value='1980'>1980</option>
<option value='1979'>1979</option>
<option value='1978'>1978</option>
<option value='1977'>1977</option>
<option value='1976'>1976</option>
<option value='1975'>1975</option>
<option value='1974'>1974</option>
<option value='1973'>1973</option>
<option value='1972'>1972</option>
<option value='1971'>1971</option>
<option value='1970'>1970</option>
<option value='1969'>1969</option>
<option value='1968'>1968</option>
<option value='1967'>1967</option>
<option value='1966'>1966</option>
<option value='1965'>1965</option>
<option value='1964'>1964</option>
<option value='1963'>1963</option>
<option value='1962'>1962</option>
<option value='1961'>1961</option>
<option value='1960'>1960</option>
<option value='1959'>1959</option>
<option value='1958'>1958</option>
<option value='1957'>1957</option>
<option value='1956'>1956</option>
<option value='1955'>1955</option>
<option value='1954'>1954</option>
<option value='1953'>1953</option>
<option value='1952'>1952</option>
<option value='1951'>1951</option>
<option value='1950'>1950</option>
<option value='1949'>1949</option>
<option value='1948'>1948</option>
<option value='1947'>1947</option>
</select>
</div>
</div>
</div>
<div class='col-sm-6 col-sm-offset-3 col-xs-12 p-custom text-center remove_field'>
<button type='button' class='btn btn-block button-cont-custom'><span class='glyphicon glyphicon-chevron-up' id='glyphicon-menu-right-margin'></span> Remove Field</button>
</div>
<div class='col-sm-6 col-sm-offset-3 col-xs-12 text-center'>
<hr id='hr-field'/>
</div>
</div>
";
?>
<div class='repeater-edu'>
// The repeater will go here
</div>
<!----------------- Repeater Button ----------------->
<div class="col-sm-6 col-sm-offset-3 col-xs-12 text-center">
<hr id="hr-field"/>
</div>
<div class="col-sm-6 col-sm-offset-3 col-xs-12 p-custom text-center">
<button type="button" class="btn btn-block button-cont-custom add-field-edu"><span class="glyphicon glyphicon-plus" id="glyphicon-menu-right-margin"></span> Add More Field</button>
</div> // The jQuery execution call
jQuery:-
$(document).ready(function(){
$('.remove_field').click(function(){
$(this).parent('div').remove();
})
});
$(document).ready(function(){
var max_fields = 20; // Maximum input boxes allowed
var wrapper = $(".repeater-edu"); // Fields wrapper
var add_field = $(".add-field-edu"); // Add button ID
var x = '<?php echo $x; ?>'; // Initlal text box count
$(add_field).click(function(e){ // On add input button click
e.preventDefault(); // Preventing action from executed?
if(x < max_fields){ // Max input box allowed
x++; // Text box increment
$(wrapper).append('<div><div class="col-sm-6 col-sm-offset-3 col-xs-12 text-center"><hr id="hr-field"/></div><div class="col-sm-6 col-sm-offset-3 col-xs-12 field-label-size" id="p-custom-color">School Name</div><div class="col-sm-6 col-sm-offset-3 col-xs-12 form-group-2 p-custom"><input type="text" name="education["'+(x)+'"][school]" class="form-control field-custom" placeholder="Your School Name"/></div><div class="col-sm-6 col-sm-offset-3 col-xs-12 field-label-size" id="p-custom-color">Concentration</div><div class="col-sm-6 col-sm-offset-3 col-xs-12 form-group-2 p-custom"><input type="text" name="education["'+(x)+'"][concentration]" class="form-control field-custom" placeholder="Your Course/Program Name"/></div><div class="col-sm-6 col-sm-offset-3 col-xs-12 field-label-size" id="p-custom-color">Period</div><div class="col-sm-6 col-sm-offset-3 col-xs-12 field-label-size form-group-2 p-custom"><div class="field-spacing"><div class="col-sm-6 col-xs-6" id="col-padding-left"><select name="education["'+(x)+'"][start_year]" class="form-control field-custom"><option value="" disabled selected>Start Year</option><option value="2016">2016</option><option value="2015">2015</option><option value="2014">2014</option><option value="2013">2013</option><option value="2012">2012</option><option value="2011">2011</option><option value="2010">2010</option><option value="2009">2009</option><option value="2008">2008</option><option value="2007">2007</option><option value="2006">2006</option><option value="2005">2005</option><option value="2004">2004</option><option value="2003">2003</option><option value="2002">2002</option><option value="2001">2001</option><option value="2000">2000</option><option value="1999">1999</option><option value="1998">1998</option><option value="1997">1997</option><option value="1996">1996</option><option value="1995">1995</option><option value="1994">1994</option><option value="1993">1993</option><option value="1992">1992</option><option value="1991">1991</option><option value="1990">1990</option><option value="1989">1989</option><option value="1988">1988</option><option value="1987">1987</option><option value="1986">1986</option><option value="1985">1985</option><option value="1984">1984</option><option value="1983">1983</option><option value="1982">1982</option><option value="1981">1981</option><option value="1980">1980</option><option value="1979">1979</option><option value="1978">1978</option><option value="1977">1977</option><option value="1976">1976</option><option value="1975">1975</option><option value="1974">1974</option><option value="1973">1973</option><option value="1972">1972</option><option value="1971">1971</option><option value="1970">1970</option><option value="1969">1969</option><option value="1968">1968</option><option value="1967">1967</option><option value="1966">1966</option><option value="1965">1965</option><option value="1964">1964</option><option value="1963">1963</option><option value="1962">1962</option><option value="1961">1961</option><option value="1960">1960</option><option value="1959">1959</option><option value="1958">1958</option><option value="1957">1957</option><option value="1956">1956</option><option value="1955">1955</option><option value="1954">1954</option><option value="1953">1953</option><option value="1952">1952</option><option value="1951">1951</option><option value="1950">1950</option><option value="1949">1949</option><option value="1948">1948</option><option value="1947">1947</option></select></div><div class="col-sm-6 col-xs-6" id="col-padding-right"><select name="education["'+(x)+'"][end_year]" class="form-control field-custom"><option value="" disabled selected>End Year</option><option value="2016">2016</option><option value="2015">2015</option><option value="2014">2014</option><option value="2013">2013</option><option value="2012">2012</option><option value="2011">2011</option><option value="2010">2010</option><option value="2009">2009</option><option value="2008">2008</option><option value="2007">2007</option><option value="2006">2006</option><option value="2005">2005</option><option value="2004">2004</option><option value="2003">2003</option><option value="2002">2002</option><option value="2001">2001</option><option value="2000">2000</option><option value="1999">1999</option><option value="1998">1998</option><option value="1997">1997</option><option value="1996">1996</option><option value="1995">1995</option><option value="1994">1994</option><option value="1993">1993</option><option value="1992">1992</option><option value="1991">1991</option><option value="1990">1990</option><option value="1989">1989</option><option value="1988">1988</option><option value="1987">1987</option><option value="1986">1986</option><option value="1985">1985</option><option value="1984">1984</option><option value="1983">1983</option><option value="1982">1982</option><option value="1981">1981</option><option value="1980">1980</option><option value="1979">1979</option><option value="1978">1978</option><option value="1977">1977</option><option value="1976">1976</option><option value="1975">1975</option><option value="1974">1974</option><option value="1973">1973</option><option value="1972">1972</option><option value="1971">1971</option><option value="1970">1970</option><option value="1969">1969</option><option value="1968">1968</option><option value="1967">1967</option><option value="1966">1966</option><option value="1965">1965</option><option value="1964">1964</option><option value="1963">1963</option><option value="1962">1962</option><option value="1961">1961</option><option value="1960">1960</option><option value="1959">1959</option><option value="1958">1958</option><option value="1957">1957</option><option value="1956">1956</option><option value="1955">1955</option><option value="1954">1954</option><option value="1953">1953</option><option value="1952">1952</option><option value="1951">1951</option><option value="1950">1950</option><option value="1949">1949</option><option value="1948">1948</option><option value="1947">1947</option></select></div></div></div><div class="col-sm-6 col-sm-offset-3 col-xs-12 p-custom text-center remove_field"><button type="button" class="btn btn-block button-cont-custom"><span class="glyphicon glyphicon-chevron-up" id="glyphicon-menu-right-margin"></span> Remove Field</button></div></div>'); // Add input box
}
});
$(wrapper).on("click",".remove_field", function(e){ // User click on remove text
e.preventDefault(); $(this).parent('div').remove(); x--;
})
});

You can simple do this like
$x = -1;
foreach ($something as $some){
$x++;
}
and then you can assign the value of $x in your javascript code like this
var x = '<?php echo $x; ?>';
var max = 20;
if(x < max){
x++;
// The 'x' will continue the count as 3, 4, 5,...
}

You can add a hidden element to DOM and assign the value of $x to it. Then in jQuery you can take the value from the hidden element.
Example
In PHP:
... ...
... ...
Your PHP code upto "repeater-edu"
... ...
<input type="hidden" name="last_counter_of_loop" id="last_counter_of_loop" value="<?php echo $x; ?>"/>
<div class='repeater-edu'>
... ... ...
... ... ...
Then in your JQuery code:
var x = $("#last_counter_of_loop").val()
... ... ...
rest of your code
... ... ...
I hope it will work.
Thanks.

take click event as:
$(document).on("click",".add-field-edu",function(e){
...
});

More simpler way
<?php
$x=count($something)-1;
<script>
$(<you DOM object>).ready(function(){
var x = '<?php echo $x; ?>';
var max = 20;
if(x < max){
x++;
}
})
</script>
?>

Related

How to get value from combobox into next url

I wonder if anyone can help my problem, in which way I can get value from combobox selection into next url by clicking so I can make it as a parameter to the pdf report.
this is my code to add some combobox :
<form method="GET">
<label class="col-sm-3 control-label">Bulan</label>
<div class="col-sm-2">
<select name="bln" class="form-control">
<option value="1">Januari</option>
<option value="2">Februari</option>
<option value="3">Maret</option>
<option value="4">April</option>
<option value="5">Mei</option>
<option value="6">Juni</option>
<option value="7">Juli</option>
<option value="8">Agustus</option>
<option value="9">September</option>
<option value="10">Oktober</option>
<option value="11">November</option>
<option value="12">Desember</option>
</select>
</div>
<br>
<br>
<label class="col-sm-3 control-label">Tahun</label>
<div class="col-sm-2">
<select name="thn" class="form-control">
<option value="2018">2018</option>
<option value="2019">2019</option>
<option value="2020">2020</option>
<option value="2021">2021</option>
<option value="2022">2022</option>
<option value="2023">2023</option>
<option value="2024">2024</option>
<option value="2025">2025</option>
</select>
</div>
</form>
<?php
if (isset($_GET['bln'])) {
$bln = $_GET['bln'];
$thn = $_GET['thn'];
}
?>
<a class="btn btn-success" href="rep-cuti-tahunan-all.php?bln="$bln"&thn="$thn"">Print</a>
so I would like to put $bln and $thn into <a class="btn btn-success" href="rep-cuti-tahunan-all.php?bln="$bln"&thn="$thn"">Print</a>
and get my report in pdf exactly according to that parameters
any suggestions?
You don't have to add it as a parameter.Just change your form like this.
<form method="GET" action ="rep-cuti-tahunan-all.php">
....
</form>
then on your php file, you fetch the selected value.
<?php
....
if (isset($_GET['bln'])) {
$bln = $_GET['bln'];
$thn = $_GET['thn'];
}
....
//Do your print method ...
?>

Auto populate next select box based on previous field answer

I have 2 questions if the answer to the first question is 1> I want the next answer to be auto populated as Yes. Can someone steer me in the right direction please?
<div class="field">
<!-- Number of Employees -->
<label>Number of Employees</label>
<div class="styled-select">
<select name="NoOfEmployees" class="validate[required]" data-prompt-position="topLeft:110,15">
<option value="">Please select</option>
<?php
foreach (range(0, 49) as $number) {
echo '<option value="'.$number.'">'.$number.'</option>';
}
?>
<option value="50 Plus">50+</option>
</select>
</div>
</div>
<div class="field">
<!-- Employers Liability Cover Required -->
<label>Employers Liability Cover Required</label>
<div class="styled-select">
<select name="EmployersLiabilityCover" class="validate[required]" data-prompt-position="topLeft:110,15">
<option value="">Please select</option>
<option value="Yes">Yes</option>
<option value="No">No</option>
</select>
</div>
</div>
If the answer is 0 then I just need the next question to stay as Please Select.
use jquery onchange , give selectbox inputs seperate id,
Use jquery val() to get selectbox value
use if condition to check its greater than 0
code shown below
<div class="field">
<!-- Number of Employees -->
<label>Number of Employees</label>
<div class="styled-select">
<select name="NoOfEmployees" class="validate[required]" id="select1" data-prompt-position="topLeft:110,15">
<option value="">Please select</option>
<?php
foreach (range(0, 49) as $number) {
echo '<option value="'.$number.'">'.$number.'</option>';
}
?>
<option value="50 Plus">50+</option>
</select>
</div>
</div>
<div class="field">
<!-- Employers Liability Cover Required -->
<label>Employers Liability Cover Required</label>
<div class="styled-select">
<select name="EmployersLiabilityCover" id="select2" class="validate[required]" data-prompt-position="topLeft:110,15">
<option value="">Please select</option>
<option value="Yes">Yes</option>
<option value="No">No</option>
</select>
</div>
</div>
<script src="https://code.jquery.com/jquery-2.1.4.js"></script>
<script>
$('#select1').on('change', function() {
if(this.value>0)
{
$("#select2").val("Yes");
}
else
{
$("#select2").val("");
}
});
</script>

How to call data with variables that we want

I'm creating a system which provide a report generator, now my problem is i dont know how to call data with the variables that i want. For example I only want the data from January // 2014 // from the account department and so on. Now, i don't know what code to show but below is the menu where user select their variables
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
function showForm() {
var selopt = document.getElementById("opts").value;
if (selopt == "Tenaga Nasional Berhad") {
document.getElementById("f1").style.display = "block";
document.getElementById("f2").style.display = "none";
}
if (selopt == "Telekom Malaysia") {
document.getElementById("f2").style.display = "block";
document.getElementById("f1").style.display = "none";
}
if (selopt == 0) {
document.getElementById("f2").style.display = "none";
document.getElementById("f1").style.display = "none";
}
}
</script>
<div id="contact_form">
<h4>Pilih Ketetapan Laporan</h4>
<form method="post" name="pilih_kategori_daftar">
<label for="daerah">Daerah:</label>
<select class="required input_field" name="daerah" id="daerah" required/>
<option value="">--Pilih Daerah--</option>
<option value="Kuantan">Kuantan</option>
<option value="Maran">Maran</option>
<option value="Bentong">Bentong</option>
<option value="Raub">Lipis</option>
<option value="Jerantut">Jerantut</option>
<option value="Cameron Highlands">Cameron Highlands</option>
<option value="P.Tenggara">Pahang Tenggara</option>
<option value="Pekan">Pekan</option>
<option value="Rompin">Rompin</option>
<option value="Temerloh">Temerloh</option>
<option value="Bera">Bera</option>
<option value="Ibu Pejabat">Ibu Pejabat</option>
</select>
<div class="cleaner_h10"></div>
<label for="bulan_bill">Bulan</label>
<select class="required bill_caj" name="bulan_bill" id="bulan_bill" width="300px" required/>
<option value="">--Pilih Bulan--</option>
<option value="Januari">Januari</option>
<option value="Februari">Februari</option>
<option value="Mac">Mac</option>
<option value="April">April</option>
<option value="Mei">Mei</option>
<option value="Jun">Jun</option>
<option value="Julai">Julai</option>
<option value="Ogos">Ogos</option>
<option value="September">September</option>
<option value="Oktober">Oktober</option>
<option value="November">November</option>
<option value="Disember">Disember</option>
</select>
<div class="cleaner_h10"></div>
<label for="tahun_bill">Tahun</label>
<input type="text" maxlength="4" size="4" id="tahun_bill" name="tahun_bill" class="required year_field" placeholder="Tahun" onkeyup="this.value=this.value.replace(/[^0-9.]/g,'')" required/>
<div class="cleaner_h10"></div>
<label for="kategori_akaun">Kategori</label>
<select id="opts" onchange="showForm()" class="input_field" required>
<option value="0">--Pilih Kategori--</option>
<option value="Tenaga Nasional Berhad">Akaun TNB</option>
<option value="Telekom Malaysia">Akaun TM</option>
</select>
<div class="cleaner_h10"></div>
<div id="f1" style="display:none">
<form name="akaun_tnb">
<label for="jenis">Jenis Akaun</label>
<select id="opts" onchange="showForm()"class="input_field">
<option value="0">--Pilih Jenis Akaun--</option>
<option value="Rumah Pam">Rumah Pam</option>
<option value="Loji Air">Loji Air</option>
<option value="Stor">Stor</option>
</select>
</form>
</div>
<div id="f2" style="display:none">
<form name="akaun_tm">
<label for="jenis">Jenis Akaun</label>
<select id="opts" onchange="showForm()"class="input_field">
<option value="0">--Pilih Jenis Akaun--</option>
<option value="Telefon">Telefon</option>
<option value="Telefon/Streamyx">Telefon/Streamyx</option>
<option value="Streamyx">Streamyx</option>
<option value="SMS Blast">SMS Blast</option>
<option value="TM NET">TM NET</option>
</select>
</form>
</div>
</form>
</div>
search in google:
1, How to create mysqli database, tables, columns.
2, PHP, how to connect, extract, insert data from mysqli using php,
3. How to echo mysqli data into html, 4. how to submit form to php and insert data to mysqli, 5. Learn to use AJAX & JSON...this you may leave for few months:)

Need my HTML Search Form to search the Options Selected on the Search Form and then show my results?

I have a website with 4 pages: Home Page , Sale Page , Rent Page and Contact Us Page.
On the Home Page is a Search Form that need to Search the Options they select. It is a Property website so if they click on the search button they need to be taken to the Sale Page or Rent Page what they have selected, for instance if they Select For Sale, House, 3 Bedrooms, 2 Bathrooms and then whatever amount on Min Price / Max Price then it needs to go to the Sale Page and retrieve the Houses on the Values they have Selected.
Here is a Image of the Search Form:
Can anyone help me to solve this please?
Here is the HTML Search Form coding:
<form id="advanced_search" action="search.php" class="clearfix" name="advanced_search" method="post">
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<label for="type"><b>Type</b></label>
<select>
<option value="House">House</option>
<option value="Apartment">Apartment</option>
<option value="Flat">Flat</option>
<option value="Townhouse">Townhouse</option>
<option value="Plot">Plot</option>
<option value="Farm">Farm</option>
</select>
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<label for="status"><b>Status</b></label>
<select>
<option value="For Sale">For Sale</option>
<option value="For Rent">For Rent</option>
</select>
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<label for="bedrooms"><b>Bedrooms</b></label>
<select>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<label for="baths"><b>Bathrooms</b></label>
<select>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<label for="min_price"><b>Min Price</b></label>
<select>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<label for="max_price"><b>Max Price</b></label>
<select>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
</div>
<div class="clearfix"></div>
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<button name="search" type="submit" class="btn btn-success">SEARCH PROPERTY</button>
</div>
</form>
Here is the PHP Coding that I am struggling with:
<?php
if(isset($_POST['search']))
{
$key=$_POST["search"]; //key=pattern to be searched
$con=mysqli_connect("localhost", "root", "");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result=mysqli_query($con,"SELECT * FROM properties WHERE");
while($row=mysqli_fetch_assoc($result))
{
}
}
?>
Try this:
<?php
$type = $_POST['type'];
$status = $_POST['status'];
$minPrice = $_POST['min_price'];
$maxPrice = $_POST['max_price'];
$bedrooms = $_POST['bedrooms'];
$baths = $_POST['baths'];
$con=mysqli_connect("localhost","root","","house");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM properties WHERE tipo='$type' and baths = '$baths' and bedrooms = '$bedrooms' and status = '$status' and price between '$minPrice' and '$maxPrice'");
while($row = mysqli_fetch_array($result)) {
echo $row['type'] . " " . $row['status'];
echo "<br>";
}
mysqli_close($con);
I have created a table here and it is working.
Do not forget to change on html code " tag and add on this the property "name"
There are a lot of things wrong, first you didn't named your html form tags, and secondly you didn't use the $key variable in your php code to modify your query. You need learn about basic form interactions. I also strongly suggest you to look into MVC, having a sql connection just 10 lines above the template code is really bad.
You have to change this:
$result=mysqli_query($con,"SELECT * FROM properties WHERE");
first question, are the options: "max price, type and etc..." on the same table?
you can do something like that:
$type = $_POST['type'];
$status = $_POST['status'];
$minPrice = $_POST['minPrice'];
$maxPrice = $_POST['maxPrice'];
$result=mysqli_query($con,"SELECT * FROM properties WHERE type='$type' and status = '$status' and max_price between '$minPrice' and '$maxPrice'");
to the same for another fields, should work
try this in your query:
$type = $_POST['$type'];
$status = $_POST['$status'];
$bedrooms = $_POST['bedrooms'];
$bathrooms = $_POST['bathrooms'];
$minPrice = $_POST['minPrice'];
$maxPrice = $_POST['maxPrice'];
$result=mysqli_query($con,"SELECT * FROM properties WHERE type = '$type' and status = '$status' and bedrooms='$bedrooms' and bathrooms = '$bathrooms' and price between $minPrice and $maxPrice");
it will work if your code above your $result is ok

IF POST Variables equals then redirect

Im trying to get form post values and if they match certain criteria redirect only my page doesnt seem to...
I have a form with multiple input fields
HTML
<form method="post" action="?page_id=151">
<div style="display: block;" id="Q1">
<label for="vehicleType">1. What type of vehicles do you have?</label>
<select id="vehicleType" name="vehicleType">
<option selected="selected" value="choose">Please choose</option>
<option value="hgv">HGV</option>
<option value="psv">PSV</option>
<option value="lgv">LGV</option>
<option value="car">Car</option>
<option value="mixed">Mixed Fleet</option>
</select>
</div>
<div id="Q2group1">
<label for="coverageRegion">2. Do you require national or international coverage?</label>
<select id="coverageRegion" name="coverageRegion">
<option selected="selected" value="choose">Please choose</option>
<option value="national">National</option>
<option value="international">International</option>
</select>
</div>
<div id="Q2others">
<label for="pricing">2. Do you prefer a fixed weekly price or a pump-related price?</label>
<select id="pricing" name="pricing">
<option selected="selected" value="choose">Please choose</option>
<option value="fixed">Weekly fixed price</option>
<option value="pump">Pump-related price</option>
</select>
</div>
<div id="Q3group1">
<label for="locationType">3. What type of site do you prefer?</label>
<select id="locationType" name="locationType">
<option selected="selected" value="choose">Please choose</option>
<option value="truckstops">Truck stops at lowest price</option>
<option value="motorway">Branded motorway sites</option>
</select>
</div>
<div id="Q3others">
<label for="siteCoverage">3. What site coverage do you need?</label>
<select id="siteCoverage" name="siteCoverage">
<option value="choose">Please choose</option>
<option value="countysites">County</option>
<option value="nationalsites">National</option>
</select>
</div>
<div id="Q4others">
<label for="county">4. Select your county:</label>
<select id="county" name="county">
<option value="choose">Please choose</option>
<optgroup label="England">
<option>Bedfordshire</option>
<option>Berkshire</option>
<option>Bristol</option>
<option>Tyrone</option>
</optgroup>
</select></div>
<div id="searchbuttondiv"> <input type="submit" id="searchbutton" onclick="_gaq.push(['_trackEvent', 'Fuel Card Search', 'Homepage Select Criteria'])"> </div>
</form>
PHP
if ($_POST['vehicleType'] == 'car' && $_POST['pricing'] == 'fixed' && $_POST['coverageRegion'] == 'national') {
echo '<script>alert("hi");</script>';
// header('Location: /car-weekly-national/');
}
One of your conditions is falling (and because they are all bound by AND, all of the if statement fails.
Have an else statement where you var_dump all relevant variables and see what's wrong.
Sorry everyone, oversight by me, I was checking to see if the wrong variable had been posted, wouldn't have found it if it wasn't for the suggestions to add an else statement, thanks!

Categories