Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,724 members, 7,809,758 topics. Date: Friday, 26 April 2024 at 02:22 PM

How Do I Go About These Codes? - Webmasters - Nairaland

Nairaland Forum / Science/Technology / Webmasters / How Do I Go About These Codes? (1217 Views)

Bluehost Or Hostgator: Which Should I Go For? / .com Or .com.ng Which Should I Go For (2) (3) (4)

(1) (Reply) (Go Down)

How Do I Go About These Codes? by Emusan(m): 11:31pm On Jun 23, 2015
First Part

<?php
$makes = array('Acura', 'BMW', 'Lexus', 'Toyota', 'Honda');

if(isset($_POST['submit']))
{ echo '<pre>'; print_r($_POST); echo '</pre>'; }
?>
Form for the First Select Box
<form action="" method="post">
<select id="makes" name="makes">
<?php
foreach($makes as $m){ echo '<option value="'.$m.'">'.$m.'</option>'; }
?>
</select>
<select id="models" name="models">
</select>
<input type="submit" name="submit" value="Submit">
</form>

Second Part-JQuery

<script>
$('#makes').change(function(){
var make = $(this).val();
$.post('select-request.php', {make:make}, function(data){
$('#models').html(data);
});
});
</script>

Third Part
<?php
error_reporting(E_ALL);

$acura = array('Integra', 'TSX', 'MDX');
$bmw = array('1 Series', '3 Series', '5 Series', 'X5');
$lexus = array('ES300', 'GX470', 'GS350', 'LS400H');
$toyota = array('Venza', 'Camry', 'Corolla', 'Echo');
$honda = array('Pilot', 'Accord', 'Civic', 'Ridgeline');

if(isset($_POST['make']))
{
$model = strtolower($_POST['make']);

foreach($$model as $mo){ echo '<option value="'.$mo.'">'.$mo.'</option>'; }
}
?>
Re: How Do I Go About These Codes? by Nobody: 9:32am On Jun 24, 2015
yes you can,both codes are pretty much small to have stand-alone files.....
what's the file is to look like depends on what you are trying to achieve,
1.)do you want to select the irrespective of the other.... you could build different form tags in your layout and include this

2.)if you want to select them in the same form, fix everything in just one form tag


Then again, your Call

for the JavaScript --- you can also place it in the same file but you must make sure the jquery library is included before it
Re: How Do I Go About These Codes? by Emusan(m): 4:38pm On Jun 25, 2015
Jregz:
yes you can,both codes are pretty much small to have stand-alone files.....
what's the file is to look like depends on what you are trying to achieve,
1.)do you want to select the irrespective of the other.... you could build different form tags in your layout and include this

2.)if you want to select them in the same form, fix everything in just one form tag


Then again, your Call

for the JavaScript --- you can also place it in the same file but you must make sure the jquery library is included before it

Thanks very much.

I so much love this your advice and I'll go for it.

But can you just use those code above as an example of what to do, please?
Re: How Do I Go About These Codes? by Emusan(m): 4:35pm On Jun 27, 2015
Jregz:
yes you can,both codes are pretty much small to have stand-alone files.....
what's the file is to look like depends on what you are trying to achieve,
1.)do you want to select the irrespective of the other.... you could build different form tags in your layout and include this

2.)if you want to select them in the same form, fix everything in just one form tag


Then again, your Call

for the JavaScript --- you can also place it in the same file but you must make sure the jquery library is included before it
Re: How Do I Go About These Codes? by Emusan(m): 1:08pm On Jul 03, 2015
Emusan:
First Part

<?php
// An array of options for the first select box.
$makes = array('Acura', 'BMW', 'Lexus', 'Toyota', 'Honda');

