Issue with javascript validation in netbeans - php

I am new to Netbeans. I have created a simple php project. It consists of a form and I've written a separate Javascript file to validate its fields. I tried running the project outside netbeans, it works fine. I recently imported the project to netbeans but javascript doesn't seem to work well. When I try opening the page(form page) in the browser, the entire javascript file appears on the page.
I do not know where I'm going wrong. As I'm new to netbeans I am unable to rectify it. Kindly help.
My Javascript file is named Validation.js and is in the js folder.
function ValidateForm(theForm)
{
//returns true/false after validation
}
My php file looks similar to this.
<?php include "js/Validation.js";
//if validation returns true
if(isset($_POST))
{
if(isset ($_POST['bt_save']) && $_POST['bt_save'] == 'Submit')
{
//code to insert fields into database after validation
}
}
?>
<form method="post" name="form1" id="form1" onsubmit="return ValidateForm(this);">
<!--form elements-->
<input type="submit" name="bt_save" value="Submit"/>
</form>
Thanks in advance!

Your include is wrong, if you use php include it just loads the page.
Try using <script type="text/javascript" src="js/Validation.js"></script>. instead of include "js/Validation.js".
So your new file would be something like.
<?php
//if validation returns true
if(isset($_POST))
{
if(isset ($_POST['bt_save']) && $_POST['bt_save'] == 'Submit')
{
//code to insert fields into database after validation
}
}
?>
<form method="post" name="form1" id="form1" onsubmit="return ValidateForm(this);">
<!--form elements-->
<input type="submit" name="bt_save" value="Submit"/>
</form>
<script type="text/javascript" src="js/Validation.js"></script>

Related

PHP form not sending data to $_POST

<!DOCTYPE html>
<html>
<body>
<form action="test.php" method="post">
<input name="test" type="text">
<input type="password" name="data">
<input type="submit">
</form>
</body>
</html>
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$name = $_POST['test'];
$pass = $_POST['data'];
}
?>
I am trying to make a simple form in PHP that users will use for login. When using this code, nothing ever gets put inside the $_POST. I am debugging using PHPstorm and I can tell that the request method is definitely POST, but no data is getting passed through. What am I doing wrong?
Try adding a slash at the begening of the action parameter of the form tag.
<form action="/test.php" method="post">
Try to remove "action" from form. It worked for me.
Code: http://nimb.ws/6pXjnz
Output:http://nimb.ws/PvuxZs

Cannot echo the values from a simple php form

