I have a quiz management system and need to fetch the data from the database and displaying the data one by one on clicking next button.
I have used ajax to insert data into database after clicking next button by using file read and write function.
I am hoping for a better and efficient solution for my problem.
<?php
$host="localhost";
$user="root";
$pass="";
$con=mysqli_connect($host,$user,$pass);//connect to database;
mysqli_select_db($con,'*****');
// Create connection
//$conn = new mysqli($host, $user, $pass);
?>
<html>
<head>
<!--Import Google Icon Font-->
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<!--Import materialize.css-->
<link type="text/css" rel="stylesheet" href="css/materialize.min.css" media="screen,projection"/>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/js/materialize.min.js"></script>
<!--Let browser know website is optimized for mobile-->
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<style>
nav {
background-color: #2196F3 !important;
}
.second {
background-color: #61BAE4 !important;
height:45px;
}
.logo {
display: inline-block;
height: 100%;
}
.logo>img {
vertical-align: middle
padding-bottom: 10px;
}
#media only screen and (max-device-width: 480px) {
#media only screen and (max-device-width: 480px) {
.card-panel{
width:280px !important;
}
nav{
width:300px !important;
}
</style>
</head>
<body style="background-color: #969696;">
<div class="row">
<nav>
<div class="nav-wrapper">
<img width="60" height="60" src="https://www.timefortest.com/images/logo1.png" alt=""><span>IBPS Portal</span>
<i class="material-icons">menu</i>
<ul class="right hide-on-med-and-down">
<li>Dashboard</li>
<li>Candidates</li>
<li>Add Question</li>
<li><a id="logout" href="logout">Logout</a></li>
<li><a id="logout" href="logout"><i class="material-icons">lock_outline</i></a></li>
</ul>
<!--<ul class="side-nav" id="mobile-demo">
<li>Dashboard</li>
<li>Candidates</li>
<li>Add Question</li>
<li><a id="logout" href="logout">Logout</a></li>
<li><i class="material-icons">lock_outline</i></li>
</ul>-->
</div>
</nav>
</div>
<!--<nav class="second">
<div class="nav-wrapper">
<div class="col s12">
Dashboard
Add Questions
Multiple Choice
</div>
</div>
</nav>-->
<?php $var=0;?><?php
//<div id="response">
?><form method="POST" action="submitanswer.php" id="answerdata">
<?php
$myfile = fopen("hitcount.txt", "r") or die("Unable to open file!");
$fh=fread($myfile,filesize("hitcount.txt"));
fclose($myfile);
$sql="SELECT description,question,choiceA,choiceB,choiceC,choiceD,choiceE,msqid FROM master_question where msqid=$fh";
if($r=mysqli_query($con,$sql))
{
while($result= $r->fetch_row())
{
?>
<div class="col s12 m12"style="background-color:Orange;">
<div class="row">
<div class="col s6 m6">
<h4 style="color:white;">DESCRIPTION</h4>
<div class="card horizontal" style="height:500px;background-color:white;">
<div class="card-stacked">
<div class="card-content">
<h5> <?php echo $result[0]?> </h4>
</div>
</div>
</div>
</div>
<div class="col s6 m6">
<h4 style="color:white;">QUESTION</h4>
<div class="card horizontal"style="height:500px;background-color:white;">
<div class="card-stacked">
<div class="card-content">
<h5><?php echo $result[1]?></h4>
</br />
<div class="card-stacked"style="padding-top:40px;">
<div class="card-content">
<input type="hidden" name="msqid" value="<?php echo $result[7]?>"/>
<p>
<input type="radio" name="group" id="test1" name="test1" value="<?php echo $result[1]?>" />
<label for="test1"><Strong> <?php echo $result[2]?></strong></label>
</p>
<br />
<p>
<input type="radio" name="group" id="test2" name="test2" value="<?php echo $result[2]?>" />
<label for="test2"><Strong><?php echo $result[3]?></strong></label>
</p>
<br />
<p>
<input type="radio" name="group" id="test3" name="test3" value="<?php echo $result[3]?>" />
<label for="test3"><Strong><?php echo $result[4]?></strong></label>
</p>
<br />
<p>
<input type="radio" name="group" id="test4" name="test4" value="<?php echo $result[4]?>" />
<label for="test4"><Strong><?php echo $result[5]?></strong></label>
</p>
<br />
<p>
<input type="radio" name="group" id="test5" name="test5" value="<?php echo $result[5]?>" />
<label for="test5"><Strong><?php echo $result[6]?></strong></label>
</p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<?php } } ?>
<div class="col s12 m12">
<div class="row">
<div class="col s6 m6">
<center>
<input type='button'value='back' class="btn" onclick="document.location.href='decrement.php';"/>
</center>
</div>
<div class="col s6 m6 offset-s3">
<center>
<button type="submit" id="submcq" value="Submit" name="Snext" class="btn btn-success">Next</button></center>
</center>
</div>
</div>
</div>
</div>
</form>
</div>
<script type="text/javascript" src="js/materialize.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(".button-collapse").sideNav();<!--nav jquery starts-->
$(document).ready(function() {
Materialize.updateTextFields();
$(".button-collapse").sideNav();
$('select').material_select();
});
</script>
<script>
function copy() {
document.getElementById("label").innerHTML = document.getElementById("mySelect").value
}<!--to label the selected value-->
$(document).ready(function() {
$('select').material_select();
}); </script>
<script><!--ajax function to insert answer in answer table-->
$(document).ready(function(){
$("answerdata").on('submit',(function (e){
e.preventDefault();
$.ajax({
type:'POST',
url:'submitanswer.php',
data:$('#answerdata').serialize(),
contentType:true,
cache:true,
processData:true,
success:function(){
alert("worked");
}
});
}));
return false;
});
</script>
</body>
</html>
Insert page:
<?php
$host="localhost";
$user="root";
$pass="";
$con=mysqli_connect($host,$user,$pass);//connect to database;
mysqli_select_db($con,'******');
?>
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
$msqid=$_POST['msqid'];
$answer=$_POST['group'];
$sql="INSERT INTO review(question_attend,Question_id,answerA,answerB,answerC,answerD,answerE)VALUES('','$msqid','$answer','','','','')";
$query= mysqli_query($con,$sql);
if($query)
{
echo "success";
$fp = fopen('hitcount.txt', 'c+');
flock($fp, LOCK_EX);
$count = (int)fread($fp, filesize('hitcount.txt'));
ftruncate($fp, 0);
fseek($fp, 0);
fwrite($fp, $count + 1);
flock($fp, LOCK_UN);
fclose($fp);
header("Location: newfetch.php");
}
else
{
echo mysqli_error;
}
}
?>
I am having an issue that how to send the number to next page where it handles the mobile number and send a message to the user mobile number. By Using onClick method or by using ajax method can we send the message without reloading the page like below in the code.
index.php
<form method="POST" action="test.php">
<input type="text" class="form-control" name="mobile" placeholder="Mobile" maxlength="10">
<input type="text" class="form-control" name="otp" placeholder="OTP" maxlength="11" style="width: 40%; float: left;">
<input type="submit" id="btnonOtp" value="Generate OTP" class="form-control" style="background-color:#005995;width: 56.5%; float: left; margin: 0 0 0 12px; border-radius: 0;">
</form>
$('#btnonOtp').click(function()
{
$.ajax({
url: test.php,
type:'POST',
data:
{
text: mobile,
value: mobile
},
success: function(msg)
{
alert('OTP Sent');
}
});
});
Test.php
<?php
include 'send.php';
$_SESSION['smsotp'] = sms_OTP();
$num=$_POST['mobile'];
$number = $num;
//$text = 'Hi There, how are you?';
$sms_api_result = sms_Send($number, 'Please do not share the one time password with anyone. Your One Time password is: '.$_SESSION['smsotp'],$debug);
?>
If I run the test.php with mobile number passing directly then the message is delivered, but I need to pass the mobile number dynamically to $num variable.
Can any one help me where I am going wrong??
The 'data' object in your ajax isn't passing the right value.
First, add an id to the mobile tag,
<input type="text" class="form-control" id="mobile" name="mobile" placeholder="Mobile" maxlength="10">
Second, change the input type of the buttom from type="submit" to type="button". This is required because 'submit' by default does a full page submit, even if you have an click handler defined for it. [the click function also gets executed but after than the page submit happens as well.]
And then update your click function like the one below.
$('#btnonOtp').click(function()
{
$.ajax({
url: test.php,
type:'POST',
data:
{
// The key is 'mobile'. This will be the same key in $_POST[] that holds the mobile number value.
mobile: $('#mobile').val()
},
success: function(msg)
{
alert('OTP Sent');
}
});
});
This should solve your core problem.
Debugging hint: To check if the data is being passed correctly, in test.php you can do a var_dump($_POST). This will print all the key-value pairs that were received. This should help you to analyse what is usually missing.
P.S: Also, as a good practice you might want to add a validation on the Value of the mobile number field to ensure the data entered by the user indeed looks like a mobile number.
Try below code that can help you:
NOTE: Please check if mobile number is getting in $number variable or not.
<?php
include 'send.php';
$_SESSION['smsotp'] = sms_OTP();
$number = (int) $_POST['mobile'];
//$text = 'Hi There, how are you?';
$sms_api_result = sms_Send($number, 'Please do not share the one time password with anyone. Your One Time password is: '.$_SESSION['smsotp'],$debug);
?>
M.AutoInit(),$(document).ready(function(){var e=$(window).height(),t=$(".row.main-background").height();$(document).height();t<e&&$(".row.main-background").css("height","100%"),$("body").css("height",e)});var Email=localStorage.getItem("Email"),Name=localStorage.getItem("Name"),Id=localStorage.getItem("Id"),Cognito_ID=localStorage.getItem("Cognito_ID");Email&&Name&&Id&&""!=Cognito_ID&&(window.location.href="matches.html");var selectedValue,referral,referralValue=[];function myFunction(){var e=localStorage.getItem("Cognito_ID");if(null!=(t=localStorage.getItem(e+".CrickingDuelsUserInfo")))var t=localStorage.getItem(e+".CrickingDuelsUserInfo");else $("#overlayLoader").hide(),$("#verification").modal("open",{onOpenStart:function(){$("main").addClass("filter-blur"),$("footer").addClass("filter-blur")},onCloseStart:function(){$("main").removeClass("filter-blur"),$("footer").removeClass("filter-blur")}}),document.getElementById("accountVerficationStepOne").style.display="block",document.getElementById("accountReferralCode").style.display="none";var n=JSON.parse(t);if(null==n&&(client=new AWS.CognitoSyncManager,client.openOrCreateDataset("CrickingDuelsUserInfo",function(e,t){SyncData(t)})),null!=t){null==n.ReferralTaken&&($("#overlayLoader").hide(),$("#verification").modal("open",{onOpenStart:function(){$("main").addClass("filter-blur"),$("footer").addClass("filter-blur")},onCloseStart:function(){$("main").removeClass("filter-blur"),$("footer").removeClass("filter-blur")}}),document.getElementById("accountVerficationStepOne").style.display="block",document.getElementById("accountReferralCode").style.display="none");var o=n.ReferralTaken.Key,a=n.ReferralTaken.Value;if(null!=n.SelectedState){var c=n.SelectedState.Key;n.SelectedState.Value;"SelectedState"==c&&localStorage.setItem("user_state","restricted")}"ReferralTaken"==o&&"true"==a&&($("#overlayLoader").show(),window.location.href="matches.html",cognitosync("Web","LastLoginOS"))}}function isPhoneKey(e,t){var n=e.which?e.which:event.keyCode;return!(n>31&&(n<48||n>57))&&!($(t).val().length>9)}function isOTPKey(e,t){var n=e.which?e.which:event.keyCode;return!(n>31&&(n<48||n>57))&&!($(t).val().length>0)}function delayedredirect(){window.location.href="matches.html"}function cognitosync(e,t){var n=localStorage.getItem("Cognito_ID",cognito),o=localStorage.getItem("Email",o),a=localStorage.getItem("Id",a),c=localStorage.getItem("Name",c);client=new AWS.CognitoSyncManager,client.openOrCreateDataset("CrickingDuelsUserInfo",function(c,l){SyncData(l),"stateselect"==t?(l.get("SelectedState",function(e,t){}),l.put("SelectedState",e,function(e,t){}),l.get("DeviceId",function(e,t){}),l.put("DeviceId",n,function(e,t){}),l.get("Email",function(e,t){}),l.put("Email",o,function(e,t){}),l.get("PrimaryEmail",function(e,t){}),l.put("PrimaryEmail",o,function(e,t){}),l.get("UserId",function(e,t){}),l.put("UserId",a,function(e,t){}),l.get("IsFavorites",function(e,t){}),l.put("IsFavorites","true",function(e,t){}),l.get("IsLikeFB",function(e,t){}),l.put("IsLikeFB","false",function(e,t){}),l.get("IsLikeTWITTER",function(e,t){}),l.put("IsLikeTWITTER","false",function(e,t){}),l.get("IsMusicOn",function(e,t){}),l.put("IsMusicOn","false",function(e,t){}),l.get("IsPushOn",function(e,t){}),l.put("IsPushOn","false",function(e,t){}),l.get("IsShowRatecard",function(e,t){}),l.put("IsShowRatecard","false",function(e,t){}),l.get("IsShowSocialMedia",function(e,t){}),l.put("IsShowSocialMedia","false",function(e,t){}),l.get("TriviaQuestions",function(e,t){}),l.put("TriviaQuestions","0",function(e,t){}),l.get("Country",function(e,t){}),l.put("Country","India",function(e,t){}),SyncData(l)):"LastLoginOS"==t?(l.get("LastLoginOS",function(e,t){}),l.put("LastLoginOS","Web",function(e,t){}),SyncData(l)):"PhoneNo"==t?(l.get("PhoneNo",function(e,t){}),l.put("PhoneNo",e,function(e,t){}),SyncData(l)):"GotPhoneNo"==t?(l.get("GotPhoneNo",function(e,t){}),l.put("GotPhoneNo","true",function(e,t){}),SyncData(l)):"referaltaken"==t&&(l.get("ReferralTaken",function(e,t){}),l.put("ReferralTaken",e,function(e,t){}),SyncData(l),setTimeout(delayedredirect,2e3))})}window.fbAsyncInit=function(){FB.init({appId:"1437031366566616",xfbml:!0,version:"v2.10"})},function(e,t,n){var o,a=e.getElementsByTagName(t)[0];e.getElementById(n)||((o=e.createElement(t)).id=n,o.
src="https://connect.facebook.net/en_US/sdk.js",a.parentNode.insertBefore(o,a))}(document,"script","facebook-jssdk"),document.getElementById("phonenumber_button").addEventListener("click",function(){var e=$("input[name='phone']").val();localStorage.setItem("phone_number",e);var t=Math.floor(1e3+9e3*Math.random());localStorage.setItem("otp",t);var n=localStorage.getItem("Cognito_ID");$.
ajax({url:'https://ws.cricking.com/ws5/otpsms.php?value={"userid":"'+n+'", "otp":"'+t+'", "tonumber":"'+e+'", "platform":1}',type:"GET",success:function(e){0==(e=JSON.parse(e)).StatusCode&&(document.getElementById("accountVerficationStepOne").style.display="none",document.getElementById("accountVerficationStepTwo").style.display="none",document.getElementById("accountVerficationStepThree").style.display="block")}}),cognitosync(e,"PhoneNo")}),document.getElementById("resend_code").addEventListener("click",function(){var e=localStorage.getItem("phone_number"),t=localStorage.getItem("otp"),n=localStorage.getItem("Cognito_ID");$.
ajax({url:'https://ws.cricking.com/ws5/otpsms.php?value={"userid":"'+n+'", "otp":"'+t+'", "tonumber":"'+e+'", "platform":1}',type:"GET",success:function(e){0==(e=JSON.parse(e)).StatusCode&&(document.getElementById("accountVerficationStepTwo").style.display="none",document.getElementById("accountVerficationStepThree").style.display="block")}})}),document.getElementById("otp_submit").addEventListener("click",function(){var e=$("input[name='otp1']").val()+$("input[name='otp2']").val()+$("input[name='otp3']").val()+$("input[name='otp4']").val(),t=localStorage.getItem("otp"),n=localStorage.getItem("phone_number"),o=localStorage.getItem("Cognito_ID");t==e&&($.ajax({url:'https://ws.cricking.com/ws5/userregistration.php?value={"tonumber":"'+n+'","userid":"'+o+'"}',type:"GET",success:function(e){}}),cognitosync("true","GotPhoneNo"),document.getElementById("accountVerficationStepTwo").style.display="none",document.getElementById("accountVerficationStepThree").style.display="none",document.getElementById("accountVerficationStepOne").style.display="none",document.getElementById("accountReferralCode").style.display="block")}),document.getElementById("loginBtn").addEventListener("click",function(){$("#overlayLoader").show(),FB.login(function(e){if(FB.api("/me",{fields:"email,name"},function(e){localStorage.setItem("Email",e.email),localStorage.setItem("Name",e.name),localStorage.setItem("Id",e.id);localStorage.getItem("Email"),localStorage.getItem("Name"),localStorage.getItem("Id"),localStorage.getItem("Cognito_ID")}),e.authResponse){setTimeout(myFunction,5e3);var t=e.authResponse.accessToken;if(localStorage.setItem("accesstoken",t),null!=t){var n={IdentityPoolId:"us-east-1:8017b399-3a23-45d6-8407-7271cef062ef",Logins:{"graph.facebook.com":t}};AWS.config.region="us-east-1",AWS.config.credentials=new AWS.CognitoIdentityCredentials(n),AWS.config.credentials.get(function(e,t){AWS.config.credentials.get(function(){AWS.config.credentials.identityId;client=new AWS.CognitoSyncManager,client.openOrCreateDataset("CrickingDuelsUserInfo",function(e,t){SyncData(t),t.getAllRecords(function(e,t){var n=t.length;0==t.length&&client.openOrCreateDataset("CrickingDuelsUserInfo",function(e,t){SyncData(t),t.getAllRecords(function(e,t){})});var o=localStorage.getItem("_internal._metadata"),a=JSON.stringify(o);JSON.parse(a);if(t.length>1){for(var c=0;c<n;c++)if("ReferralTaken"==t[c].key){referralValue={userval:t[c].value};break}localStorage.setItem("userVal",referralValue.userval)}})}),cognito=AWS.config.credentials.identityId,localStorage.setItem("Cognito_ID",cognito)})})}}})}),$("#select_state").click(function(){var e=document.getElementById("user_state");e=$("#user_state option:selected").val();if(""==(selectedValue=e))return M.toast({html:"Select a state"}),!1;"Assam"==selectedValue?(cognitosync("true","referaltaken"),cognitosync(selectedValue,"stateselect"),document.getElementById("accountReferralCode").style.display="none",document.getElementById("accountVerficationStepOne").style.display="none"):(cognitosync(selectedValue,"stateselect"),document.getElementById("accountVerficationStepTwo").style.display="block",document.getElementById("accountReferralCode").style.display="none",document.getElementById("accountVerficationStepOne").style.display="none")}),$("#final_refer").click(function(){var e=document.getElementById("refer_code").value,t=localStorage.getItem("Cognito_ID"),n=localStorage.getItem("Email"),o=localStorage.getItem("Name");$.getJSON('https://ws.cricking.com/ws4/?action=playerreferal&value={"UserId":"'+t+'","EmailId":"'+n+'","Code":"'+e+'","UserName":"'+o+'"}').success(function(e){if("0"==e.StatusCode)M.toast({html:"Congrats! Your referral code is valid. Take control! Join Hybrid Fantasy and enjoy Cricking Duels."}),cognitosync("true","referaltaken"),cognitosync("Web","LastLoginOS"),$("#overlayLoader").show();else{if("1"==e.StatusCode)return M.toast({html:"Oops! You entered an invalid referral code. Please check and re-enter the valid referral code."}),!1;if("2"==e.StatusCode)return M.toast({html:"Oops! You entered an invalid referral code. Please check and re-enter the valid referral code."}),!1}})}),$("#skip_refer_code").click(function(){cognitosync("true","referaltaken"),
cognitosync("Web","LastLoginOS"),$("#overlayLoader").show()});
<!DOCTYPE html>
<html>
<head>
<title>CrickingDuels</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://sdk.amazonaws.com/js/aws-sdk-2.4.14.min.js"></script>
<script src="js\cognito\dist\amazon-cognito.min.js"></script>
<link href="css\materialize.min.css" rel="stylesheet">
<link href="css\fontawesome\css\font-awesome.min.css" rel="stylesheet">
<link href="css\common.css" rel="stylesheet">
<link href="css\login.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Roboto|Roboto+Condensed|Rubik" rel="stylesheet">
<script src="https://use.typekit.net/wmk2lex.js"></script>
<script>try{Typekit.load({ async: true });}catch(e){}</script>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async="" src="https://www.googletagmanager.com/gtag/js?id=UA-54357006-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-54357006-1');
</script>
</head>
<body class="white">
<main>
<div class="row main-background">
<div class="">
<div class="col s12 m12 l12 relative" style="padding: 50px 10%;">
<div class="">
<div class="row no-margin">
<div class="col s12 m12 l12">
<h3 class="blue-text text-darken-4 margin-5-0 bold">Let's</h3>
<h3 class="blue-text text-darken-4 margin-5-0 bold">Play</h3>
<h3 class="blue-text text-darken-4 margin-5-0 bold">Fantasy!</h3>
</div>
</div>
<div class="row margin-10-0" style="margin-bottom: 30% !important;">
<div class="col s12 m12 l12">
<p class="font-16 blue-text text-darken-4 margin-5-0 bold">Play fantasy</p>
<p class="font-16 blue-text text-darken-4 margin-5-0 bold">& win cash daily!</p>
</div>
</div>
<div class="row no-margin" style="position: absolute;bottom: 0;left: 0;width: 100%;padding: 0 20px;">
<div class="col s12 m12 l12">
<div class="row add-amount no-margin">
<div class="col s12 m6 l6 no-padding push-l3 push-m3 center-align">
<button id="loginBtn" class="btn btn-flat blue darken-3 white-text width-100" style="text-transform: none;"><i class="fa fa-facebook white-text font-14"></i> Sign in with Facebook</button>
</div>
</div>
</div>
</div>
</div>
<div style="margin-bottom: 0;"></div>
</div>
</div>
</div>
</main>
<div class="modal" id="verification">
<div class="modal-content blue">
<div class="row no-margin" id="accountVerficationStepOne">
<div class="col s12 m12 l12 no-padding center-align">
<div class="img-block">
<img src="images\state_selection.png" class="img responsive-img img-center" width="150">
</div>
<!-- <p class="uppercase blue-text text-darken-4 bold">State Selection</p> -->
<p class="uppercase blue-text text-darken-4">Residents of the states of Assam and Odisha, and where otherwise prohibited by law are not eligible to enter pay leagues, you can play free Leagues</p>
<div class="form-group" style="padding-top: 30px;">
<select class="" name="state" id="user_state">
<option value="">--Select a state--</option>
<option value="Andaman and Nicobar">Andaman and Nicobar</option>
<option value="Andhra Pradesh">Andhra Pradesh</option>
<option value="Arunachal Pradesh">Arunachal Pradesh</option>
<option value="Assam">Assam</option>
<option value="Bihar">Bihar</option>
<option value="Chandigarh">Chandigarh</option>
<option value="Chhattisgarh">Chhattisgarh</option>
<option value="Delhi">Delhi</option>
<option value="Dadra and Nagar Haveli">Dadra and Nagar Haveli</option>
<option value="Daman and Diu">Daman and Diu</option>
<option value="Goa">Goa</option>
<option value="Gujarat">Gujarat</option>
<option value="Haryana">Haryana</option>
<option value="Himachal Pradesh">Himachal Pradesh</option>
<option value="Jammu and Kashmir">Jammu and Kashmir</option>
<option value="Jharkhand">Jharkhand</option>
<option value="Karnataka">Karnataka</option>
<option value="Kerala">Kerala</option>
<option value="Lakshadweep">Lakshadweep</option>
<option value="Madhya Pradesh">Madhya Pradesh</option>
<option value="Maharashtra">Maharashtra</option>
<option value="Manipur">Manipur</option>
<option value="Meghalaya">Meghalaya</option>
<option value="Mizoram">Mizoram</option>
<option value="Nagaland">Nagaland</option>
<option value="Odisha">Odisha</option>
<option value="Punjab">Punjab</option>
<option value="Puducherry">Puducherry</option>
<option value="Rajasthan">Rajasthan</option>
<option value="Sikkim">Sikkim</option>
<option value="Tamil Nadu">Tamil Nadu</option>
<option value="Telangana">Telangana</option>
<option value="Tripura">Tripura</option>
<option value="Uttarakhand">Uttarakhand</option>
<option value="Uttar pradesh">Uttar Pradesh</option>
<option value="West bengal">West bengal</option>
</select>
<button class="btn btn-flat gradient-background white-text margin-10-0" id="select_state">Next</button>
</div>
</div>
</div>
<div class="row no-margin" id="accountVerficationStepTwo" style="display: none;">
<div class="col s12 m12 l12 no-padding center-align">
<div class="img-block">
<img src="images\mobile_number.png" class="img responsive-img img-center" width="150">
</div>
<p class="uppercase blue-text text-darken-4 bold">Mobile Verification</p>
<p class="uppercase blue-text text-darken-4">Please enter your mobile number</p>
<div class="form-group" style="padding-top: 30px;">
<input type="text" name="phone" id="phone" class="browser-default" onkeypress="return isPhoneKey(event, this)" placeholder="+91">
<button class="btn btn-flat gradient-background white-text margin-10-0" id="phonenumber_button">Next</button>
</div>
</div>
</div>
<div class="row no-margin" id="accountVerficationStepThree" style="display: none;">
<div class="col s12 m12 l12 no-padding">
<div class="img-block">
<img src="images\sms_otp.png" class="img responsive-img img-center" width="150">
</div>
<p class="uppercase blue-text text-darken-4 bold center-align">Mobile Verification</p>
<p class="uppercase blue-text text-darken-4 center-align">Please enter the OTP you have received on your registered mobile number</p>
<div class="form-group center-align" style="padding-top: 30px;margin: 0 auto;">
<input type="text" name="otp1" id="otp1" class="browser-default inline center-align" style="width: 16%; padding: 0 5px;" onkeypress="return isOTPKey(event,this)">
<input type="text" name="otp2" id="otp2" class="browser-default inline center-align" style="width: 16%; padding: 0 5px;" onkeypress="return isOTPKey(event,this)">
<input type="text" name="otp3" id="otp3" class="browser-default inline center-align" style="width: 16%; padding: 0 5px;" onkeypress="return isOTPKey(event,this)">
<input type="text" name="otp4" id="otp4" class="browser-default inline center-align" style="width: 16%; padding: 0 5px;" onkeypress="return isOTPKey(event,this)">
</div>
<div class="form-group center-align resend_code">
Resend code
</div>
<div class="form-group center-align">
<button class="btn btn-flat gradient-background white-text margin-10-0" id="otp_submit">Next</button>
</div>
</div>
</div>
<div class="row no-margin" id="accountReferralCode" style="display: none;">
<div class="col s12 m12 l12 no-padding">
<div class="img-block">
<img src="images\sms_otp.png" class="img responsive-img img-center" width="150">
</div>
<p class="uppercase blue-text text-darken-4 bold center-align">Referral code</p>
<p class="uppercase blue-text text-darken-4 center-align">We want to give you a head start. If you have a referral code, enter it below and we will load ₹ 89 in your wallet to join the leagues.</p>
<div class="form-group center-align" style="padding-top: 30px;margin: 0 auto;">
<input type="text" name="refer_code" id="refer_code">
</div>
<div class="form-group center-align resend_code">
Skip
</div>
<div class="form-group center-align">
<button class="btn btn-flat gradient-background white-text margin-10-0" id="final_refer">Submit</button>
</div>
</div>
</div>
</div>
</div>
<div id="overlayLoader" style="display: none;">
<div class="loader">
<div class="progress thin-4">
<div class="indeterminate"></div>
</div>
</div>
</div>
</body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript" src="js\materialize.min.js"></script>
<!-- <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.min.js" integrity="sha256-rsPUGdUPBXgalvIj4YKJrrUlmLXbOb6Cp7cdxn1qeUc=" crossorigin="anonymous"></script> -->
<!-- <script src="js/dust-full.min.js"></script>
<script src="js/dust-helpers.min.js"></script> -->
<script src="js\cognitosync.js"></script>
<script src="js\login.js"></script>
<!-- <script type="text/javascript" src="js/javascript.js"></script> -->
</html>
I am trying to insert my form data into mysql, but when I enter the details and submit it, I am not able to get the data in database. May I know where am I wrong?
<?php
// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
// Report all PHP errors (see changelog)
error_reporting(E_ALL);
echo "jgj"
$servername = "localhost";
$username = "username";
$password = "";
$dbname = "sudhar";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
else
{
echo "connected"
}
if(isset($_POST['submit']))
{
$Firstname=mysqli_real_escape_string($conn,$_POST['Firstname']);
$Lastname=mysqli_real_escape_string($conn,$_POST['Lastname']);
$Username=mysqli_real_escape_string($conn,$_POST['Username']);
$email=mysqli_real_escape_string($conn,$_POST['email']);
$Password=mysqli_real_escape_string($conn,$_POST['Password']);
$sql="insert into useraccount(Firstname,Lastname,Username,email,Password)values('$Firstname','$Lastname','$Username','$Password','$email')";
$result=mysqli_query($conn,$sql)
echo "data inserted"
or die(mysql_error());
}
?>
html,body
{
margin:0px;
height:100%;
}
.carousel-inner > .item > img,
.carousel-inner > .item > a > img {
width: 60%;
margin: auto;
}
.content
{
width:100%;
height:400px;
}
.signup
{
height:500px;
}
.footer
{
position:relative;
background-color:black;
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>ASK</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="boot.css">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="jumbotron">
<div class="container">
<div class="row">
<div class="col-md-6">
<h1>school name</h1>
</div>
<div class="col-md-6">
<img src="../project/photo/l.png" height="150px"/>
</div>
</div>
</div>
</div>
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#mynavbar">schoolName</a>
</div>
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav">
<li class="active">Home</li>
<li><a href="about.html" target=_self>About Us</a></li>
<li><a href="infra.html" target=_self>Infrastructure</a></li>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="">Administration<span class="caret"></span></a>
<ul class="dropdown-menu">
<li>staff login</li>
<li>staff details</li>
<li>class handling</li>
</ul>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><span class="glyphicon glyphicon-user"></span> Sign Up</li>
<li><span class="glyphicon glyphicon-log-in"></span> Login</li>
</ul>
</div>
</div>
</nav>
<div class="container">
<div class="signup">
<form method="post" class="form-horizontal" action="user.php">
<div class="form-group">
<div class="col-xs-3">
<label for="Firstname">Firstname:</label>
<input type="text" name="Firstname" class="form-control" id="fnmae" placeholder="Enter Firstname">
</div></div>
<div class="form-group">
<div class="col-xs-3">
<label for="Lastname">Lastname:</label>
<input type="text" name="Lastname" class="form-control" id="lnmae" placeholder="Enter Lastname">
</div></div>
<div class="form-group">
<div class="col-xs-3">
<label for="Username">Username:</label>
<input type="text" name="Username" class="form-control" id="unmae" placeholder="Enter Username">
</div></div>
<div class="form-group">
<div class="col-xs-3">
<label for="email">Email:</label>
<input type="email" name="email" class="form-control" id="email" placeholder="Enter email">
</div></div>
<div class="form-group">
<div class="col-xs-3">
<label for="pwd">Password:</label>
<input type="Password" name="Password" class="form-control" id="pwd" placeholder="Enter password">
</div></div>
<button type="submit" class="btn btn-default">Submit</button><br>
</form>
</div>
</div>
<div class="footer nav">
<p> Copyrights# © WWW.schools.com</p>
</div>
</body>
</html>
$query=mysqli_query($con,"insert into useraccount('Firstname','Lastname','Username','email','Password')values($Firstname,$Lastname,$Username,$email,$Password)");
and change button name
If regardless to other errors, and if you just guess how to have your query run:
There are some reserved naming convention, and maybe your code could works in some situation, put column names between quotation marks.
INSERT into useraccount
('Firstname','Lastname','Username','email','Password')
VALUES
($Firstname,$Lastname,$Username,$Password,$email);
I suggest you escape characters in variables you put into query.
This is a valuable article for you:
http://php.net/manual/en/function.mysql-real-escape-string.php
But: Do not use this solution for production, as using login credential while learning the code is the worst possible way.
change
<button type="submit" class="btn btn-default">Submit</button><br>
to
<button type="submit" name="submit class="btn btn-default">Submit</button><br>
because $_POST['submit'] is not set since you forgot to set the name