// Displays the posted info
if(isset($_POST['submit']))
{ echo '<pre>'; print_r($_POST); echo '</pre>'; }
?>
<form action="" method="post">
<select id="makes" name="makes">
<?php
// Build the options for the first select box
foreach($makes as $m){ echo '<option value="'.$m.'">'.$m.'</option>'; }
?>
</select>
<select id="models" name="models">
</select>
<input type="submit" name="submit" value="Submit">
</form>

Second Part-JQuery

<script>
$('#makes').change(function(){ //Basically saying when the first select box changes values run the function below.
var make = $(this).val(); // Grab the value of the selection to send to the select-request.php via ajax
$.post('select-request.php', {make:make}, function(data){ // Run a ajax request and send the var make as a post variable named "make" and return the info in the "data" var.
$('#models').html(data); // Have jquery change the html within the second select box with the "data" we got back from the ajax request.
});
});
</script>

Third Part

<?php
error_reporting(E_ALL); //Remove this line for production, it simply will allow php to display any errors

// I built arrays based on the value names in the $makes array
$acura = array('Integra', 'TSX', 'MDX');
$bmw = array('1 Series', '3 Series', '5 Series', 'X5');
$lexus = array('ES300', 'GX470', 'GS350', 'LS400H');
$toyota = array('Venza', 'Camry', 'Corolla', 'Echo');
$honda = array('Pilot', 'Accord', 'Civic', 'Ridgeline');

//We check to see if the "make" post has come through before we do any processing.
if(isset($_POST['make']))
{
$model = strtolower($_POST['make']);

foreach($$model as $mo){ echo '<option value="'.$mo.'">'.$mo.'</option>'; }
}
?>

Can someone help me on how to put all these in ONE PHP FILE?

I tried it but it always returns ERROR.
Re: How Do I Go About These Codes? by Emusan(m): 11:21am On Aug 26, 2015
Emusan:


<?php
$makes = array('Acura', 'BMW', 'Lexus', 'Toyota', 'Honda');

if(isset($_POST['submit']))
{ echo '<pre>'; print_r($_POST); echo '</pre>'; }
?>

<form action="" method="post">
<select id="makes" name="makes">
<?php
foreach($makes as $m){ echo '<option value="'.$m.'">'.$m.'</option>'; }
?>
</select>
<select id="models" name="models">
</select>
<input type="submit" name="submit" value="Submit">
</form>



<script>
$('#makes').change(function(){
var make = $(this).val();
$.post('select-request.php', {make:make}, function(data){
$('#models').html(data);
});
});
</script>


<?php
error_reporting(E_ALL);

$acura = array('Integra', 'TSX', 'MDX');
$bmw = array('1 Series', '3 Series', '5 Series', 'X5');
$lexus = array('ES300', 'GX470', 'GS350', 'LS400H');
$toyota = array('Venza', 'Camry', 'Corolla', 'Echo');
$honda = array('Pilot', 'Accord', 'Civic', 'Ridgeline');

if(isset($_POST['make']))
{
$model = strtolower($_POST['make']);

foreach($$model as $mo){ echo '<option value="'.$mo.'">'.$mo.'</option>'; }
}
?>

Please a need someone to help here;
1. I want to put the whole thing on the same file
2. How can I re-write the jQuery/<script> so that it won't contain the $.post(select-request)?
3. I want onchange event to occur at the second select box.

Please if you want to help, kindly use the above codes as an example.

Thanks!
Re: How Do I Go About These Codes? by onyengbu: 1:15am On Aug 27, 2015
Emusan:


Please a need someone to help here;
1. I want to put the whole thing on the same file
2. How can I re-write the jQuery/<script> so that it won't contain the $.post(select-request)?
3. I want onchange event to occur at the second select box.

Please if you want to help, kindly use the above codes as an example.

Thanks!

Below is the same code with the jquery part re written. Save in a php file named select.php or update line 27 if you save in another file.
You can see the jquery library I added is hosted online so run the file in a internet connected system or change line 19 to link to to your local copy of jquery library if you are offline.
Cheers.

