I want to change the content of my subscription div,like when user is already subscribed I want to change the div color into red and to tell a message that 'you are already subscribed'.I know it can be done with ajax but I don't know anything about Ajax.
Here is the PHP inserted div
<?php
echo ';
<div id="sub">
<form method="post" action="subscription.inc.php">
<input type="text" name="email" placeholder="Enter your Email">
<button type="submit" name="submit" value="submit" `enter code here`onclick="myAjax()">Subscirbe</button>
</form>
</div>
';
?>
This is the PHP code that should be run on clicking
<?php
include 'conn.php';
include 'sub.php';
if (isset($_POST['submit'])) {
$email=mysqli_real_escape_string($conn,$_POST['email']);
$sql="INSERT INTO `subscribe` (`id`, `email`) VALUES (NULL, '$email');";
$emailcheck="SELECT * FROM `subscribe` WHERE email='ayush.antino#gmail.com'";
$doubleemail=mysqli_query($conn,$emailcheck);
$num_rows=mysqli_num_rows($doubleemail);
if ($num_rows>0) {
header("Location:sub.php?subscribe=alreadyexist");
exit();
}elseif ($num_rows==0) {
if (!filter_var($email,FILTER_VALIDATE_EMAIL)) {
echo "please enter valid email";
} else {
mysqli_query($conn,$sql);
header("Location:sub.php?subscribe=success");
exit();
}
}
}
?>
How should I refresh the message 'you are already subscribed' without refreshing the page
Try Like this..
<div id="sub">
<form method="post" action="subscription.inc.php">
<input type="text" name="email" placeholder="Enter your Email">
<button type="submit" name="submit" value="submit" `enter code here`onclick="myAjax()">Subscirbe</button>
</form>
</div>
<script>
checkSubscribe();
function checkSubscribe() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
$('#sub').css('background-color:red;')
}
};
xmlhttp.open("GET", "checkuser.php", true);
xmlhttp.send();
}
</script>
Related
i'm doing a very rookie password signup thing for my web development project and i'm stuck here
<script>
<?php
if ( isset($_POST['enter']) ){
if ($_POST['password']=='0000'){
echo('document.getElementById("adminsection").style.display = "block";
document.getElementById("passsection").style.display = "none" ;');
}
else{
echo ("alert('password incorrect');");
}
}
?>
</script>
for some reason this part doesn't seem to work when the condition is met:
echo('document.getElementById("adminsection").style.display = "block";
document.getElementById("passsection").style.display = "none" ;');
adminsection and passsection are just section tags that contain the elemnts of the page and they look kinda like this
<form style="width:100%; margin:10% 0 10% 0" action="administrator123.php" method="POST">
<section id="passsection">
<input style="margin-left:-5px" id="passfield" name="password" type="password" placeholder="****">
<input type="submit" id="password" name="enter" value="enter">
</section>
<section id="adminsection">
<p>hey</p>
</section>
</form>
the goal behind this code is to hide the passsection and show the adminsection upon entering the correct password
i'd appreciate it very much if you provided me with a solution that doesn't involve jQuery
You're mixing javascript and PHP the wrong way.
Normal form submission. The order of operations are:
you have a form in html (you're missing the form element)
user submits the form
this sends the submit to the server causing a page load/reload
server-side PHP checks if the password is good
The newly loaded page does stuff with the PHP output.
<?php
if ( isset($_POST['enter']) ){
if ($_POST['password']=='0000'){
$loggedin = true;
} else {
$error="Password was incorrect";
}
}
?>
<?php if (!$loggedin) {?>
<section id="passsection">
<?php if ($error) {?>
<div class='error'>
<?php echo $error;?> </div>
<?}?>
<form method='post'>
<input style="margin-left:-5px" id="passfield" name="password" type="password" placeholder="****">
<input type="submit" id="password" name="enter" value="enter">
</form>
</section>
<?}?>
Ajax form submission. The order of operations are:
you have a form in html (you're missing the form element)
user submits the form
javascript prevents the form from reloading the page, and sends the form data through ajax to the server
server-side PHP checks if the password is good and outputs the response in json_encode() format
ajax function gets response and uses that data to provide feedback.
Your php will live in a separate file
doLogin.php
<?php
$loggedin = false;
if ( isset($_POST['enter']) ){
if ($_POST['password']=='0000'){
$loggedin = true;
} else {
$error="Password was incorrect";
}
}
$response = array('isLoggedIn' => $loggedin);
if ($error) $response['error'] = $error;
echo json_encode($response);
?>
document.querySelector('#passsection form').addEventListener('submit', function(e) {
e.preventDefault(); // stop the page from reloading
var request = new XMLHttpRequest();
let url = "/php/doLogin.php";
request.open("POST", url, true);
request.setRequestHeader("Content-Type", "application/json");
request.onreadystatechange = function() {
if (request.readyState === 4 && request.status === 200) {
var jsonData = JSON.parse(request.response);
if (jsonData.isLoggedIn) {
document.querySelector('#passsection').style.display = "none"
} else if (jsonData.error) {
document.querySelector('#passsection form .error').innerHTML = jsonData.error
}
}
};
let data = JSON.stringify({
"passfield": document.getElementById("passfield").value
});
request.send(data);
});
<section id="passsection">
<div class='error'></div>
<form method='post'>
<input style="margin-left:-5px" id="passfield" name="password" type="password" placeholder="****">
<input type="submit" id="password" name="enter" value="enter">
</form>
</section>
Hi I have two HTML forms when the one is submit a JavaScript function submits the second the one form works but the second doesn't I'm not sure if it is the forms or the post pages can any one help.
The one form sends an email this is sending the email correctly sending the correct data to the correct email address.
The second form is meant to upload a file it doesn't seem to be doing anything at all there are now errors displayed to the screen I have done a try catch and nothing is displayed i have also looked into the logs and nothing is displayed I'm
HTML
<div id="loginborder">
<form id ="upload" enctype="multipart/form-data" action="upload_logo.php" method="POST">
<input name="userfile" type="file" />
<input type="submit" onsubmit="alert()" value="dont press" disabled>
</form>
<div id="login">
<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
<input type="hidden" name="subject" value="Can you create me a Contributors account">
<input type="text" name="first_name" id="first_name" placeholder="Name">
<input type="text" name="company" id="company" placeholder="Company Name">
<input type="checkbox" id="tc" onclick= "checkbox()">
<input type="submit" id="submit" onsubmit="alert()" name="submit" value="Register" disabled>
</form>
</div>
</div>
<?php
}
else
// the user has submitted the form
{
// Check if the "subject" input field is filled out
if (isset($_POST["subject"]))
{
sleep(5);
$subject = $_POST["subject"];
$first = $_POST["first_name"];
$company = $_POST["company"];
$therest = "First name= $first" . "\r\n" . "Company= $company" . "\r\n";
}
echo "$therest <br>";
$first = wordwrap($first, 70);
mail("careersintheclassroom01#gmail.com",$subject,$name,$therest,"subject: $subject\n");
echo "Thank you for sending us feedback";
header( "refresh:5;url=index.php" );
}
?>
</body>
</html>
javascript
<script type="text/javascript">
function alert()
{
document.getElementById("upload").submit();
}
function checkbox(){
if (document.getElementById("tc").checked == true)
document.getElementById("submit").disabled = false;
else
document.getElementById("submit").disabled = true;
}
$('input[placeholder],input[placeholder],input[placeholder],input[placeholder],input[placeholder]').placeholder();
</script>
Upload_Logo.php
<html>
<head>
</head>
</html>
<?php
$uploaddir = "./images/";
echo $uploaddir;
mkdir($uploaddir, true);
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
echo "<br />";
echo " <b>Your media has been uploaded</b><br /><br /> ";
?>
the <?php echo $_SERVER["PHP_SELF"];?> on the second form calls the php at the bottom of the page this is the one that is working it is the upload_logo.php that is not currently working any help would be much appreciated
You're trying to submit 2 forms at once. That can't work, as your browser can only be directed to 1 page at a time, so your attempt to submit the upload form with JavaScript is cancelled by the contact form being submitted. I'd suggest that you move the file input into the same form as the contact fields, and handle them both in your "the user has submitted the form" section.
Something like this should do the trick:
<?php
if (!isset($_POST["submit"]))
{
?>
<div id="loginborder">
<div id="login">
<form enctype="multipart/form-data" method="POST">
<input name="userfile" type="file" />
<input type="hidden" name="subject" value="Can you create me a Contributors account">
<input type="text" name="first_name" id="first_name" placeholder="Name">
<input type="text" name="company" id="company" placeholder="Company Name">
<input type="checkbox" id="tc" onclick="checkbox()">
<input type="submit" id="submit" name="submit" value="Register" disabled>
</form>
</div>
</div>
<?php
}
else
// the user has submitted the form
{
// Check if the "subject" input field is filled out
if (!empty($_POST["subject"]))
{
sleep(5);
$subject = $_POST["subject"];
$first = $_POST["first_name"];
$company = $_POST["company"];
$therest = "First name= $first" . "\r\n" . "Company= $company" . "\r\n";
echo "$therest <br>";
$first = wordwrap($first, 70);
mail("careersintheclassroom01#gmail.com",$subject,$name,$therest,"subject: $subject\n");
echo "Thank you for sending us feedback";
header( "refresh:5;url=index.php" );
}
if (isset($_FILES['userfile']['name'])) {
$uploaddir = "./images/";
if (!file_exists($uploaddir)) {
mkdir($uploaddir, true);
}
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile);
echo "<br />";
echo " <b>Your media has been uploaded</b><br /><br /> ";
}
}
?>
</body>
try this after $uploadfile = ...
move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile);
when you submit 2nd form e-mail would be generated becausue it triggers if(isset($_POST["subject"])) condition and the code will follow the next commands.
But when you submit 1st form, it will call onsubmit="alert(); function and that function again submits the same form because of these.
function alert()
{
document.getElementById("upload").submit();
}
so you are just triggering a never ending loop.
My solution is
<script type="text/javascript">
function alert()
{
function checkbox(){
if (document.getElementById("tc").checked == true)
document.getElementById("submit").disabled = false;
else
document.getElementById("submit").disabled = true;
}
}
$('input[placeholder],input[placeholder],input[placeholder],input[placeholder],input[placeholder]').placeholder();
</script>
I'am not 100% sure about your requirement. hope you can get the point what i'am making. gl!
I trying to construct a registration page with PHP/MySQL ,first time when i tried to register as a new user it worked perfectly .after sometime time ,i checked once again with registration ,that page is not processing the data to the database .
the following code for registration page info as follows
please help me out
<?php
require_once("models/config.php");
if(isUserLoggedIn()) { header("Location: index.php"); die(); }
?>
<?php
//Forms posted
if(!empty($_POST))
{
$errors = array();
$email = trim($_POST["email"]);
$username = trim($_POST["username"]);
$password = trim($_POST["password"]);
$confirm_pass = trim($_POST["passwordc"]);
//Perform some validation
//Feel free to edit / change as required
if(minMaxRange(5,25,$username))
{
$errors[] = lang("ACCOUNT_USER_CHAR_LIMIT",array(5,25));
}
if(minMaxRange(8,50,$password) && minMaxRange(8,50,$confirm_pass))
{
$errors[] = lang("ACCOUNT_PASS_CHAR_LIMIT",array(8,50));
}
else if($password != $confirm_pass)
{
$errors[] = lang("ACCOUNT_PASS_MISMATCH");
}
if(!isValidemail($email))
{
$errors[] = lang("ACCOUNT_INVALID_EMAIL");
}
//End data validation
if(count($errors) == 0)
{
//Construct a user object
$user = new User($username,$password,$email);
//Checking this flag tells us whether there were any errors such as possible data duplication occured
if(!$user->status)
{
if($user->username_taken) $errors[] = lang("ACCOUNT_USERNAME_IN_USE",array($username));
if($user->email_taken) $errors[] = lang("ACCOUNT_EMAIL_IN_USE",array($email));
}
else
{
//Attempt to add the user to the database, carry out finishing tasks like emailing the user (if required)
if(!$user->userPieAddUser())
{
if($user->mail_failure) $errors[] = lang("MAIL_ERROR");
if($user->sql_failure) $errors[] = lang("SQL_ERROR");
}
}
}
if(count($errors) == 0)
{
if($emailActivation)
{
$message = lang("ACCOUNT_REGISTRATION_COMPLETE_TYPE2");
}
else {
$message = lang("ACCOUNT_REGISTRATION_COMPLETE_TYPE1");
}
}
}
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Registration | <?php echo $websiteName; ?> </title>
<?php require_once("head_inc.php"); ?>
</head>
<body>
<div class="modal-ish">
<div class="modal-header">
<h2>Sign Up</h2>
</div>
<div class="modal-body">
<div id="success">
<p><?php echo $message ?></p>
</div>
<div id="regbox">
<form name="newUser" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<p>
<label>Username:</label>
<input type="text" name="username" />
</p>
<p>
<label>Password:</label>
<input type="password" name="password" />
</p>
<p>
<label>Re-type Password:</label>
<input type="password" name="password" />
</p>
<p>
<label>Email:</label>
<input type="text" name="email" />
</p>
</div>
</div>
<div class="modal-footer">
<input type="submit" class="btn btn-primary" name="new" id="newfeedform" value="Register" />
</div>
</form>
</div>
<div class="clear"></div>
<p style="margin-top:30px; text-align:center;">
Login / Forgot Password? / Home Page
</p>
</body>
</html>
also check your connection page properly..
add these kind of queries to your regrestration page..
mysql_query("insert into <your_table>(email,username,password) values('$email',' $username',' $password')");
Where exactly is the MySQL code? You've done your checks for the data but that's all it does. It is not being sent to a database. It's only being validated.
PHP Code :
<?php
if(isset($_POST['submit']))
{
$sent = mail($to, $subject, $email_message, $headers) ;
if($sent)
{
echo '<script> cleartext(); </script>';
}
}
?>
JavaScript :
<script type="text/javascript">
function cleartext()
{
alert(document.getElementById('name').value);
document.getElementById('name').value = '';
document.getElementById('email').value = '';
document.getElementById('phone').value = '';
document.getElementById('message').value = '';
alert(document.getElementById('name').value);
}
</script>
Source Code :
<form action="#" method="post" class="margin-bottom" target="hidden" id="contact">
<div class="row">
<div class="col-lg-6 col-sm-6">
<input type="text" placeholder="Your Name*" id="name" name="name" />
</div>
<div class="col-lg-6 col-sm-6">
<input type="text" placeholder="Your Email*" id="email" name="email" />
</div>
<div class="col-lg-6 col-sm-6">
<input type="text" placeholder="Your Phone*" id="phone" maxlength="13" name="phone" />
</div>
</div>
<textarea placeholder="Message" id="message" name="message"></textarea>
<input type="submit" id="clear" name="clear" class="button" value="Clear " onClick="cleartext();">
<input name="submit" type="submit" class="button" id="submit" onClick="MM_validateForm('name','','R','email','','RisEmail','phone','','RisNum','message','','R');return document.MM_returnValue" value="Submit ">
</form>
Problem
I want to clear all input text after sending a mail on submit.on submit I am calling cleartext() from my PHP code.but on that time I am not being able to clear my input text value.And when I call cleartext() method on clear button it's works proper.if somebody have idea about it so please help me.
You missed script type. that might be the problem.
<?php
//stuff to do
echo '<script type="text/javascript">
cleartext();
</script>';
?>
see this How to call a JavaScript function from PHP?
For button to clear fields, use type "reset" instead of "submit"
<input type="reset" value="Clear" />
And to clear the fields after submit, try redirecting
if($sent) {
header('Location: ' . $_SERVER['PHP_SELF']);
exit(0);
}
you can put a setTimeout in the onClick of the js function that clear the form
<input name="submit" type="submit" class="button" id="submit" onClick="setTimeout( MM_validateForm, 1000 )" >
Try this:
PHP:
if(isset($_POST['submit']))
{
$sent = mail($to, $subject, $email_message, $headers) ;
if($sent) $b_sent = true;
}
Javascript:
function cleartext()
{
alert(document.getElementById('name').value);
document.getElementById('name').value = '';
document.getElementById('email').value = '';
document.getElementById('phone').value = '';
document.getElementById('message').value = '';
alert(document.getElementById('name').value);
}
var bClear = <?= $b_sent? 'true':'false' ?>;
if (bClear) cleartext();
you using submit button for clear, it shouldn't work because the form will be submitting its values
and the way is you should make it return false in onclick in order to prevent submitting form.
your code should be like this:
<input type="submit" id="clear" name="clear" class="button" value="Clear " onClick="cleartext(); return false;">
Hello I am wondering if this is possible? Having two different forms on the same page using the jquery post to send it php to do some checking. The first from works flawlessly, however when I go to the second form I get an error saying it is an undefined variable but I am using the exact same method I used for the first form. It will load anything echoed in the php page for the feed form but will not echo back what I am typing in. Is there a better, more correct way to do it?
This is not for a real site, just testing for a project I am working on.
HTML:
<form action="php/signup.php" method="post" class="form-inline" name="signupForm">
<input type="text" maxlength="20" name="username" id="user_in">
<input type="password" maxtlength="20" name="password" id="pass_in">
<input type="submit" name="submit" Value="Sign Up">
</form>
<div id="feedback"></div> <!-- Feedback for Sign Up Form -->
<br /><br />
<form name="feedForm">
<input type="text" id="feed_in" name="feed_me_in" placeholder="feed">
<div id="feedme"></div> <!-- FEEDback for feed form -->
</form>
<script src="js/jquery-1.9.1.js"></script>
JavaScript:
<script>
$(document).ready(function() {
$('#feedback').load('php/signup.php').show();
//SIGN IN FORM
$('#user_in, #pass_in').keyup(function() {
$.post('php/signup.php', { username: document.signupForm.username.value,
password: document.signupForm.password.value },
function(result) {
$('#feedback').html(result).show
});
});
$('#feedme').load('php/feed.php').show();
//FEED FORM
$('#feed_in').keyup(function() {
$.post('php/feed.php', { feed: document.feedForm.feed_me_in.value },
function(result) {
$('$feedme').html(result).show
});
});
});
</script>
PHP for Feed Form:
<?php
$feed = mysql_real_escape_string($_POST['feed']);
if(isset($feed)) {
echo $feed;
} else {}
?>
PHP for the Sign Up Form:
<?php
if(isset($_POST['username'])) {
include_once('connect.php'); //Connect
$username = mysql_real_escape_string($_POST['username']);
$sql1 = "SELECT username FROM users WHERE username='$username'";
$check = mysql_query($sql1);
$numrows = mysql_num_rows($check);
if(strlen($username)<=4) {
echo "Username is too short";
} elseif($numrows == 0) {
echo "Username is available";
} elseif($numrows > 0) {
echo "Username is already taken";
}
} else {
echo "Please type a username";
}
?>
$('$feedme').html(result).show
should be
$('#feedme').html(result).show();