laravel ajax 500 internal server error - php

after changing my AJAX from GET method to POST method,
I got this internal server error 500 every time the JavaScript runs.
And I really cant find anything wrong.
Im using laravel 4.2 on windows.
JavaScript function on keyup:
function usernameCheck()
{
var input = document.getElementById("usernameInput");
var icon = document.getElementById("userIcon");
var xmlhttp,
username = document.getElementById("usernameInput"),
message = document.getElementById("usernameMessage");
if(username.value != "")
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
message.innerText = "";
icon.style.color = xmlhttp.responseText;
input.style.border = xmlhttp.responseText+" solid 2px";
if(username.value.length < 4)
{
message.innerText = "username is too short";
message.style.color = "red";
}
else if(xmlhttp.responseText == "false")
{
message.innerText = "username is already in use";
message.style.color = "red";
}
else if(xmlhttp.responseText == "true")
{
message.innerText = "username is available";
message.style.color = "green";
}
}
}
xmlhttp.open("POST", "usernamevalidation", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form- urlencoded");
xmlhttp.send("username="+ username.value);
}
else
{
icon.style.color = "";
input.style.border = "";
message.innerText = "";
}
}
Route.php:
Route::post('usernamevalidation', 'UserController#validateUsername');

Related

My register button cant submit but the login is ok in PHP+AJAX

i am having issue with my registration page in PHP and AJAX, the login section seems to be working fine because i can sign in, but on the registeration button looks disable or dead, cant submit the form.
i think i make a mistake in some part so am unable to figure out where the problem can be.
Login:
this is the login code that can be submit in the login section.
<div class="form-group">
<div class="col-sm-offset-3 col-sm-9">
<button type="button" class="btn btn-block btn-primary" id="loginBtn">Sign In </button>
</div>
</div>
<?php require_once("inc/jscript_section.php"); ?>
<script type="text/javascript">
document.getElementById("loginBtn").addEventListener("click", submitLogin);
function submitLogin(){
var username = password = '';
username = document.getElementById("usern").value;
password = document.getElementById("pswd").value;
if ((username === '' || password === '')){
window.location.assign("sign-in.php?m="+'None of the Fields must be empty!');
} else{
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
if (this.responseText === '1'){
window.location.assign("dashboard/index.php");
}else{
window.location.assign("sign-in.php?m="+'Login Failed!');
}
}
};
xmlhttp.open("POST","dashboard/ajax/submitlogin.php",true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("password="+password+"&username="+username);
document.getElementById("loginBtn").disabled = true;
}
}
</script>
Register:
this is the register code that don't submit but look disable or have a dead link.
<div class="form-group">
<div class="col-sm-offset-3 col-sm-9">
<button type="submit" class="btn btn-block btn-primary" id="regBtn">Sign Up</button>
</div>
</div>
<?php require_once("inc/jscript_section.php"); ?>
<script type="text/javascript">
document.getElementById("regBtn").addEventListener("click", submitReg);
function submitReg(){
var fname = sname = othername = email = phoneno = refid = '';
fname = document.getElementById("fname").value;
sname = document.getElementById("sname").value;
othername = document.getElementById("othername").value;
email = document.getElementById("email").value;
phoneno = document.getElementById("phoneno").value;
refid = document.getElementById("refid").value;
terms = document.getElementById("terms").checked;
if ((fname === '' || sname === '' || email === '' || phoneno === '' || refid === '' || terms === false)){
console.log("No Field should be empty!");
} else{
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
//window.location.assign("sign-up.php?m="+ this.responseText);
if (this.responseText == '1'){
document.getElementById("regResult").innerHTML = "Your registration was successful, check your email to complete the process";
}else{
document.getElementById("regResult").innerHTML = this.responseText;
}
}
};
xmlhttp.open("POST","dashboard/ajax/submitreg.php",true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("fname="+fname+"&sname="+sname+"&othername="+othername+"&phoneno="+phoneno+"&email="+email+"&refid="+refid);
document.getElementById("regBtn").disabled = true;
}
}
</script>
i was able to add enable at the document.getElementById("regBtn").enable= true;
<?php require_once("inc/jscript_section.php"); ?>
<script type="text/javascript">
document.getElementById("regBtn").addEventListener("click", submitReg);
function submitReg(){
var fname = sname = othername = email = phoneno = refid = '';
fname = document.getElementById("fname").value;
sname = document.getElementById("sname").value;
othername = document.getElementById("othername").value;
email = document.getElementById("email").value;
phoneno = document.getElementById("phoneno").value;
refid = document.getElementById("refid").value;
terms = document.getElementById("terms").checked;
if ((fname === '' || sname === '' || email === '' || phoneno === '' || refid === '' || terms === false)){
console.log("No Field should be empty!");
} else{
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
//window.location.assign("sign-up.php?m="+ this.responseText);
if (this.responseText == '1'){
document.getElementById("regResult").innerHTML = "Your registration was successful, check your email to complete the process";
}else{
document.getElementById("regResult").innerHTML = this.responseText;
}
}
};
xmlhttp.open("POST","dashboard/ajax/submitreg.php",true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("fname="+fname+"&sname="+sname+"&othername="+othername+"&phoneno="+phoneno+"&email="+email+"&refid="+refid);
document.getElementById("regBtn").disabled = true;
}
}
</script>

