doumowu7371 2018-02-03 16:11
浏览 107

Post有请求有限数量的参数吗?

I'm trying to send to my server 5 parameters:

  1. Action: will contain the name of the form, in this case "signin"
  2. Name: Name of the person who wants to signin
  3. Surname: Surname of the person who wants to signin
  4. Email: Email of the person who wants to signin
  5. Password: Password of the person who wants to signin

the problem is that my server reads only 4 parameters: Name, Surname, Email and Password, and it don't see Action!

Here's the code:

Javascript:

function signin() {
    alert("OK");
    var action = $(this).attr('name'); // puts in action the name of the form (this case "signin")
    $.ajax({
        type: "POST",
        url: "submit.php",
        data: {
            Action: action, // the server don't see it!!
            Name: document.getElementById('signin-name').value, // Name in the form
            Surname: document.getElementById('signin-surname').value, // // Surname in the form
            Email: document.getElementById('singin-email').value, // Email in the form
            Password: document.getElementById('singin-password').value // // Password in the form
        },
        cache: false,
        success: function() {
            alert("success");
            window.location.href = "index.php"; // load the index.php page, which contains the login form
        }
    });
}

PHP - Signin.php:

<!-- Signin Form -->

<?php
require('include/header.php');
?>

<div class="limiter">
    <div class="form-container">
        <div class="form-wrap">
            <form action="submit.php" method="post" name="form-signin" id="form-signin" autocomplete="off">

                <span class="form-title">Registration form</span>

                <div class="form-field">
                    <label for="Name">Name</label>
                    <input type="text" name="Name" id="signin-name" class="form-control" required pattern=".{1,100}" autofocus>
                </div>
                <div class="form-field">
                    <label for="Surname">Surname</label>
                    <input type="text" name="Surname" id="signin-surname" class="form-control" required pattern=".{1,100}" autofocus>
                </div>
                <div class="form-field">
                    <label for="email">Email address</label>
                    <input type="email" name="Email" id="signin-email" class="form-control" required>
                </div>
                <div class="form-field">
                    <label for="Password">New password</label>
                    <input type="password" name="Password" id="signin-password" placeholder="Almeno 6 caratteri" class="form-control">
                </div>
                <div id="display-error" class="alert alert-danger fade in"></div><!-- Display Error Container -->



                <div class="form-submit-container">
                    <div class="form-submit-wrap">
                        <button class="form-cancel-button" type="submit">Cancel</button>
                        <button class="form-submit-button" type="submit" onclick="signin()">Signin</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

<?php require('include/footer.php');?>

PHP - Submit.php:

<?php

#Detect AJAX and POST request, if is empty exit
if((empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') or empty($_POST)){
    exit("Unauthorized Acces");
}

require('inc/config.php');
require('inc/functions.php');

# Check if Login form is submitted
if(!empty($_POST) && $_POST['Action'] === 'form-login'){

    # Define return variable. for further details see "output" function in functions.php
    $Return = array('result'=>array(), 'error'=>'');

    $email = $_POST['Email'];
    $password = $_POST['Password'];

    /* Server side PHP input validation */
    if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $Return['error'] = "Please enter a valid Email address.";
    } else if($password === '') {
        $Return['error'] = "Please enter Password.";
    }
    if($Return['error']!='') {
        output($Return);
    }

    # Checking Email and Password existence in DB

    # Selecting the email address of the user with the correct login credentials.
    $query = $db->query("SELECT Email FROM USERS WHERE Email='$email' AND Password='$password'");
    $result = $query->fetch(PDO::FETCH_ASSOC);

    if($query->rowCount() == 1) {
        # Success: Set session variables and redirect to Protected page
        $Return['result'] = $_SESSION['UserData'] = $result;
    } else {
        # Failure: Set error message
        $Return['error'] = 'Invalid Login Credential.';
    }

    output($Return);
}

# Check if Registration form is submitted
if(!empty($_POST) && $_POST['Action'] === 'form-signin') {

    # Define return variable. for further details see "output" function in functions.php
    $Return = array('result'=>array(), 'error'=>'');

    $name = $_POST['Name'];
    $surname = $_POST['Surname'];
    $email = $_POST['Email'];
    $password = $_POST['Password'];

    # Server side PHP input validation
    if($name === '') {
        $Return['error'] = "Please enter Full name.";
    } else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $Return['error'] = "Please enter a valid Email address.";
    } else if($password === '') {
        $Return['error'] = "Please enter Password.";
    }
    if($Return['error']!='') {
        output($Return);
    }

    # Check Email existence in DB
    $result = $db->query("SELECT Email FROM USERS WHERE Name='$name' AND Surname='$surname' AND Email='$email'");
    if($result->rowCount() == 1){
        # Email already exists: Set error message
        $Return['error'] = 'You have already registered with us, please login.';
    }else{
        # Insert the new user data inside the DB
        try{
            $db->query("INSERT INTO `users` (`ID_user`, `Name`, `Surname`, `Email`, `Password`) VALUES (NULL, '$name', '$surname', '$email', '$password')");
        }
        catch (PDOException $e) {
            echo $e->getMessage();
        }

        # Success: Set session variables and redirect to Protected page
        $Return['result'] = $_SESSION['UserData'] = $result;
    }

    output($Return);
}

PHP - Functions.php

# Function to set JSON output
function output($Return=array()){
    header('Content-Type: application/json; charset=UTF-8');
    #exit(json_encode($Return)); # Final JSON response
    echo json_encode($Return);
}

here is a screenshot of the debugger: Debug Screenshot

  • 写回答

2条回答 默认 最新

  • dongpi9480 2018-02-03 16:17
    关注

    function signin() {
        alert("OK");
        var action = $('#form-signin').attr('name'); // puts in action the name of the form (this case "signin")
        
      //  alert(action);
        $.ajax({
            type: "POST",
            url: "submit.php",
            data: {
                Action: action, // the server don't see it!!
                Name: $('signin-name').val(), // Name in the form
                Surname: $('signin-surname').val(), // // Surname in the form
                Email: $('singin-email').val(), // Email in the form
                Password: $('singin-password').val() // // Password in the form
            },
            cache: false,
            success: function() {
                alert("success");
                window.location.href = "index.php"; // load the index.php page, which contains the login form
            }
        });
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="limiter">
        <div class="form-container">
            <div class="form-wrap">
                <form action="submit.php" method="post" name="form-signin" id="form-signin" autocomplete="off">
    
                    <span class="form-title">Registration form</span>
    
                    <div class="form-field">
                        <label for="Name">Name</label>
                        <input type="text" name="Name" id="signin-name" class="form-control" required pattern=".{1,100}" autofocus>
                    </div>
                    <div class="form-field">
                        <label for="Surname">Surname</label>
                        <input type="text" name="Surname" id="signin-surname" class="form-control" required pattern=".{1,100}" autofocus>
                    </div>
                    <div class="form-field">
                        <label for="email">Email address</label>
                        <input type="email" name="Email" id="signin-email" class="form-control" required>
                    </div>
                    <div class="form-field">
                        <label for="Password">New password</label>
                        <input type="password" name="Password" id="signin-password" placeholder="Almeno 6 caratteri" class="form-control">
                    </div>
                    <div id="display-error" class="alert alert-danger fade in"></div><!-- Display Error Container -->
    
    
    
                    <div class="form-submit-container">
                        <div class="form-submit-wrap">
                            <button class="form-cancel-button" type="submit">Cancel</button>
                            <button class="form-submit-button" type="submit" onclick="signin()">Signin</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数