使用php将表单字段值数据传递到另一个页面

时间:2016-07-14 07:17:26

标签: php forms

我有两个PHP页面:index.php和thankyou.php。在index.php中,有一个表单。我正在使用Javascript和ajax验证表单,并将表单值插入到数据库中。在数据库查询后,我将此表单重定向到Thankyou.php。我想要的是将表单字段值传递给thankyou.php。请在下面找到完整的代码。 :

在标头中运行的SQL查询: -

    ?php


error_reporting(0);

include_once('cc/connect.php');

if($_SERVER['REQUEST_METHOD'] === 'POST')

{



$str="insert into registration(fname,lname,email,mobile_number,code,designation,organization,comps,city,affid,date_time,status)values('".mysql_escape_string($_POST['txtfname'])."','".mysql_escape_string($_POST['txtlname'])."','".mysql_escape_string($_POST['txtemail'])."','".mysql_escape_string($_POST['txtmobilenumber'])."','".mysql_escape_string($_POST['txtcode'])."','".mysql_escape_string($_POST['desig'])."','".mysql_escape_string($_POST['org'])."','".mysql_escape_string($_POST['comps'])."','".mysql_escape_string($_POST['txtcity'])."','".mysql_escape_string($_POST['txtaff'])."',now(),0)";

 $rslt=mysql_query($str);

 if(!$rslt)

 {

    echo '<script type="text/javascript">

alert("We are experiencing some issues, please try later");

</script>

';

 }

 else

 {

        echo '<script type="text/javascript">

window.location.href="thankyou.php";

</script>

';

 }

}


 ?>

Javascript验证: -

       <script type="text/javascript">

function validate_form()

{

    var pattern =/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/; 

    var mob=/^(\+91[\-\s]?)?[89]\d{9}$/;

    if(document.getElementById('txtfname').value=="" || document.getElementById('txtfname').value==null)

    {

        alert("Please enter First Name");

        document.getElementById('txtfname').focus();

        return false;

    }

    if(document.getElementById('txtlname').value=="" || document.getElementById('txtlname').value==null)

    {

        alert("Please enter Last Name");

        document.getElementById('txtlname').focus();

        return false;

    }

    if(document.getElementById('txtemail').value=="" || document.getElementById('txtemail').value==null)

    {

        alert("Please enter the Email");

        document.getElementById('txtemail').focus();

        return false;

    }


    if(!pattern.test(document.getElementById('txtemail').value))

    {

        alert("Please enter the valid Email");

        document.getElementById('txtemail').focus();

        return false;

    }

    if(document.getElementById('txtmobilenumber').value=="" || document.getElementById('txtmobilenumber').value==null)

    {

        alert("Please enter the Mobile Number");

        document.getElementById('txtmobilenumber').focus();

        return false;

    }



    if(document.getElementById('txtcode').value=="" || document.getElementById('txtcode').value==null)

    {

        alert("Please enter verification code");

        document.getElementById('txtcode').focus();

        return false;

    }else

    {

    check_existence(document.getElementById('txtcode').value,6);

    }



if(document.getElementById('comps').value=="" || document.getElementById('comps').value==null)

    {

        alert("Please enter Company strength");

        document.getElementById('comps').focus();

        return false;

    }

    if(!isNaN(document.getElementById('comps').value))

    {

        alert("Please select the valid Company strength");

        document.getElementById('comps').value='';

        document.getElementById('comps').focus();

        return false;

    }



if(document.getElementById('org').value=="" || document.getElementById('org').value==null)

    {

        alert("Please enter Organization");

        document.getElementById('org').focus();

        return false;

    }




    if(document.getElementById('txtcity').value=="" || document.getElementById('txtcity').value==null)

    {

        alert("Please enter the city");

        document.getElementById('txtcity').focus();

        return false;

    }

    if(!isNaN(document.getElementById('txtcity').value))

    {

        alert("Please enter the valid city");

        document.getElementById('txtcity').value='';

        document.getElementById('txtcity').focus();

        return false;

    }




}



function check_existence(val,caseno)

