I have been having problems returning results from my database using AJAX. I have tried to echo $diskspace and $price, both of which are returning undefined.
index.php
<form id="form" method="POST">
Diskspace:
<select id="Diskspace">
<option value="0 AND 1">$0 - 1GB</option>
<option value="1 AND 5">$1 - 5GB</option>
<option value="5 AND 10">$5 - 10GB</option>
</select></br></br>
Price:
<select id="Price">
<option value="0 AND 5">$0 - $5</option>
<option value="1 AND 5">$5 - $10</option>
<option value="10 AND 20">$10 - $20</option>
<option value="20 AND 40">$20 - $40</option>
<option value="40 and 500">>$40</option>
</select></br></br>
<input type="submit" id="submit" value="enter">
</form>
<div id="output"></div>
JS
$(document).ready(function(){
$("div#output").hide();
$("#submit").click(function(){
$.post('join.php', {
diskspace: $("#diskspace").val(),
price: $("#price").val()
},
function(data){
$("div#output").html(data);
$("div#output").show();
}
);
return false;
});
});
</script>
join.php
<?php
if (isset($_POST['diskspace'])){
mysql_connect("localhost","root","") or die('Could not connect');
mysql_select_db("webhost") or die ('Could not find DB');
$diskspace = $_POST['diskspace'];
$price =$_POST['price'];
$query = mysql_query("
SELECT * FROM data WHERE Diskspace BETWEEN $diskspace
AND Price BETWEEN $price
");
$count = mysql_num_rows($query);
if ($count == 0){
$output = "No such results, sorry.";
}else{
while($row = mysql_fetch_array($query)){
$diskspace = $row['Diskspace'];
$price = $row['Price'];
$host = $row['Provider'];
$output .= '<div>'.$host.' '.$diskspace.' '.$price.'</div>';
}
}
echo $output;
}
?>
Your HTML element has an id of Diskspace and you're looking for an element of diskspace.
Take the following for example:
<select id="Diskspace">
<option value="hello">hello</option>
</select>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
console.log( $('#diskspace').val() );
});
</script>
The result is undefined, and in new versions of jQuery, if the element cannot be found it will not pass the variable through ajax/post.
The same applies to your Price element.
Your select id is not same in your jquery. Change it like this:
<form id="form" method="POST">
Diskspace:
<select id="diskspace">
<option value="0 AND 1">$0 - 1GB</option>
<option value="1 AND 5">$1 - 5GB</option>
<option value="5 AND 10">$5 - 10GB</option>
</select></br></br>
Price:
<select id="price">
<option value="0 AND 5">$0 - $5</option>
<option value="1 AND 5">$5 - $10</option>
<option value="10 AND 20">$10 - $20</option>
<option value="20 AND 40">$20 - $40</option>
<option value="40 and 500">>$40</option>
</select></br></br>
<input type="submit" id="submit" value="enter">
</form>
<div id="output"></div>
replace:
diskspace: $("#diskspace").val(),
with
diskspace: $("#Diskspace").val(),
You didn't give your <select>s names:
<select id="Diskspace" name="Diskspace">
^^^^^^^^^^^^^^^^--- missing
No name, no form submission for that field. id does NOT count - it's used purely for DOM operations, and is not relevant for form submissions.
Beyond that, you're vulnerable to SQL injection attacks.
Related
I am trying to make it so when you select an option, it goes to a specific subdomain. e.g. if coffee is selected, it should redirect to coffee.domain.com. This is what I have, it seems to work but doesn't go to a chosen subdomain
<form>
<select name='http://domain.com' onchange='this.form.submit()'>
<option selected>Select Option</option>
<option value="coffee">Coffee</option>
<option value="tea">Tea</option>
</select>
<noscript><input type="submit" value="Submit"></noscript>
</form>
The code you posted is just HTML. There is no code there to actually do what you want.
If you wanted to do this in PHP you could try something like
<?php
if(ISSET($_POST['domainSelect']) == 'coffee'){
header('Location: http://www.domain-coffee.com');
}
if(ISSET($_POST['domainSelect']) == 'tea'){
header('Location: http://www.domain-tea.com');
}
?>
<form method="POST">
<select name='domainSelect' onchange='this.form.submit()'>
<option selected>Select Option</option>
<option value="coffee">Coffee</option>
<option value="tea">Tea</option>
</select>
<noscript><input type="submit" value="Submit"></noscript>
</form>
Or you could do it with JavaScript by doing something like
<script type='text/javascript'>
function gotodomain(){
var e = document.getElementById('domainSelectId');
var strSelected = e.options[e.selectedIndex].value;
if(strSelected == 'coffee'){
document.location.href = 'http://domain-coffee.com'
}
if(strSelected == 'tea'){
document.location.href = 'http://domain-tea.com'
}
}
</script>
<form method="POST">
<select id="domainSelectId" name='domainSelect' onchange='gotodomain()'>
<option selected>Select Option</option>
<option value="coffee">Coffee</option>
<option value="tea">Tea</option>
</select>
<noscript><input type="submit" value="Submit"></noscript>
</form>
In either case I suggest you start thinking about what it is exactly you want and in what language its best to achieve in.
<select name='car1'>
<option value='1'>Toyota</option>
<option value='2'>Honda</option>
<option value='3'>Ford</option>
</select>
<input type='submit' name='submitform' />
if(isset($_POST['submitform'])){
$Getvalue = $_POST['car1'];
echo $Getvalue;
}
How do I get the values (Toyota, Honda, and Ford) not the numbers
In POST you receive only submitted value, so you should use
<option value="Toyota">Toyota</option>
Use it.
<select name='car1'>
<option value='Toyota'>Toyota</option>
<option value='Honda'>Honda</option>
<option value='Ford'>Ford</option>
</select>
Alternatively, if you could, you could use jquery in this one. You need another container for that 'Name'. Consider this example:
<?php
if(isset($_POST['car1'])) {
$id = $_POST['car1'];
$name = $_POST['selected'];
echo $name;
}
?>
<form method="POST" action="">
<input type="hidden" name="selected" value="" />
<select name='car1'>
<option value='1'>Toyota</option>
<option value='2'>Honda</option>
<option value='3'>Ford</option>
</select>
</form>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('select[name="car1"]').change(function(){
// as you select your pick,
// this will append the name inside the hidden input
var selected = $(this).val();
var value = $('select[name="car1"] option[value="'+selected+'"]').text();
$(this).prev('input[name="selected"]').attr('value', value);
$('form').submit();
});
});
</script>
Sample Fiddle
Only option values will be send in $_POST. If you don't need values in numbers, replace them with text values.
<select name='car1'>
<option value='Toyota'>Toyota</option>
<option value='Honda'>Honda</option>
<option value='Ford'>Ford</option>
</select>
Web-browser send to web-server post line like "car1=1&submitform=". Php script does not known about what inside options. Change values to:
<select name='car1'>
<option value='Toyota'>Toyota</option>
<option value='Honda'>Honda</option>
<option value='Ford'>Ford</option>
</select>
I want to create a very simple (no plugins and basic layout) for a dropdown menu that has checkboxes for options. For example, if C and F are selected and the form is submitted, I want the url to show /index.php?category1=3&&category2=6
<form action="" method="get">
<select multiple="multiple" name="category">
<optgroup label="Category1">
<option value="1">A</option>
<option value="2">B</option>
<option value="3">C</option>
</optgroup>
<optgroup label="Category2">
<option value="4">D</option>
<option value="5">E</option>
<option value="6">F</option>
</optgroup>
</select>
<br><input type="submit" value="go">
</form>
I am not even sure how to start the jquery to pass the values to the url. Any help is much appreciated!
EDIT:
I figured I can do this using instead and simulate a dropdown with jquery.
Here is the jsfiddle: http://jsfiddle.net/k6R7g/
how can I show "2 Selected" instead of "Select..." when selections are made?
See code below, construct the url yourself and it will work. In case the form contains more values you can better add the category to the value of the option.
<form method="get">
<select multiple="multiple" name="category">
<optgroup label="Category1">
<option value="1">A</option>
<option value="2">B</option>
<option value="3">C</option>
</optgroup>
<optgroup label="Category2">
<option value="4">D</option>
<option value="5">E</option>
<option value="6">F</option>
</optgroup>
</select>
<br><input type="button" value="go" onclick="clicked();">
</form>
<script type="text/javascript">
function clicked(){
var v = "";
$('select').find(":selected").each(
function(){
if (v != ""){
v += "&";
}
v += $(this).parent()[0].label + '=' + $(this).context.value;
}
);
window.location.href = "index.php?" + v;
}
</script>
I want to display a number of textboxes in a form based on the selected option from a dropdown listbox.
For example, if user selects 1 then 1 textbox should be shown and if user selects 2 then 2 textboxes should be displayed. And I need to do it in PHP.
I found some answers using jQuery. Can we use jQuery inside PHP? If yes, then how?
Edit
#Edwin Alex
This is how my select option looks like.
<h2><u>DEPENDENT DETAILS</u></h2><br />
<table border="1" style="border-style:dotted" width="100%" id="dependenttable">
<tr><td>No of Dependent</td><td><select name="numDep" id="dropdown">
<option value="">Please Select</option>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option></select></td></tr>
<tr id="textboxDiv"></tr>
At the end of file inside <> these I have written your code.
You can use Jquery to get this. Try this,
HTML :
<tr><td>No of Dependent</td><td><select name="numDep" id="dropdown">
<option value="">Please Select</option>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option></select></td>
</tr>
<tr id="textboxDiv"></tr>
Jquery :
<script type="text/javascript">
$(document).ready(function() {
$("#dropdown").change(function() {
var selVal = $(this).val();
$("#textboxDiv").html('');
if(selVal > 0) {
for(var i = 1; i<= selVal; i++) {
$("#textboxDiv").append('<input type="text" name="textVal[]" value="" />');
}
}
});
});
</script>
Paste this code at the bottom of your page inside tag.
Your select box ID should be dropdown.
You need to have a div with an ID textboxDiv to place your generated textboxes.
I think you can do it easily with the help of JavaScript. Here is an example of it. Try it and modify it according to your requirement
<html>
<head>
<title>Select DIV to show</title>
<script type="text/javascript">
function show(obj) {
no = obj.options[obj.selectedIndex].value;
count = obj.options.length;
for(i=1;i<count;i++)
document.getElementById('myDiv'+i).style.display = 'none';
if(no>0)
document.getElementById('myDiv'+no).style.display = 'block';
}
</script>
</head>
<body>
<form name="myForm">
<select onChange="show(this)">
<option value="0">Select</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
</form>
<div id="myDiv1" style="display:none"> <form>
<select>
<option value="0">Select</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
</form></div>
<div id="myDiv2" style="display:none"><form><input type="text" ><br>
<input type="text"/><br>
<input type="submit"/></form></div>
<div id="myDiv3" style="display:none"><form><input type="text" ><br>
<input type="text"/><br>
<input type="submit"/></form></div>
</body>
</html>
In this example just change the code in "myDiv1", "myDiv2", "myDiv3". I think it will hep you.:)
I have a html form defined . I have a button for the user to select his occupation . If his occupation happens to be a student then i need to generate another new button dynamically . If the user selects anything other than a student then i dont want any new button . I am unsure how to do . Do i use jquery ? I have no idea to use jquery . Can some one help ? Any pointers
Thanks.
<?php
?>
<script language="javascript" type="text/javascript">
$(document).ready(function()
{
$('#occupation').change(function()
{
if($(this).val() == 'Student')
{
$('#school').show();
} else {
$('#school').hide();
}
});
});
</script>
<html>
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
<form>
<select name="occupation" id="occupation">
<option value="">- Select Occupation -</option>
<option value="Worker">Worker</option>
<option value="Student">Student</option>
</select>
<select name="school" id="school" style="display: none;">
<option value="">Select School Type</option>
<option value="4 Year">4 Year</option>
<option value="2 Year">2 Year</option>
</select>
</form>
</body>
</html>
Personally I accomplish this by placing the additional form element in the HTML, simply hidden like so:
<select name="occupation" id="occupation">
<option value="">- Select Occupation -</option>
<option value="Worker">Worker</option>
<option value="Student">Student</option>
</select>
<select name="school" id="school" style="display: none;">
<option value="">Select School Type</option>
<option value="4 Year">4 Year</option>
<option value="2 Year">2 Year</option>
</select>
Then you can use JS, as I prefer jQuery, to show the div or hide the div when applicable:
$(document).ready(function()
{
$('#occupation').change(function()
{
if($(this).val() == 'Student')
{
$('#school').show();
} else {
$('#school').hide();
}
});
});
Using jQuery would simplify this:
Let's say you have the following:
<form>
<select name="occupation">
<option value="Non-Student">Non-Student</option>
<option value="Student">Student</option>
</select>
</form>
Then the following would be used to append the button if the user selected 'Student':
$(function(){
$('select[name="occupation"]').change(function(){
var val = $(this).val();
if(val=='Student'){
$('form').append('<button>New Button</button>');
}
});
});