Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7923975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T17:30:31+00:00 2026-06-03T17:30:31+00:00

The javascript parameter Step should trigger a switch-case function in php. If Step is

  • 0

The javascript parameter “Step” should trigger a switch-case function in php. If Step is one than trigger this piece of code in php and return the output by JSON.

If I take a look in firebug the post string is: Step=one&inputFname=rick&inputLname=bovenkamp I think this is correct. So the problem must be in the php file and I think it’s in the $_POST part…

What am I doing wrong? Any help would be very great!

javascript code:

$(document).ready(function() {
    $("form#userForm").submit(function() {
        var inputFname = $('#inputFname').attr('value');
        var inputLname = $('#inputLname').attr('value');
            var Step = "one";
        $.ajax({
            type: "POST",
            url: "main.php",
            data: {Step: Step,inputFname: inputFname,inputLname: inputLname},
            dataType: "json",
            contentType:"application/json; charset=utf-8",
                success: function(data) {
                $("p.succesText").html(data.jsCode);
                    $("form#userForm").hide();
                $("div.success").fadeIn();
            },
            error: function(xhr, status, error) {
                $("form#userForm").hide();
                $("p.errorHead").html("Something went wrong.");
                $("p.errorText").text("ResponseText: " + xhr.responseText
                                        + "Statuscode: " + xhr.status
                                        + "ReadyState: " + xhr.readyState);
                $("div.error").fadeIn();
            }
        });
            return false;
    });
});     

PHP file:

<?php header('content-type: application/json; charset=utf-8');

    $log = array();

    $varStep = htmlspecialchars(trim($_POST["Step"]));  

    switch($varStep) {

        case "one":

        $varFname = htmlspecialchars($_POST["inputFname"]);
        $varLname = htmlspecialchars($_POST["inputLname"]);

        //Make Database connection
        $db = mysql_connect("192.168.178.254","root","852456");
        if(!$db) die("Error connecting to MySQL database.");
        mysql_select_db("Ajax" ,$db);

        //Generate code and check if code already exists in the database
        do
        {
            $varCode = rand(10000, 99999);
            $dbCheckCode = "";
            $dbCheckCode = mysql_query("SELECT * FROM TableAjax WHERE code='$varCode'");
        }
        while (mysql_fetch_array($dbCheckCode) !== false);

        //Save the Form data in the database
        $sql = "INSERT INTO TableAjax (fname, lname, code) VALUES (".PrepSQL($varFname) . ", " .PrepSQL($varLname) . ", " .PrepSQL($varCode) . ")";
        mysql_query($sql);  

        //Return code to frontend
        $log['jsCode'] = $varCode;

        break;
    }

    echo json_encode($log);


    //Clean SQL statement
    function PrepSQL($value)
    {
        if(get_magic_quotes_gpc()) 
        {
            $value = stripslashes($value);
        }
        $value = "'" . mysql_real_escape_string($value) . "'";
        return($value);
    }   

?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T17:30:32+00:00Added an answer on June 3, 2026 at 5:30 pm

    Put Step in quotes data : {"Step" : Step,....

    You are passing the value of the variable as the key in that case, that is to say you are actually passing data : {"one" : "one",.... You should do the same with inputLname and inputFname.

    Edit – Explanation

    If you look at what the contentType options does here at http://api.jquery.com/jQuery.ajax/, you will see that the default is application/x-www-form-urlencoded which is what you want. Essentially what your PHP error was indicating is that the $_POST array was empty because it did not know how to read your data due to the format. You want your return data to be json, the dataType option was all you needed.

    You still would have needed to do what I indicated in the first part of the post, but essentially you had two errors that were tripping you up.

    I hope this makes sense!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The JavaScript sort function which takes a parameter allows one to pass in a
Is this a valid url parameter in jquery.ajax(), <script type=text/javascript> $(document).ready(function() { getRecordspage(); });
i followed the ajax solr tutorial, and in step one i wrote this code.
I have a generic JavaScript function which takes one parameter function foo(val) { ...}
I try to do something like this Javascript function as a parameter to another
Through JavaScript I am appending one query parameter to the page url and I
I've noticed this parameter on the Accept request header as: Accept: application/json, text/javascript, */*;
I want pass a parameter of javascript functiona which is a string. This javascript
I am passing a parameter to a javascript function from jsp. <a href=javascript:void(0) title=Update
I have a javascript function that takes in a parameter that is given by

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.