{

    var pattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;    

    var mob=/^(\+91[\-\s]?)?[789]\d{9}$/;

    var xmlhttp;

    if(caseno=="1")

    {

        if(!pattern.test(document.getElementById('txtemail').value))

        {

            alert("Please enter the valid email");

            document.getElementById('txtemail').value='';

            document.getElementById('txtemail').focus();

            return false;

        }

    }

    if(caseno=="2")

    {

        if(!mob.test(document.getElementById('txtmobilenumber').value))

        {

            alert("Please enter the valid mobile number");

            document.getElementById('txtmobilenumber').value='';

            document.getElementById('txtmobilenumber').focus();

            return false;

        }

    }





    if(caseno=="3")

    {

        if(!mob1.test(document.getElementById('txtname').value))

        {

            alert("Please enter the valid mobile number");

            document.getElementById('txtname').value='';

            document.getElementById('txtname').focus();

            return false;

        }

    }





    if(window.XMLHttpRequest)

    {

    xmlhttp=new XMLHttpRequest();

    }

    else

    {

    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")

    }

    xmlhttp.onreadystatechange=function()

    {

      if (xmlhttp.readyState==4 && xmlhttp.status==200)

      { 


            if(xmlhttp.responseText=="1")

            {

            alert("Email address already exists");

            document.getElementById('txtemail').value='';

            document.getElementById('txtemail').focus();

            }


            if(xmlhttp.responseText=="2")

            {

                alert("Verification code has been sent to your mobile");

                document.getElementById('txtcode').focus();



            }

            if(xmlhttp.responseText=="3")

            {

                document.forms["formsms"].submit();



            }

            if(xmlhttp.responseText=="4")

            {

                alert("Please enter the valid verification code");



                document.getElementById('txtcode').focus();



            }

            if(xmlhttp.responseText=="5")

            {

            alert("Mobile Number already exists");

            document.getElementById('txtmobilenumber').value='';

            document.getElementById('txtmobilenumber').focus();

            }



      }

    }



    xmlhttp.open("GET","ajax_file.php?caseno="+caseno+"&val="+val,true);

    xmlhttp.send();

}

</script>

表格代码: -

<div class="form-content">
            <form class="form-box register-form form-validator" id="formsms" name="formsms" method="post">

              <div class="form-group">
                <label>First name: <span class="required">*</span></label>
                <input class="form-control" type="text" name="txtfname" id="txtfname" required>
              </div>
              <div class="form-group">
                <label>Last name: <span class="required">*</span></label>
                <input class="form-control" type="text" name="txtlname" id="txtlname"  required>
              </div>
              <div class="form-group">
                <label>Email: <span class="required">*</span></label>
                <input class="form-control" type="email" name="txtemail" id="txtemail" onchange="return check_existence(this.value,1);" required>
              </div>


              <div class="form-group">
               <div style="float:left; width:270px;" >
                <label>Mobile: <span class="required">*</span></label>
                <input class="form-control" type="text" name="txtmobilenumber" id="txtmobilenumber" onchange="return check_existence(this.value,2);" required>
                 </div>
                <div style="float:right">
                <label>Verification Code: <span class="required">*</span></label>
                <input class="form-control" type="text" name="txtcode" id="txtcode" required>
                </div>
              </div>
              <div style="clear:both;"></div>
              <div class="form-group">
               <label>Select Graduation: <span class="required">*</span></label>
              <select class="form-control" name="comps" id="comps">
              <option>Select...</option>
              <option value="BA">BA</option>
              <option value="BBA">BBA</option>
              <option value="BCom">BCom</option>
              <option value="BSC">BSC</option>
              <option value="BTech">BTech</option>
              <option value="Other">Other</option>




            </select>


              </div>

              <div class="form-group">
                <label>Graduation%: <span class="required">*</span></label>
                <input class="form-control" type="text" name="org" id="org" required>
              </div>


               <div class="form-group">
                <label>City: <span class="required">*</span></label>
                <input class="form-control" type="text" name="txtcity" id="txtcity" required>
              </div>



              <div class="buttons-box clearfix">
              <input type="button" id="btnsubmit" name="btnsubmit" class="btn btn-default"  value="Submit" onclick="return validate_form()"/>

                <span class="required"><b>*</b> Required Field</span>
                <br>

              </div>
            </form><!-- .form-box -->
          </div>

2 个答案:

答案 0 :(得分:1)

最简单的方法是使用PHP会话。这些将存储来自与用户的一次交互的数据,以便在另一次交互中检索。

connect.php 中,添加:

session_start();

index.php 中,在数据库中验证并保存信息后,保存要在$_SESSION数组中的页面之间传递的数据

$_SESSION['fname'] = $_POST['txtfname'];
....

