www.webdeveloper.com
Results 1 to 3 of 3

Thread: PHP include in JS - dynamic select form

  1. #1
    Join Date
    Jan 2008
    Posts
    7

    Question PHP include in JS - dynamic select form

    Hi

    I need some help. I need a form with two select tags, one 'category' and one 'subcategory' where the subcategory change depending on the main category.

    I have tried lots of things but can't get it working.. My latest attempt was to create seperate html-pages with just the options in them and to use the PHP include() to add the different subcategories via onChange. I was obviously unsuccessful.

    Code:
    <form method="post" action="search.php">
    	<select name="category" onChange="javascript:showCategory(this)">
    		<option value="">Välj en kategori</option>
    		<option value="cat1">Category1</option>
    		<option value="cat2">Category2</option>
    	</select>
    </form>
    Code:
    <script>
    	function showCategory(catItem)
    	{
    		var i = catItem.selectedIndex;
    		if(i > 0)
    		{
    			var value = catItem.options[i].value;
    			if(value == "cat1")
    			{
    				<?php include("category/cat1.html");?>
    			}
    			else if(value == "cat2")
    			{
    				<?php include("category/cat2.html");?>
    			}
    		}
    	}
    </script>
    Contents of cat1.html
    Code:
    <option>Car</option>
    <option>Boat</option>
    Would this method work at all? I've searched a lot on the subject and learnt that most recommend using AJAX to do this, but I barely know anything about JS so it would take too long to learn how to do that on my own; my deadline is Monday..

    Please help me

  2. #2
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    No. It woun't work. php (and any other server language) writes something only when the page is loaded or the session is changed. You need the AJAX technique to do what you want to do:

    http://en.wikipedia.org/wiki/Ajax_(programming)
    http://www.ajaxf1.com/tutorial/ajax-php.html
    http://www.w3schools.com/PHP/php_ajax_database.asp

    Quote Originally Posted by YZF-R1
    my deadline is Monday..
    Hmm... You have settled down to a much too ambitious job. You must have known javascript quite well in order to accept that deadline

    You may also simply submit the data to a php application which will write a new page, but that means you will have to change the session.
    Last edited by Kor; 04-30-2009 at 03:08 PM.

  3. #3
    Join Date
    Jan 2008
    Posts
    7
    Ok I just have to skip that part of the page then =/. Thanks for clearing it up for me

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles