Checking If Email Exists In Database - php

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

Related

laravel ajax 500 internal server error

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');

PHP INSERT to hosting database

I have this PHP code that inserts id, firstname and lastname:
<?php
header('Access-Control-Allow-Origin: *');
$conn = mysql_connect('localhost', 'happy', '***');
$db = mysql_select_db('dbtest');
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
mysql_query("INSERT INTO tbltry (firstname, lastname) VALUES('$firstname', '$lastname')");
?>
And I have this HTML with AJAX:
<script>
function save(){
var xmlhttp;
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("POST", " **url** ", true);
console.log("added");
xmlhttp.send();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
}
}
}
</script>
</head>
<body>
Firstname: <input type="text" name="firstname" id="firstname">
Lastname: <input type="text" name="lastname" id="lastname">
<button onclick="save()">Save</button>
</body>
This two are working fine, it insert a new row in database, but the only data that is inserted is the id on id column and column firstname and lastname is blank.
Can someone tell what am I missing? Any help would be appreciated.
Try this
<script>
function save(){
var xmlhttp;
var fname = document.getElementById("firstname").value;
var lname = document.getElementById("lastname").value;
var data = {firstname:fname,lastname:lname};
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("POST", " **url** ", true);
console.log("added");
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xmlhttp.send(data);
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
}
}
}
Try something like this,
function save(){
var fname = document.getElementById("firstname").value;
var lname = document.getElementById("lastname").value;
var data = "firstname="+fname+"&lastname="+lname;
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)
{
alert(xmlhttp.responseText);
}
}
xmlhttp.open("POST","url?"+data,true);
xmlhttp.send();
}
your test file should be like
<?php
$conn = mysql_connect('localhost', 'happy', '***');
$db = mysql_select_db('dbtest');
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
mysql_query("INSERT INTO tbltry (firstname, lastname) VALUES('$firstname', '$lastname')");
?>
Instead of using old xmlhttp its better to use jQuery.Ajax, Also mysql functions are deprecated use PDO.
You should use any mysql escape function for security.
Hope it helps..

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.

Escaped single character introducer instead of actual character

For some reason \u009a is being sent to the xmlhttp.responseText and not \u0161, and I'm not sure why. I want ลก to be displayed in the textbox, but, instead, the single character introducer is being sent instead. Any ideas how I can fix this?
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)
{
alert(xmlhttp.responseText);
var a = JSON.parse(xmlhttp.responseText);
document.getElementById("textbox").value=a.first;
document.getElementById("textbox2").value=a.second;
document.getElementById("textbox3").value=a.third;
document.getElementById("textbox4").value=a.fourth;
document.getElementById("textbox5").value=a.fifth;
document.getElementById("textbox6").value=a.sixth;
}
}
xmlhttp.open("GET","loadTextBox.php?id=4",true);
xmlhttp.send();
}
loadTextBox.php code:
<?php
header("Content-type: application/json");
---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[1];
$textboxValue3 = $queryResult[2];
$textboxValue4 = $queryResult[3];
$textboxValue5 = $queryResult[4];
$textboxValue6 = $queryResult[5];
echo json_encode(array('first'=>utf8_encode($textboxValue),'second'=>
utf8_encode($textboxValue2),'third'=>utf8_encode($textboxValue3),'fourth'=>
utf8_encode($textboxValue4),'fifth'=>utf8_encode($textboxValue5),'sixth'=>
utf8_encode($textboxValue6)));
?>
Adding in the line $mysql->query("SET CHARACTER SET 'utf8'"); inside loadTextBox.php after connecting to the DB and before the SQL query and removing the lines with utf8_encode fixed it.

Categories