Jump to content

HTML noob needs help adding PHP interactivity


Chamza

Recommended Posts

First of all, I am very new to PHP and come from a background of designing, aka HTML and CSS.  PHP has been blowing my mind these past several weeks and I am truly stuck. 

 

Basically, I have a very simple drop-down menu and a submit button made in HTML.  What I want to do is so that when I choose something from the drop down menu, the "submit" button will execute a different PHP code depending on my choice.  So, for example, if I choose "Everything" in the dropdown menu, the submit button's destination will turn in to everything.php without reloading the page, and if i choose "Jeans" the button will turn in to jeans.php behind the scenes.

 

Here is my html:

<form method="post">
<select name="category">
<option value="everything">Everything</option>
<option value="jeans">Jeans</option>
<option value="watches">Watches</option>
</select>
<input type="submit" />
</form>

 

I've attempted to add functionality with php and I can post that code but it is completely wrong.  So if anyone can give me some pointers or write some of the code themselves to show me, I'd appreciate it.  Thanks.

Link to comment
Share on other sites

Okay.. first of all posting a form will reload the page.. if you don't want the page to reload your need to looking to frames or AJAX,

 

with that said.. here a basic one which will require a reload, but should give you an idea (untested)

<form method="post">
<select name="category">
<option value="everything">Everything</option>
<option value="jeans">Jeans</option>
<option value="watches">Watches</option>
</select>
<input type="submit" />
</form>
<?php
//default page
$default = "default.php";

//check something has been selected
if(!empty($_POST['category']))
{
    //convert to lowercase
    $cat = strtolower($_POST['category']).".php";
}else{
    $cat = $default;
}

//an array of allowed files (for security reasons)
$pages = array($default,"everything.php","jeans.php","watches.php");

//check the selected file is in the allowed list
if (in_array($cat, $pages )) {
    //if so include it
    echo "loading $cat";
    include $cat;
}else{
    //if not then use default.php
    include $default;
}
?>

 

EDIT: as a note you could also use header to redirect to a page ..

Link to comment
Share on other sites

You could also just include something in the destination script that would change the action depending on the value of "category".

 

i.e.

 

<?php
if ($_POST['category'] = 'everything')
{
//Do something...
}

elseif ($_POST['category'] = 'jeans')
{
//Do something else
}

elseif ($_POST['category'] = 'watches')
{
//Do something else entirely
}

else
{
echo 'Something screwed up...';
}
?>

 

This would keep all your code in one file, and you won't need to fiddle around w/ JavaScript. The user wouldn't be any the wiser, either. :D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.