0

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:

  • Answer1: If filled, must equal to what is in mysql
  • Checkbox1/2/3/3: Will retrieve data if one or more of these is checked. For example, if 'coffee' and 'juice' is checked, rows that have either coffee or juice set to true will be returned.
  • Dropdown1/2: Two from/to variables that specify a range

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('<','&lt;',$htmlStr); 
$xmlStr=str_replace('>','&gt;',$xmlStr); 
$xmlStr=str_replace('"','&quot;',$xmlStr); 
$xmlStr=str_replace("'",'&#39;',$xmlStr); 
$xmlStr=str_replace("&",'&amp;',$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>';

?>

1 Answer 1

1

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.

  1. Assign a name value to each form element.
  2. Use the 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'] .
Sign up to request clarification or add additional context in comments.

4 Comments

Hi Nick, I have posted a php source that I use now for a simple 3 question form. However, I'm not sure how to port these variables off into that file
Ahh, so I don't even need to use variables then ? However I just realized that I have a question where four buttons decide what the next question will be - which is why I decided to use variables. Can I somehow transfer one of the button clicks to a name value?
No offense, but you should consider getting a book on PHP/JavaScript. This is really basic stuff. You can bind a click event to a function using $.bind with jQuery.
Sorry if the question sounded too simple Nick, you're right I am really new to this - I'm currently looking stuff up on Amazon so hopefully I will get better :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.