How Do I Go About These Codes? - Webmasters - Nairaland
Nairaland Forum › Science/Technology › Webmasters › How Do I Go About These Codes? (1488 Views)
| How Do I Go About These Codes? by Emusan(op): 11:31pm On Jun 23, 2015*. Modified: 11:08am On Aug 26, 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(op): 4:38pm On Jun 25, 2015 |
Jregz: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(op): 4:35pm On Jun 27, 2015 |
Jregz: |
| Re: How Do I Go About These Codes? by Emusan(op): 1:08pm On Jul 03, 2015 |
Emusan: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(op): 11:21am On Aug 26, 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! |
| Re: How Do I Go About These Codes? by onyengbu: 1:15am On Aug 27, 2015 |
Emusan: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 |
| Re: How Do I Go About These Codes? by Emusan(op): 8:48pm On Aug 28, 2015 |
onyengbu: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: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(op): 10:55pm On Aug 28, 2015*. Modified: 11:21pm On Aug 28, 2015 |
onyengbu: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: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. |
Bluehost Or Hostgator: Which Should I Go For? • .com Or .com.ng Which Should I Go For • 2 • 3 • 4
Adsense Has Finally Approved Me • Naija And Usa Non-hosted Acct For Sale Inbox Now 08102308819 #5,000 • Smf Forum Developer/designer Urgently Needed