Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Ok I can't get this jQuery to work whatsoever! And its so simple! All I want it to do is make the #main div fade in on load.
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="randomizer.js"></script>
<title>
<?php
if (isset($_POST['first']) && isset($_POST['second'])) {
echo "Randomized {$_POST['first']}-{$_POST['second']}";
}
else {
echo "Randomizer";
}
?>
</title>
<link type="text/css" rel="stylesheet" href="randomizer.css" />
<script type="text/javascript" src="randomizer.js"></script>
</head>
<body>
<?php
if (isset($_POST['first']) && isset($_POST['second'])) {
$grabFirst = strip_tags($_POST['first']);
$grabSecond = strip_tags($_POST['second']);
}
?>
<div id="main">
<center><form action="randomizer.php" method="post">
<input type="text" name="first" value="<?php if(isset($grabFirst)){ echo htmlspecialchars($grabFirst); } ?>" placeholder="First Number" /><br />
<input type="text" name="second" value="<?php if(isset($grabSecond)){ echo htmlspecialchars($grabSecond); } ?>" placeholder="Second Number" /><br />
<input type="submit" />
</form>
<?php
if (isset($_POST['first']) && isset($_POST['second'])) {
$grabFirst = strip_tags($_POST['first']);
$grabSecond = strip_tags($_POST['second']);
}
else {
print "Please enter two numbers.\n";
die;
}
if (strlen($grabFirst) < 1 or strlen($grabSecond) < 1) {
print "Please make sure you filled out both feilds.\n";
die;
}
elseif (!is_numeric($grabFirst) && !is_numeric($grabSecond)) {
print "Please make sure you entered two valid numbers.\n";
die;
}
elseif (!is_numeric($grabSecond)) {
print "Please make sure you entered two valid numbers.\n";
die;
}
elseif (!is_numeric($grabFirst)) {
print "Please make sure you entered two valid numbers.\n";
die;
}
else {
}
$rand = rand($grabFirst, $grabSecond);
print "A random number between {$grabFirst} and {$grabSecond} is {$rand}.\n";
?></center>
</div>
</body>
</html>
Heres my jQuery
$(document).ready(function(){
$('#main').fadeTo('slow', 1);
});
If anyone could help, I'd really appreciate it. I've been trying to fix it for like an hour, and I have no luck. I'm just beginning with jQuery and have no experience, maybe its a really dumb error, but I have no idea. Thanks~
You need to include jQuery in your page:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
I don't know PHP and have downloaded this code that works perfectly fine.
I add this code on top of any *.php file and it makes that page, password protected that only opens up if you type the password which in this case is 1234.
Now, I want to add 3 passwords instead of 1. I just don't know where to edit this code.
<?php
session_start();
if(isset($_POST['submit_pass']) && $_POST['pass'])
{
$pass=$_POST['pass'];
if($pass=="1234")
{
$_SESSION['password']=$pass;
}
else
{
$error="Incorrect Pssword";
}
}
if(isset($_POST['page_logout']))
{
unset($_SESSION['password']);
}
?>
<?php
if($_SESSION['password']=="1234")
{
?>
<form method="post" action="" id="logout_form">
<input type="submit" name="page_logout" value="Logout">
</form>
<?php
}
else
{
?>
<form method="post" action="">
<div>Protected Content</div>
<br />
<input type="password" name="pass" placeholder="Type your password">
<input type="submit" name="submit_pass" value="Login">
<br /><br />
<div><?php echo $error;?></div>
</form>
<?php
}
?>
Where should I, add what, to be able to have 3 possible passwords?
For this, you may edit password checking line like this:
<?php
session_start();
if(isset($_POST['submit_pass']) && $_POST['pass'])
{
$pass=$_POST['pass'];
$available_passwords = ['12345','pass123','myOtherPassword'];
if(in_array($pass,$ava_passwords))
{
$_SESSION['password']=$pass;
}
else
{
$error="Incorrect Pssword";
}
}
if(isset($_POST['page_logout']))
{
unset($_SESSION['password']);
}
?>
This question already has answers here:
The 3 different equals
(5 answers)
Closed 4 years ago.
I am learning PHP and trying to test out some code but it won't work and I can't figure out why. I am trying to make a simple form where:
A)When the page first loads beneath the form it says "You have not written anything yet."
B)If something is written in the form field and submitted then beneath the form it says "Your message has been sent"
C)If nothing is written in the form field but the user hits submit that it says "Error: You didn't write anything".
I can get the first two parts to work but no matter what I do I can't get C to work.
Here is my code:
<?php
$oldname = isset($_POST['name']);
?>
<!DOCTYPE html>
<html>
<head>
<title>Form Test</title>
</head>
<body>
<form method="POST" action="">
<label for="name">Your Name:</label>
<input type="text" name="name" id="name">
<input type="submit" value="submit">
</form>
<?php
if ($oldname) {
if ($_POST['name'] = '') {
echo "Error: You didn't write anything";
}
else {
echo "Your message has been sent";
}
}
else {
echo "You have not written anything yet";
}
?>
</body>
</html>
You wrote an assignment in your if statement:
$_POST['name'] = ''
I think you meant to do a comparison:
$_POST['name'] == ''
You have a typo. You were using assignment operator in the if condition which makes it true all the time so it never enters else condition "Your message has been sent".
I have fixed the code. Take a look at it.
<?php
$oldname = isset($_POST['name']);
?>
<!DOCTYPE html>
<html>
<head>
<title>Form Test</title>
</head>
<body>
<form method="POST" action="">
<label for="name">Your Name:</label>
<input type="text" name="name" id="name">
<input type="submit" value="submit">
</form>
<?php
if ($oldname) {
if ($_POST['name'] == '') {
echo "Error: You didn't write anything";
}
else {
echo "Your message has been sent";
}
}
else {
echo "You have not written anything yet";
}
?>
</body>
</html>
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
this is my page when i run it i got the excption which sayes that the formSubmit is not define . i know that i have to check if the post contents that variable but i couldn't know the solution
<?php
if($_POST['formSubmit'] == "Submit")
{
$errorMessage = "";
if(empty($_POST['formPassword']))
{
$errorMessage .= "<li> You forgot to enter your password!</li>";
}
if(empty($_POST['formName']))
{
$errorMessage .= "<li> You forgot to enter a name!</li>";
}
$varPassword = $_POST['formPassword'];
$varName = $_POST['formName'];
if(empty($errorMessage))
{
require('Document1.php');
User::add_user($varName, $varPassword);
exit;
//header("Location: normal.php");
}
}
?>
<html>
<head>
<title>SIGN UP</title>
<style type="text/css">
<!--
.style1 {
font-size: xx-large;
color: #0000CC;
}
-->
</style>
</head>
<body>
<p align="center"><br>
<span class="style1">SIGN UP</span></p>
<p> </p>
<form action="myform1.php" method="post">
<p>
What is Your NAME ?<br>
<input type="text" name="formName" maxlength="50" value="" />
</p>
<p>
What is Your PASSWORD ?<br>
<input type="text" name="formPassword" maxlength="50" value="" />
</p>
<?php
/*
?>
<p>
What is Your Email ?<br>
<input type="text" name="formEmail" maxlength="50" value="" />
</p>
*/
?>
<input type="submit" name="formSubmit" value="Submit" />
<input type=reset value = "delete fields ">
</form>
</body>
</html>
any help would be appreicated
The reason is because the PHP code also runs the very first time when the page loads, even though the form isn't yet submitted. The solution is to change your first IF statement as follows:
<?php
if(isset($_POST['formSubmit']) && $_POST['formSubmit'] == "Submit")
{
// OTHER CODE REMAINS AS IT IS
}
?>
It will now check that not only $_POST['formSubmit'] is set to "Submit" but also that it exists. When the page first loads, $_POST['formSubmit'] will not be available and therefore no exception will be thrown.
Hope it helps!
I suggest using .
if (isset($_POST['formSubmit']) && ($_POST['formSubmit'] == "Submit")) {
/* ... */
}
isset() method will check if value exists
You can check that its a $_POST request with $_SERVER['REQUEST_METHOD'] === 'POST' also add errors to an array this way you can place the errors where you want.
<?php
if($_SERVER['REQUEST_METHOD'] === 'POST'){
$error = array();
if(empty($_POST['formPassword']))
{
$error['pass'] = "You forgot to enter your password!";
}
if(empty($_POST['formName']))
{
$error['name'] = "You forgot to enter a name!";
}
if(empty($error))
{
require('Document1.php');
User::add_user($_POST['formName'], $_POST['formPassword']);
exit;
//header("Location: normal.php");
}
}
?>
Then if error just add <?php echo isset($error['name']) ? $error['name'] : null ?> where you want the message specific to the error. like next to the input for name.
It's always a good practice to use isset() to check if a variable is set.
Change
if($_POST['formSubmit'] == "Submit")
to
if(isset($_POST['formSubmit']) && $_POST['formSubmit'] == "Submit"){
//rest of the code
}
See also: empty()
Before you try to get the value of $_POST['formSubmit'], you should check if it is set with isset($_POST['formSubmit'])
if (isset($_POST['formSubmit']) && $_POST['formSubmit'] == "Submit") {
// your code
}
I think it also work with:
if ($_POST['formSubmit'] && $_POST['formSubmit'] == "Submit") {
// your code
}
but is better to use isset()
Rather than using isset() everywhere, I prefer to declare default values. For example...
$defaults = array(
'formSubmit' => '',
'formPassword' => '',
'formName' => '',
);
$form = $_POST + $defaults;
if ($form['formSubmit'] == 'Submit') {
// ...
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have four text boxes name
textbox1:
textbox2:
textbox3:
textbox3:
I need to validate this field by following some criteria like below :
1.If anyone put value in textbox1,he must forced to put value in textbox2.Otherwise both textbox can be empty.
2.If anyone put value in textbox3,he must forced to put value in textbox4.Otherwise both textbox can be empty.
Above this criteria,how can i make validation.
You can try this.May be it will help you.
first put the css and js library.
<link rel="stylesheet" href="http://www.position-relative.net/creation/formValidator/css/validationEngine.jquery.css" type="text/css"/>
<script src="http://www.position-relative.net/creation/formValidator/js/jquery-1.7.2.min.js" type="text/javascript"></script>
<script src="http://www.position-relative.net/creation/formValidator/js/languages/jquery.validationEngine-en.js" type="text/javascript" charset="utf-8"></script>
<script src="http://www.position-relative.net/creation/formValidator/js/jquery.validationEngine.js" type="text/javascript" charset="utf-8"></script>
this is your js function which will validate your form field while submit the form
<script type="text/javascript">
$(function(){
$("#ckForm").validationEngine();
});
</script>
Now the form
<form action="" method="post" accept-charset="utf-8" id="ckForm">
<input type="text" name="textbox1" id="textbox1" />
<input type="text" name="textbox2" id="textbox2" class="validate[condRequired[textbox1]]" />
<input type="text" name="textbox3" id="textbox3" />
<input type="text" name="textbox4" id="textbox4" class="validate[condRequired[textbox3]]" />
<input type="submit" value="GO" />
</form>
Please let me know if you need any further help.
It's just an example and not a final solution but if you've a few PHP and JS skills it should help you a lot.
PHP:
$Input = new Input();
$Input
->post("username")
->validate(Input::RULE_MINLENGTH, 4)
->validate(Input::RULE_MAXLENGTH, 20)
->post("email")
->validate(Input::RULE_NOT_EMPTY, null)
->validate(Input::RULE_IS_EMAIL, true)
->post("email2")
->validate(Input::RULE_EQUALS, "email")
->post("password")
->validate(Input::RULE_MINLENGTH, 1)
->validate(Input::RULE_MAXLENGTH, 20)
->post("password2")
->validate(Input::RULE_EQUALS, "password")
->post("random_text");
if (empty($_POST)) {
// $_POST is empty, show the template!
$Template->assign("validation_rules", $Input->get_rules_json());
$Template->show("template.tpl");
} else {
if ($Input->submit("post")) {
// everything alright
echo "No errors.";
} else {
echo "Errors: ";
print_r($Input->get_errors());
}
}
Template/JavaScript:
$("#form").ajaxForm({
"beforeSubmit": function() {
// validate
return formValidation($("#form"), '<?php echo $this->validation_rules?>');
}
});
Edit:
Check out this video: http://www.youtube.com/watch?v=Qf6o3MIcAtA
Enjoy!
I think what you are trying to say to me it seems as if you require ajax. See if you can use AJAX or not for fulfilling your requirement.
You can achive this using simple java script function.
function validate()
{
var txtBox1= document.getElementById('textbox1');
var txtBox2= document.getElementById('textbox2');
var txtBox3= document.getElementById('textbox3');
var txtBox4= document.getElementById('textbox3');
if(txtBox1.value!=""&&txtBox2.value=="")
{
alert("Enter a value in text box");
txtBox2.focus();
return;
}
if(txtBox3.value!=""&&txtBox4.value=="")
{
alert("Enter a value in text box");
txtBox4.focus();
return;
}
}
you can call this method onchange of each text box.let me know i answer your question?
I have a php script for a number-guessing game and an html script for a congratulation page. If the guess is correct, the game will end and the congratulation page will open. In the php, I have a variable $prize=1000-100 * $_POST['tries'], such that if the first guess is right, the player will win $1000; if the player has a second guess, the prize will be $100 less, and so on. This variable is saved in a hidden field in the php as $_POST['prize']. I hope the final prize can be printed in the congratulation page, but it didn’t work as I expected. Did I do anything wrong in the html? Thanks guys, Maria.
guess.php:
<?php
if(isset($_POST['number'])) {
$num = $_POST['number'];
} else {
$num = rand(1,10);
}
if(isset($_POST['prize'])) {
$prize =1000-100 * $_POST['tries'];
} else {
$prize = 900;
}
$tries=(isset($_POST['guess'])) ? $_POST['tries']+1: 0;
if (!isset($_POST['guess'])) {
$message="Welcome to the Guessing Game!";
} elseif (!is_numeric($_POST['guess'])) {
$message="You need to type in a number.";
} elseif ($_POST['guess']==$num) {
header("Location: Congrats.html");
exit;
} elseif ($_POST['guess']>$num) {
$message="Try a smaller number";
} else {
$message="Try a bigger number";
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Guessing Game</title>
</head>
<body>
<h1><?php echo $message; ?></h1>
<p><strong>Guess number: </strong><?php echo $tries; ?></p>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<p><label for="guess">Type your guess here:</label><br/>
<input type="text" id="guess" name="guess" />
<input type="hidden" name="tries" value="<?php echo $tries; ?>"/><br/>
<input type="hidden" name="number" value="<?php echo $num; ?>"/><br/>
<input type="hidden" name="prize" value="<?php echo $prize; ?>"/>
</p>
<button type="submit" name="submit" value="submit">Submit</button>
</form>
</body>
</html>
congrats.html:
<! DOCTYPE html>
<html>
<header>
<title>Congratulation!</title>
<body>Congratulation!<br/>
You Won <?php echo $_POST['prize']; ?> dollars!
</body>
</header>
</html>
it looks like your script will work, but you'll need to change congrats.html to congrats.php because html is static and php is dynamic. Also you might want to use sessions because anyone can inspect-element and change the value.
You just need to pass the value to the congrats page, using either GET request or a session. I'd recommend using a session so people cannot alter the prize value.
Just amend this part here:
} elseif ($_POST['guess']==$num) {
$_SESSION['prize'] = $_POST['prize'];
header("Location: Congrats.php");
exit;
}
Then (you need to change the congrats page to a php page to use the session btw to enable php)
Congrats.php
<! DOCTYPE html>
<html>
<header>
<title>Congratulation!</title>
<body>Congratulation!<br/>
You Won <?php echo $_SESSION['prize']; ?> dollars!
</body>
</header>
</html>
PS: Session will also require session_start() at the top of both documents.