/    Sign up×
Community /Pin to ProfileBookmark

ajax, javascript, php not working perfectly

While trying to create a registration page using PHP, JS and ajax, I run into errors that is due to the JS codes that I wrote and the reason for saying that is because without the ajax and JS codes, the PHP codes works perfectly.

This is the steps that I took, I wrote the HTML codes first and this is the HTML codes. It is called add.html

<!doctype html>
<html lang=’en’>
<head>
<meta charset=’utf-8′>
<title id=”title”>jQuery Insert</title>
<link rel=’stylesheet’ href=’css/styles.css’>
<script src=”js/jquery-3.3.1.min.js”></script>
</head>
<body>
<section id=”ball”>
<form action=”” method=”post” id=”theForm”>
<p><input type=”text” id=”f_name” name=”f_name” placeholder=”firstname” value=”></p>
<p><input type=”text” id=”l_name” name=”l_name” placeholder=”lastname” value=”></p>
</section>
<p><input type=”submit” id=”submit” value=”Submit” name=”submit”>
</p>
<p id=”success”></p>
<p id=”error”></p>
<p id=”statusT”></p>
<p id=”stat”></p>
<!–<script src=”js/insert.js”></script>–>
<script src=”js/ajax.js”></script>
<script src=”js/add.js”></script>
</body>
</html>
Then I create the ajax.js script

function getAjaxRequest(){
var ajax = null;
if(window.XMLHttpRequest){
ajax = new XMLHttpRequest();
}
if(window.ActiveXObject){// Older IE
ajax = new ActiveXObject(“MSXML.XMLHTTP.3.0”);
}
return ajax;
}
After this, I create the JS script

var f_name = document.getElementById(“f_name”).value;
var l_name = document.getElementById(“l_name”).value;
var submit = document.getElementById(“submit”);

var ajax = getAjaxRequest();

ajax.onreadystatechange = function(){
if(this.readyState == 4){
if((this.status == 200 || this.status < 300) && this.status == 304){
document.getElementById(“success”).innerHTML = this.responseText;
form.submit.disabled = true;
}else{
document.getElementById(“error”).innerHTML = this.responseText;
document.getElementById(“statusT”).innerHTML = this.statusText;
document.getElementById(“stat”).innerHTML = this.status;
}//End of status

}//End of readyState

}//End of ajax.onreadystatechange
ajax.open(“POST”, “/../../jquery/ajax/addStudent.php”);

var data =new FormData();
data.append(‘f_name’, f_name);
data.append(‘l_name’, l_name);

submit.addEventListener(‘click’, function(){
ajax.send(data);

}, false);//end of addEventListener
And finally, this the PHP script

<?php
// Set the database access information as constants:
DEFINE(‘DB_USER’, ‘willpower’);
DEFINE(‘DB_PASSWORD’, ‘arseblog’);
DEFINE(‘DB_HOST’, ‘localhost’);
DEFINE(‘DB_NAME’, ‘school’);
// Make the connection:
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
// Set the character set:
mysqli_set_charset($dbc, ‘utf8’);

if($_SERVER[‘REQUEST_METHOD’] == ‘POST’){
$sqlistu = “INSERT INTO students (`first_name`, `last_name`) VALUES
(?,?)”;

$queryistu = $dbc->prepare($sqlistu);
//echo $db->error;
$queryistu->bind_param(“ss”, $f_name, $l_name);
$queryistu->execute();

$IDnum = mysqli_insert_id($dbc);

// Free result set
$queryistu->close();
///////////////////////////

if (isset($IDnum)):
echo ‘success’ . ‘<br>’;
echo $IDnum;
endif;

}
While checking the NETWORK of the firefox developer edition browser under XHR and under RESPONSE, it shows the success message which was set to show

document.getElementById(“success”)

which was set to the

responseText

of ajax even before filling the form

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmAug 19.2018 — Huh?

This is a php forum. Why post a bunch of html code? And a bunch of JS code/Ajax code?

Really. If you want someone to look at your problem for free (which is what happens here) isolate the problem, give us some error messages, and help us to help you by pointing at the SPECIFIC lines of your code that are not working as you think they should be.
Copy linkTweet thisAlerts:
@oluwa86authorAug 19.2018 — Well am not that familiar with ajax. I can't really say this is where the problem is. But It should be in the JS code. I think
Copy linkTweet thisAlerts:
@rootAug 19.2018 — Word form abuse I have laid eyes on.

The proper way to deal with this is to first start with HTML only and HTML5 form fields.

Get the HTML form submitting and the server-side accepting. Once you have the HTML form working, Great, you have a fallback position.

You can then add the JavaScript layer.

If the JavaScript layer fails, maybe error in clients machine, not supporing JavaScript, etc, then the form will still work under HTML.

There seems to be a new breed or teaching that completely removes the proper use of Forms from HTML and push coding which is and alwayus has been the worst way of starting out.

So in simple terms...

HTML

JavaScript

Check it works

Put lipstick on that pig (CSS)
Copy linkTweet thisAlerts:
@oluwa86authorAug 20.2018 — If you can read my post clearly, I think I vividly mentioned it that the form submit using HTML and PHP alone
Copy linkTweet thisAlerts:
@vishalsanwarAug 20.2018 — Hi @oluwa86

I checked you have used javascript AJAX code and you write complex code.

You should use jquery instead of Javascript Ajax.

You can also check browser console for error. If you share error, I will give you proper solution.

Thanks
Copy linkTweet thisAlerts:
@rootAug 21.2018 — > @oluwa86#1595094 If you can read my post clearly,

> @oluwa86#1595053 registration page using PHP, JS and ajax, I run into errors that is due to the JS codes


Your code has lots of JavaScript in it, if you want a web form that works, start again... Ignore JavaScript until you actually have a standard HTML / PHP set up working properly.
Copy linkTweet thisAlerts:
@oluwa86authorAug 21.2018 — I said the form works with PHP and HTML alone. Simple
Copy linkTweet thisAlerts:
@oluwa86authorAug 21.2018 — I would like to see an example of how the jquery works with the POST method. I would appreciate it if you can help me
Copy linkTweet thisAlerts:
@surajitbasak109Aug 22.2018 — Hi oluwa86, your problem is solved. Please visit the below link to get the solutions.

https://pastebin.com/vbgfv0Xm

[upl-image-preview url=https://www.webdeveloper.com/forum/assets/files/2018-08-22/1534939832-538826-ajax-question-solved.png]
Copy linkTweet thisAlerts:
@oluwa86authorAug 25.2018 — Oh thanks. Sorry for the late reply. I will visit it now
×

Success!

Help @oluwa86 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...