Reputation: 7805
I made a simple (yet unconventional) form which stores each answer as a variable (I did it this way because some questions won't be answered because some questions showing up depend on what you answered on the previous question).
I would like to send that off to a php file which will search if there's a match in a Mysql database, and return it in an xml format.
Here are the variables:
Here is my code :) http://jsfiddle.net/pufamuf/S6smg/1/
HTML:
<div id="que1">Question One:
<input id="inp1" type="text">
<input type="button" value="Next Question" id="but1"></div>
<div id="que2" class="hiddendiv">Question Two:<br>
<input type="checkbox" id="checkbox1" value="coffee">Coffee<br>
<input type="checkbox" id="checkbox2" value="tea">Tea<br>
<input type="checkbox" id="checkbox3" value="latte">Latte<br>
<input type="checkbox" id="checkbox4" value="juice">Juice<br>
<input type="button" value="Next Question" id="but2"></div>
<div id="que3" class="hiddendiv">Question Three:
From:
<select id="dropdown1">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
To:
<select id="dropdown2">
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<input type="button" value="Finish" id="but3"></div>
<br><br><br>
<b>Variables</b><br>
Answer1:<div id="answer1"></div>
Checkbox1:<div id="answer2a"></div>
Checkbox2:<div id="answer2b"></div>
Checkbox3:<div id="answer2c"></div>
Checkbox4:<div id="answer2d"></div>
Dropdown1:<div id="answer3a"></div>
Dropdown2:<div id="answer3b"></div>
Jquery:
$("input[type='button']").click(function() {
var question1 = '';
var question2a = '';
var question2b = '';
var question2c = '';
var question2d = '';
var question3a = '';
var question3b = '';
switch (this.id) {
case 'but1':
question1 = $("#inp1").val();
$("#answer1").html(question1);
$('#que1').addClass('hiddendiv');
$('#que2').removeClass('hiddendiv');
break;
case 'but2':
if($('#checkbox1').is(':checked')){
question2a = '1';
}
if($('#checkbox2').is(':checked')){
question2b = '1';
}
if($('#checkbox3').is(':checked')){
question2c = '1';
}
if($('#checkbox4').is(':checked')){
question2d = '1';
}
$("#answer2a").html(question2a);
$("#answer2b").html(question2b);
$("#answer2c").html(question2c);
$("#answer2d").html(question2d);
$('#que2').addClass('hiddendiv');
$('#que3').removeClass('hiddendiv');
break;
case 'but3':
question3a = $("#dropdown1").val();
question3b = $("#dropdown2").val();
$('#que3').addClass('hiddendiv');
$("#answer3a").html(question3a);
$("#answer3b").html(question3b);
break;
}
});
Thank you very much everyone :)))!
Here's what the php file looks like now for my older html form:
<?php
require("db_access.php");
function parseToXML($htmlStr)
{
$xmlStr=str_replace('<','<',$htmlStr);
$xmlStr=str_replace('>','>',$xmlStr);
$xmlStr=str_replace('"','"',$xmlStr);
$xmlStr=str_replace("'",''',$xmlStr);
$xmlStr=str_replace("&",'&',$xmlStr);
return $xmlStr;
}
$name=$_POST['name'];
$address=$_POST['address'];
$type=$_POST['type'];
// Opens a connection to a MySQL server
$connection=mysql_connect (localhost, $username, $password);
if (!$connection) {
die('Not connected : ' . mysql_error());
}
// Set the active MySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}
// Select all the rows in the markers table
$inputs = array('name', 'address', 'type');
$where = array();
foreach($inputs as $input)
{
if(!empty($_POST[$input])) {
$where[] = "{$input} = '" . mysql_real_escape_string($_POST[$input]) . "'";
}
}
if ($where) {
$query = 'SELECT * FROM markers WHERE ' . implode(' AND ', $where);
} else {
user_error("No rows returned by:<br />\n$query");
}
$result = mysql_query($query);
if($result == false) {
die(mysql_error() . "<br />\n$query");
}
if(mysql_num_rows($result) == 0) {
user_error("No rows returned by:<br />\n$query");
}
header("Content-type: text/xml");
// Start XML file, echo parent node
echo '<markers>';
// Iterate through the rows, printing XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
echo '<marker ';
echo 'name="' . parseToXML($row['name']) . '" ';
echo 'address="' . parseToXML($row['address']) . '" ';
echo 'type="' . parseToXML($row['type']) . '" ';
echo 'lat="' . $row['lat'] . '" ';
echo 'lng="' . $row['lng'] . '" ';
echo '/>';
}
// End XML file
echo '</markers>';
?>
Upvotes: 0
Views: 416
Reputation: 16345
You need to make an HTTP request to the PHP page, and handle the form data on the server side. Depending on the form type, it will be in either $_GET
or $_POST
.
If you want to do it using AJAX, you can use a form plugin or do it manually by collecting the data and submitting it with $.get
.
name
value to each form element.name
to pull the form data from the, for example, if the field is named foo
and you're requesting data with POST, you would use $_POST['foo']
.Upvotes: 1