Checking If Email Exists In Database

I am checking if an e-mail exists in the database when a user is signing up for a new account. The "EmailMessage" is not coming up when I put in an e-mail that is in the database. Code located below:
validateemail.js
function validateemail() {
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var response = xmlhttp.responseText;
response = response.replace(/^\s+|\s+$/g,'')
if(response == 'error'){
document.getElementById("EmailMessage").innerHTML='This e-mail address is already taken! If you believe this is in error, please email support#pumpspy.com';
document.getElementById("newemail").focus();
document.getElementById("email_notice").style.display="none";
document.getElementById("submit_new_account").disabled =true;
}
else {
document.getElementById("EmailMessage").innerHTML="";
}
}
}
var newemail = document.getElementById("newemail").value;
if(newemail!=""){
xmlhttp.open("GET","Checkemail.php?email="+newemail,true);
xmlhttp.send();
}
else{
document.getElementById("EmailMessage").innerHTML="";
document.getElementById("submit_new_account").disabled =false;
}
}
Checkemail.php
$emailexists = $_GET['email'];
$sql = executeQuery("SELECT email from tbl_Users WHERE email = '$emailexists'");
$i = getNumRows($sql);
if ($i !=0) {
echo "error";
}else {
echo "pass";
}
---
<input type="text" id="newemail" name="newemail" maxlength="60" size="27" onblur="validateemail();"/>
</div>
<div id="EmailMessage" style="color:#F00"></div>
Try: $sql = executeQuery("SELECT email from tbl_Users WHERE email = '".$emailexists."'");
Also, please consider changing your code completely (concerns, security issues)...

loading a URL to a div tag

Hi I need to load a url inside to a div tag using Ajax. This is the code I used.
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("Loading_Page").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST","UserTypesPHP.php",true);
xmlhttp.send();
}
And this is how my UserTypesPHP.php looks like.
<?php
// Create connection
include('connectionPHP.php');
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//Adding Student records
if (!empty($_POST['UserTypeSubmit'])){
$sqlstu1="INSERT INTO user_type(User_ID ,User_Name) VALUES('$_POST[UserTypeID]','$_POST[UserType]')";
if (!mysqli_query($con,$sqlstu1))
{
die("<script>alert( \"Error: ". mysqli_error($con)."\");window.location.href='AdminUser.php';</script>");
}
else
echo "<script>alert ('The lecture was recorded successfully');
window.location.href=' AdminUser.php';
</script>";
}
mysqli_close($con);
?>
Im trying to redirect AdminUser.php to Loading_Page div. But this doesn't work out as expect. Please someone let me know the reason.
Thank you in advance.
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
if(xmlhttp.responseText == 'success'){
response();
}
//document.getElementById("Loading_Page").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST","UserTypesPHP.php",true);
xmlhttp.send();
}
function response(){
alert ('The lecture was recorded successfully');
window.location.href=' AdminUser.php';
}
And on the php file:
<?php
// Create connection
include('connectionPHP.php');
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//Adding Student records
if (!empty($_POST['UserTypeSubmit'])){
$sqlstu1="INSERT INTO user_type(User_ID ,User_Name) VALUES('$_POST[UserTypeID]','$_POST[UserType]')";
if (!mysqli_query($con,$sqlstu1))
{
die("<script>alert( \"Error: ". mysqli_error($con)."\");window.location.href='AdminUser.php';</script>");
}
else
echo "success";
}
mysqli_close($con);
?>
Here's a simple solution. It assumes that if the response contains <script>, the entire response is a script -- it doesn't try to parse the script out of other HTML.
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
if (/<script>/.test(xmlhttp.responseText)) {
var script = document.createElement('script');
script.appendChild(document.createTextNode(xmlhttp.responseText.replace(/<\/?script.*?>/g, '')));
document.getElementByTagName("head").item(0).appendChild(script);
} else {
document.getElementById("Loading_Page").innerHTML = xmlhttp.responseText;
}
}
}