<?php
$makes = array('Acura', 'BMW', 'Lexus', 'Toyota', 'Honda');
if(isset($_POST['submit']))
{ echo '<pre>'; print_r($_POST); echo '</pre>'; }
?>

<form action="" method="post">
<select id="makes" name="makes">
<?php
foreach($makes as $m){ echo '<option value="'.$m.'">'.$m.'</option>'; }
?>
</select>
<select id="models" name="models">
</select>
<input type="submit" name="submit" value="Submit">
</form>


<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$( "select#makes" ).change(function () {
var make = $( "#makes" ).val();
var string = 'make='+make;
$.ajax({
type: "POST",
url: "select.php",
data: string,
cache: false,
success: function(data){
$('#models').html(data);
}
});
});
return false;
});
</script>


<?php
error_reporting(E_ALL);

$acura = array('Integra', 'TSX', 'MDX');
$bmw = array('1 Series', '3 Series', '5 Series', 'X5');
$lexus = array('ES300', 'GX470', 'GS350', 'LS400H');
$toyota = array('Venza', 'Camry', 'Corolla', 'Echo');
$honda = array('Pilot', 'Accord', 'Civic', 'Ridgeline');

if(isset($_POST['make']))
{
$model = strtolower($_POST['make']);

foreach($$model as $mo){ echo '<option value="'.$mo.'">'.$mo.'</option>'; }
}
?>

1 Like

Re: How Do I Go About These Codes? by Emusan(m): 8:48pm On Aug 28, 2015
onyengbu:


Below is the same code with the jquery part re written. Save in a php file named select.php or update line 27 if you save in another file.
You can see the jquery library I added is hosted online so run the file in a internet connected system or change line 19 to link to to your local copy of jquery library if you are offline.
Cheers.

Thanks so much for your contribution but I'm having problem with the second select box now because the first select box (for #makes) populated but the second select box (for #models) doesn't populate.

What can I do?
Thanks once again.
Re: How Do I Go About These Codes? by onyengbu: 9:58pm On Aug 28, 2015
Emusan:


Thanks so much for your contribution but I'm having problem with the second select box now because the first select box (for #makes) populated but the second select box (for #models) doesn't populate.

What can I do?
Thanks once again.
The second select box (models) populating is the main plot behind the code and it is properly populating when you change makes value.

If you copied my code verbatim and followed the instructions, it must work.
Re: How Do I Go About These Codes? by Emusan(m): 10:55pm On Aug 28, 2015
onyengbu:

The second select box (models) populating is the main plot behind the code and it is properly populating when you change makes value.

If you copied my code verbatim and followed the instructions, it must work.

Yes I copied everything just as you wrote them, just don't know the reason why it couldn't populate.

Thanks once again.

Can you help me with another alternative?

*modify
Let me say this, I'm using this code inside a PHP file where getheader was placed, so where exactly will the SCRIPT fit to be inserted?
Could it be after the gethearder or before?
Inside the form tag or outside the form tag?

because I placed the SCRIPT inside the form tag.
Re: How Do I Go About These Codes? by onyengbu: 12:26am On Aug 29, 2015
Emusan:


Yes I copied everything just as you wrote them, just don't know the reason why it couldn't populate.

Thanks once again.

Can you help me with another alternative?

*modify
Let me say this, I'm using this code inside a PHP file where getheader was placed, so where exactly will the SCRIPT fit to be inserted?
Could it be after the gethearder or before?
Inside the form tag or outside the form tag?

because I placed the SCRIPT inside the form tag.

If you copied everything as I wrote, did you also change your script name to match the url in the jquery part of the script?

It doesnt matter where you placed the script as long as you added the jquery library as I instructed.

(1) (Reply)

Why You Should Become A Web Developer / How To Make $974/month With Clickbank And Youtube (without Uploading Any Videos) / Smartweb Nigeria Hosting Services: Worst Hosting Company In Nigeria 2015/2016

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 45
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.