First time i try to create a simple form using the POST method.Problem is when i click the button nothing gets echoed.
here is my insert.php file :
<?php
if(isset($_POSΤ["newitem"])){
echo $itemnew = $_POSΤ["newitem"];
}
?>
<form action="insert.php" method="POST" >
<input type="text" name="newitem">
<input type="submit" value="Save">
</form>
EDIT: I tried the GET method and it works...Any ideas why that happened? Server configurations?
NEW EDIT: So it turns out i switched method to GET and it worked.Then i switched back to POST (like the code i posted on top) and it works...I have no clue why this happened.Any suggests?
The code you have posted is perfectly valid and should work.
I'm going to guess that you do not have PHP enabled, or it is not working.
<?php ... ?> looks to the browser like a long, malformed HTML tag, and therefore ignores it, making the effect invisible.
Try right-clicking the page and selecting View Source. If you see your PHP there, then the server is indeed not processing it.
The most likely reason for this is probably the same problem I had with my very first bit of PHP code: you're trying to "run" it directly in your browser. This won't work. You need to upload it to a server (or install a server on your computer and call it from there)
Use !empty($_POST['newitem'] instead:
if(!empty($_POSΤ["newitem"])){
echo $itemnew = $_POSΤ["newitem"];
}
empty()
Try the following:
if($_POST) {
if(!empty($_POST['newitem'])) {
$itemnew = $_POSΤ['newitem'];
echo $itemnew;
// or leave it as is: echo $itemnew = $_POSΤ['newitem'];
}
}
?>
<form action="insert.php" method="POST" >
<input type="text" name="newitem">
<input type="submit" value="Save">
</form>
The if($_POST) will make sure the code is only executed on a post. The empty() function will also check if it isset() but also checks if it is empty or not.
Try this :
<?php
if(isset($_POSΤ["newitem"])){
echo $itemnew = $_POSΤ["newitem"];
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" >
<input type="text" name="newitem">
<input type="submit" value="Save">
</form>
$_SERVER['PHP_SELF']; is pre-defined variable in php.It allows the user to stay on same page after submitting the form.

embed html form to php code

I have a form on on html outside of php...
<form method="post" action="">
<input type="text" name="user"/></br>
<input type="submit" value="submit" name="login"/>
</form>
then call submit button from php and do this
if(isset($_POST["login"]))
{
print <<<this
<form method="post" action="">
<input type="submit" name="apply"/>
</form>
this;
if(isset($_POST["apply"]))
{ print "it works";}
}
Alright, so the problem is that, "it works" won't print from the second form thats inside the php. it just takes me back to where i came from. Perhaps it's a dumb question, please help though! thanks
The problem is that by the time you're checking if(isset($_POST["apply"])) the login condition becomes invalid because everything is inside the if(isset($_POST["login"])).
Try taking the if(isset($_POST["apply"])) outside the login IF.
Your "apply" code exists only INSIDE the login test code. When you submit that second form, there will be NO login form field, because you didn't include an input/textarea of that name in the second form. So the second form submits, there's no login, and the entire inner code never gets executed. You probably want:
if(isset($_POST["login"]))
{
print <<<this
<form method="post" action="" name="apply">
<input type="hidden" name="login" value="foo" /> <!-- add this line -->
etc...
I'm not sure to understand what you wanna do with this code but you obviously missed some details :
_You did not set the "action" field in your form tag, so I don't understant how you would like the PHP file to get called ?
_Your code if(isset($_POST['login'])) has no sense, you are testing the existence of a value sent by a validation button, you'd rather whrite isset($_POST['user'])
Hoping to have helped you
Your variables are declared in 2 forms, so there will be 2 calls (completely independant) to your php.
So you could have a second submit button inside your second form:
if(isset($_POST["login"]))
{
print <<<this
<form method="post" action="">
<input type="submit" name="apply" value="Second"/>
</form>
this;
}
if(isset($_POST["apply"]))
{ print "it works";}

AJAX XMLHttpRequest not working

I'm trying to work with ajax. I have two pages: request.html and reply.php.
request.html:
<html>
<script language="javascript">
var xht = new XMLHttpRequest();
function testAJAX()
{
xht.open("get","http://localhost:9999//a.php", true);
xht.send();
xht.onreadystatechange=function() {
if (xht.readyState==4) {
alert("Text: "+xht.responseText);
}
}
}
</script>
<form id="form1" name="form1" method="post" action="">
btn
<input name="btn" type="submit" id="btn" onClick="testAJAX();" value="Submit" />
</form>
</html>
reply.php:
<?php
echo 'hi';
?>
The problem is that I don't get a response via xht.responseText and with xht.responseXML I get null and with xht.status I get 0.
I asked the link http://localhost:9999//a.php via browser and got hi correctly.
P.S: I tried this on Chrome 29.0.1547.18 and Maxthon v4.1.1
any ideas..
You don't need to mention "http://localhost".
The main mistake is you have given the input type as Submit If it is submit the form will be submitted first the click event will not trigger. Change the input type to button.
If you want to do form submission do it in java script
The corrected code is below.
<form id="form1" name="form1" method="post" action="">
btn
<input name="btn" type="button" id="btn" onClick="testAJAX();" value="Submit" />
// change type to button
</form>
var xht = new XMLHttpRequest();
function testAJAX()
{
xht.open("get","a.php", true); /// Change to a.php
xht.send();
xht.onreadystatechange=function() {
if (xht.readyState==4) {
alert("Text: "+xht.responseText);
}
}
}
Adding to SarathPrakash's answer, I would like to point out that there is nothing wrong with specifying localhost. It will still work as long as the PHP file's address is valid.
You can also have the submit button. But you'll have to modify the form opening tag as follows:-
<form id="form1" name="form1" method="POST" action="" onsubmit="return false">
This is will stop the default behaviour of the form being submitted. Although in my opinion, it is best to avoid it altogether, and just stick with assigning the correct event handler to the onclick attribute.
Also, it is good practice to follow the correct syntax for HTML documents.
<html>
<head>
<title> Your title here </title>
<script type="text/javascript"> Your script here </script>
</head>
<body>
Your main document text here. Forms, tables etc.
</body>
</html>
For a simple tutorial, you could try this.

Validating input text in php when moving pages

I do know how to validate a basic input(text) when submitting a form. However, I am lost as to how I am going to validate an input(text) when leaving a web page. Even with JS I couldn't get it to stay on the same page due to the "form action" attribute.
HTML Code for the input and submit
<form name="form1" action="second.php" **onsubmit="return error()"** method="post">
<input style="" name="hall" type="text"><br>
<input name="Move" style="height: 23px" type="submit" value="Move">
</form>
PHP CODE for validating
<?php
if (isset($_POST['Move'])) {
if(($_POST['hall']) != "Hallway")
{
echo "Not among available rooms";
}
?>
Also this is the JS code
<script type="text/javascript">
function error()
{
var x=document.forms["form1"]["hall"].value
if (x==null || x=="" || x!="next")
{
alert("Wrong entry. Try again!!!");
return false;
}
}
</script>
The following code is working on my server, provided the second.php page exists. (BTW I added a ; after value, but it seems to work without it).
Did you put the js script after the form? Maybe it could impact.
<html>
<script type="text/javascript">
function error()
{
var x=document.forms["form1"]["hall"].value;
if (x==null || x=="" || x!="next")
{
alert("Wrong entry. Try again!!!");
return false;
}
}
</script>
<body>
<form name="form1" action="second.php" onsubmit="return error()" method="post">
<input style="" name="hall" type="text"><br>
<input name="Move" style="height: 23px" type="submit" value="Move">
</form>
</body>
</html>
Other things I can think of:
-Do you have any JS errors on the page that would prevent this script from running? It worked on my server, as well.
-Do you have another HTML element with an identical name?
-I am assuming that the **'s were for emphasizing that area in your code. If not, of course, they would need to be removed.
The only thing that could prevent your "return false" from working is if you have some kind of JavaScript error that kills the script from running.
If you have additional code on your page, please post it and we'll take a look!

Categories