ajax not loading content in DOM

instead of populating the "tempDiv" in html, the print.php is loaded showing the content. The same code is working for other files and javascript functions. :/
HTML:
<li><a class="button black" href="#searchbox" onclick="viewall()" >View All</a></li>
<li><button class="button black" type="submit" form="selectcol" onclick="printDiv()"></button></li>
<div id="searchresults" style="padding-top:30px; padding-bottom:10px; max-height:280px; ">
The results will show up here..!!
</div>
<div id="tempDiv"></div>
The viewall() function :
function viewall(){
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
/*
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("searchresults").innerHTML=xmlhttp.responseText;
}
}*/
xmlhttp.open("POST", "viewall.php", true); // set the request
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); // adds a header to tell the PHP script to recognize the data as is sent via POST
xmlhttp.send(); // calls the send() method with datas as parameter
// Check request status
// If the response is received completely, will be transferred to the HTML tag with tagID
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
document.getElementById("searchresults").innerHTML = xmlhttp.responseText;
}
}
}
The printDiv() function called to print the selected columns:
function printDiv()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
/*
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("searchresults").innerHTML=xmlhttp.responseText;
}
}*/
xmlhttp.open("POST", "print.php", true); // set the request
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); // adds a header to tell the PHP script to recognize the data as is sent via POST
xmlhttp.send(); // calls the send() method with datas as parameter
// Check request status
// If the response is received completely, will be transferred to the HTML tag with tagID
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
document.getElementById("tempDiv").innerHTML = xmlhttp.responseText;
}
}
}
PHP :
<?php
$col = $_POST['print'];
$flds = "";
foreach($col as $value){
if(isset($col)){
if($flds !="")
$flds .= ",";
$flds .= $value;
}
}
$sql = "SELECT ". $flds." from student";
$con = mysqli_connect("localhost", "root", "","university") or die("could not connect". mysqli_error($con));
$rs = mysqli_query($con, $sql);
echo "<table border='1'";
while($r = mysql_fetch_array($rs)){
echo "<tr>";
echo "<td class='searchtabledata'>".$r[0]."</td>";
echo "<td class='searchtabledata'>".$r[1]."</td>";
echo "</tr>";
}
?>
the result i get on clicking the submit button
Shouldn't you define the readystate function before sending the request?
part 2. It looks like your printdiv function submits a form. You should be able to remove the form tags if you are using a strictly AJAX procedure. You'll need to adjust a few other things to make that work.

JSON.parse(xmlhttp.responseText) unexpected character

I keep getting an unexpected character error in the console for the line
var a = JSON.parse(xmlhttp.responseText);
and I'm not sure why. Could this be why my textboxes aren't populating with the parsed data?
Main page code:
function loadDoc()
{
var xmlhttp;
// code for IE7+, Firefox, Chrome, Opera, Safari
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
// code for IE6, IE5
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var doc = window.document.createElement("doc");
var a = JSON.parse(xmlhttp.responseText);
document.getElementById("textbox").innerHTML=a.first;
document.getElementById("textbox2").innerHTML=a.second;
}
}
xmlhttp.open("GET","loadTextBox.php?id=4",true);
xmlhttp.send();
}
loadTextBox.php code:
<?php
---Placeholder for correct DB login info---
$result = $mysql->query(---Placeholder for correct SQL query---);
while ($row = $result->fetch_object())
{
$queryResult[] = $row->present_tense;
}
$textboxValue = $queryResult[0];
$textboxValue2 = $queryResult[2];
echo json_encode(array('first'=>$textboxValue,'second'=>$textboxValue2));
?>
Your loadTextBox.php file should not contain any HTML because the JSON.parse method expects only JSON:
<?php
header("Content-type: application/json");
$db_username = placeholder;
$db_password = placeholder;
$db_host = placeholder;
$result = $mysql->query(---Placeholder for correct SQL query---);
while ($row = $result->fetch_object())
{
$queryResult[] = $row->present_tense;
}
$textboxValue = $queryResult[0];
$textboxValue2 = $queryResult[2];
echo json_encode(array('first'=>$textboxValue,'second'=>$textboxValue2));
?>
If your DB login info is in a separate file, then there should be no HTML or BODY tags only the PHP tags.

Categories