在您完成所有字符串操作后(例如:应用mysql_escape_string之后)将事物保存到会话实际上更好。

现在,只要用户发出另一个请求,您就可以在同一个数组中找到数据。所以在 thankyou.php

$fname = $_SESSION['fname'];
...

这是一个基本的intro to sessions

下一个注释超出了您的问题,但它是一个非常重要的一课:会话依赖于Cookie来识别用户再次访问时。这意味着精明的用户可以操纵此cookie并打破他的会话或尝试将自己呈现为其他人以绕过您的安全限制。一旦您对基础知识感到满意,请研究如何安全地使用会话!

答案 1 :(得分:0)

这听起来像$_SESSION可能派上用场的典型案例。在这种情况下(因为你正在用AJAX做事),你可能想要在你的AJAX处理PHP文件中处理会话...(header.php?)这里的方法如下:

<?php
    // FILE-NAME: header.php //<== THE AJAX PROCESSING SCRIPT

    //FIRST CHECK IF SESSION EXIST BEFORE STARTING IT:
    if (session_status() == PHP_SESSION_NONE  || session_id() == '') {
        session_start();
    }


    error_reporting(0);

    include_once('cc/connect.php');

    if($_SERVER['REQUEST_METHOD'] === 'POST') {         
        // JUST START SETTING UP THE SESSION DATA IF DATA WAS POSTED...
        $_SESSION['fname']          = htmlspecialchars(trim($_POST['txtfname']));
        $_SESSION['lname']          = htmlspecialchars(trim($_POST['txtlname']));
        $_SESSION['email']          = htmlspecialchars(trim($_POST['txtemail']));
        $_SESSION['mobile_number']  = htmlspecialchars(trim($_POST['txtmobilenumber']));
        $_SESSION['code']           = htmlspecialchars(trim($_POST['txtcode']));
        $_SESSION['designation']    = htmlspecialchars(trim($_POST['desig']));
        $_SESSION['organization']   = htmlspecialchars(trim($_POST['org']));
        $_SESSION['comps']          = htmlspecialchars(trim($_POST['comps']));
        $_SESSION['city']           = htmlspecialchars(trim($_POST['txtcity']));
        $_SESSION['affid']          = htmlspecialchars(trim($_POST['txtaff']));
        $_SESSION['date_time']      = date("Y-m-d", time());
        $_SESSION['status']         = "0";

        $str="insert into registration(fname,lname,email,mobile_number,code,designation,organization,comps,city,affid,date_time,status)values('".mysql_escape_string($_POST['txtfname'])."','".mysql_escape_string($_POST['txtlname'])."','".mysql_escape_string($_POST['txtemail'])."','".mysql_escape_string($_POST['txtmobilenumber'])."','".mysql_escape_string($_POST['txtcode'])."','".mysql_escape_string($_POST['desig'])."','".mysql_escape_string($_POST['org'])."','".mysql_escape_string($_POST['comps'])."','".mysql_escape_string($_POST['txtcity'])."','".mysql_escape_string($_POST['txtaff'])."',now(),0)";

        $rslt=mysql_query($str);

        //... THE REST OF YOUR CODE...
    }

然后,在thankyou.php内,你可以这样做:

<?php
    // FILE-NAME: thankyou.php
    //FIRST CHECK IF SESSION EXIST BEFORE STARTING IT:
    if (session_status() == PHP_SESSION_NONE  || session_id() == '') {
        session_start();
    }

    // TO GET THE EMAIL, FIRST & LAST NAMES HERE, YOU CAN SIMPLE DO LIKE SO:
    $email      = isset( $_SESSION['email'] )? $_SESSION['email'] : "";
    $firstName  = isset( $_SESSION['fname'] )? $_SESSION['fname'] : "";
    $lastName   = isset( $_SESSION['lname'] )? $_SESSION['lname'] : "";

    // ASSUMING YOU WANT TO THANK THE USER BY NAME:
    // YOU MAY DO SOMETHING LIKE SO:
    $thankYou   = "<div class='thank-you'>" . PHP_EOL;
    $thankYou  .= "<p class='appreciation'>Thank you, " ;
    $thankYou  .= "<span class='user-name'>{$firstName} {$lastName}</span>";
    $thankYou  .= " for your E-Mail... bla...bla..</p>" .PHP_EOL;
    $thankYou   = "</div>" . PHP_EOL;

    echo $thankYou;