Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am working on a script that echoes an answer based on a selected value. This will be multiple values later on but for now i'm just testing. The php script is called trough AJAX so the results will show on the same page.

The only problem is that my variable $brand in this case isn't passed so the script will always return my else statement. I have included the variable in the echo to check wether it was passed, which it isn't. What is going wrong here?

this is my index file code:

<?php
session_start();
?>
<html>
<head>
<title>Test</title>
<link rel="stylesheet" type="text/css" href="css/style.css" />
<script type="text/javascript" src="JS/jquery-1.10.2.js" ></script>
<script>
        function myCall(){
            var request = $.ajax({
                url: "process.php",
                type: "post",
                dataType: "html"
            });

            request.done(function(msg) {
                $("#response").html(msg);
            });

            request.fail(function(jqXHR, textStatus) {
                alert( "Request failed: " + textStatus );
            });
        }
</script>
</head>

<body>
<div id="formulier">
<form method="" action="" name="form" id="form">
    <label for="brand">Brand</label>
    <select id="brand" name="brand">
        <option value="" >- Select -</option>
        <option value="1">Marlboro (1)</option>
        <option value="2">Pall Mall (2)</option>
    </select>

    <input type="button" value="submit" onclick="myCall();">
</form>
</div>

<div id="response">
</div>

This is my php file (process.php):

<?php
session_start();

$brand = $_POST['brand'];

if( $brand == '1'){
    echo "Value is 1";
}

else{
    echo "Value is: ".$brand;
}
?>
share|improve this question
 
I'm not seeing any references to $_SESSION? Assuming you're simply talking about post data here... –  Scuzzy Dec 1 at 23:28
 
in your jQuery ajax request i dont see any data being sent. you specified a datatype but never said what to send. –  Moussa Harajli Dec 1 at 23:57
add comment

4 Answers

up vote 0 down vote accepted

I see several problems in your script:

  • at no point you reference the actual fields of your form
  • I'd rather put the handlers inside the ajax call

Try this:

    function myCall(){
        var request = $.ajax({
            url: "process.php",
            type: "post",
            dataType: "html",
            data: $('#form').serialize(), //here you send the form fields
            success: function(msg) {
                $("#response").html(msg);
            }, 
            error: function(jqXHR, textStatus) {
                alert("Request failed: " + textStatus);
            }
         }
    }
share|improve this answer
 
This fixed it, i was struggling with the use of 'data' and couldn't find the right way to fit it in the code so I just left it out. I'm just starting to figure out jQuery and ajax, learned something today. Thanks! –  user3055582 Dec 1 at 23:59
 
Glad to hear it ;) –  Samy Dec 2 at 8:00
add comment

You should use json_encode (http://us1.php.net/json_encode) for responding in process.php.

share|improve this answer
 
This is one step forward. Now it is still returning my 'else' statement when selecting value "1" which should echo "Value is 1" and the else statement says Value is: null so apparently my post value is null while it should be 1 or 2... –  user3055582 Dec 1 at 23:39
add comment

Make sure you set the session variable and read it later (in your php else block), e.g., $_SESSION["brand"] = "2";

share|improve this answer
add comment

You should try the define the form attributes

<form method="POST" action="" name="form" id="form">
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.