Vineet Sharma Vineet Sharma - 4 months ago 12
PHP Question

Pass form field values data to another page with php

I have two PHP pages: index.php and thankyou.php. In index.php, there is a form. I am validating form with Javascript and ajax and the form values are being inserted into database. After database query I am redirecting this form to Thankyou.php. What i want is to pass form field values to thankyou.php. Please find below the complete code. :

Sql query running in header :-

?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 Validation :-

<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>


Form Code :-

<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>

Answer

The simplest way is to use PHP sessions. These will store data from one interaction with the user, to be retrieved on another interaction.

In connect.php, add:

session_start();

In index.php, after you've validated and saved info in the DB, save the data that you want to pass between pages in the $_SESSION array

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

It's actually better to save things to session after you've done all string manipulation (eg: after applying mysql_escape_string).

Now, whenever the user makes another request, you can find the data in that same array. So on thankyou.php

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

Here is a basic intro to sessions.

This next note goes beyond your question but it's one really important lesson: sessions rely on cookies to recognize a user when he makes another visit. This means that a savvy user can manipulate this cookie and break his session or try to present himself as someone else in order to bypass your security restrictions. Once you're comfortable with the basics, look into how to use sessions securely!