$_POST not working when JQuery is included - php

So, I'm having an issue where my $_POST variable does not work whenever I have JQuery included on the page.
If I comment out the JQuery link it works fine. I have asked a similar question before, but did not get a full answer and have used a workaround which does not work with this page.
My full code is as followed:
<?php
if(isset($_POST['home-register-submit']))
{
echo '<script>alert("Works!");</script>';
}
?>
<div id='home-register'>
<div id='home-register-title'><h4>Aanmelden</h4></div>
<hr>
<form method="POST">
<label>Voornaam:</label> <div id='home-register-input'><input type="text" name='home-register-fname'></div><br />
<label>Achternaam:</label> <div id='home-register-input'><input type='text' name='home-register-lname'></div><br />
<label>Leeftijd:</label> <div id='home-register-input'><input type='' name='home-register-age'></div><br />
<label>Telefoon:</label> <div id='home-register-input'><input type='tel' name='home-register-phone'></div><br />
<label>Email:</label> <div id='home-register-input'><input type='email' name='home-register-email'></div><br />
<button class="btn btn-default btn-sm" type='submit' name='home-register-submit'>Versturen</button>
</form>
</div>
<!-- jQuery -->
<script src="js/jquery.js"></script>

Fixed it by excluding a file called clean-blog.min.js which apparently blocked my page from using $_POST.

Related

Moving comment box on the webpage

My question is rather simple. But I am seriously stack as I have never done moving commentary box and nor applying it before.
Here is the code of a comment box I got from a free source:
<form method='post'>
<div class="name">NAME: <input type='text' name='name' id='name' /><br /></div>
Comment:<br />
<textarea name='comment' id='comment'></textarea><br />
<input type='hidden' name='articleid' id='articleid' value='<? echo $_GET["id"]; ?>' />
<input type='submit' value='Submit' />
</form>
And now I am trying to position it so it fits nicely on my page. I managed to move it on the page by putting the whole piece of code in a div, but when it comes to Name and comment fields even I am not sure what to do. Would be great to see some insights.
I would recommend using Bootstrap to make a clean design of your form.
Add Bootstrap to your Head of your HTML page:
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
Then you can modify your form code to use Bootstrap containers, form-groups and form-control classes to structure and style your code. See here for more details. form-group group label's and input fields together removing the need to create custom css or use html breaks to change the structure of the page. form-control adds Bootstrap styling to the input fields.
I created an example from your code sample above using Bootstrap:
<div class="container">
<div class="row">
<div class="col-md-12">
<h1>Example Form</h1>
<form>
<div class="form-group">
<label for="nameInput">Name</label> <input class="form-control" id="nameInput" placeholder="Name" type="text">
</div>
<div class="form-group">
<label for="commentInput">Comment</label>
<textarea class="form-control" id="commentInput"></textarea>
</div>
<input id='articleid' name='articleid' type='hidden' value='<? echo $_GET["id"]; ?>'>
<button class="btn btn-primary" type="submit">Submit</button>
</form>
</div>
</div>
See code pen to view the form - http://codepen.io/jamesg1/pen/ALAZKg

Adding "widgets" live to a site

I'm currently in the process of creating a website with various widgets that I want to be able to add/remove on the fly.
I'm using jQuery to achieve this(planning to at least).
The simplest widget is the login widget. Which looks like this:
<div id="widget1">
<div class="close"></div>
<div id="widget1_title"></div>
<div id="widget1_content">
<form method='post' action='index.php'>
<div id="widget1_username">
Username<input type='text' maxlength='16' name='user' value='' />
</div>
<div id="widget1_password">
Password<input type='password' maxlength='16' name='pass' value='' />
</div>
<div id="widget1_submit">
<input type='submit' value='LOGIN' />
</div>
</div>
</div>
This is stored in a file called login_widget.html. Since a login-widget is something that should be preloaded on the site(without the user having to bring it up) I have my index.php file looking like this:
<body>
<div id="container">
<div id="content">
<div id="sidebar">
<div id="login-sidebar"></div>
</div>
<div id="header">
<div></div>
<div></div>
<div id="header-logo"></div>
</div>
<div id="main-content">
<div id="widget1">
<div class="close"></div>
<div id="widget1_title"></div>
<div id="widget1_content">
<form method='post' action='index.php'>
<div id="widget1_username">
Username<input type='text' maxlength='16' name='user' value='' />
</div>
<div id="widget1_password">
Password<input type='password' maxlength='16' name='pass' value='' />
</div>
<div id="widget1_submit">
<input type='submit' value='LOGIN' />
</div>
</div>
</div>
</div>
<div id="footer">
</div>
</div>
</div>
</body>
(Originally it's in a <?php include_once('login_widget.html'); ?> ).
I then have a button set up that I press to load in the code from the file.
Here's the jQuery code:
$(document).ready(function(){
$(".close").click(function(){
$(this).parent().remove();
});
$("#login-sidebar").click(function(){
$.ajax({
url: 'login_widget.html'
}).done(function(data) {
$('#main-content').append(data);
});
});
});
And now to the problem. This works in that it posts the html-code and it shows on the site, but the problem is that the "close button" doesn't work anymore. So I can't remove the new created html-elements using the class named "close".
I'm very new to jQuery, but I figure this is one way of doing it, but I'm missing something?
Thanks in advance!
Either build your widgets as modules that bind their own events when they are created to remove themselves, or use event delegation. Event delegation would be the simplest given the very basic code that you are using thus far.
$("#main-content").on("click",".close",function(){
$(this).parent().remove();
})

form is sending variables by get method

i got a form.
<form id="site-contact-form">
<div>
<div class="wrapper"><span>Ձեր անունը:</span>
<div class="bg">
<div>
<input type="text" class="input" name="contactname" id="contactname" />
</div>
</div>
</div>
<div class="wrapper"><span>Ձեր E-mail-ը:</span>
<div class="bg">
<div>
<input type="text" class="input" name="email" id="email" />
</div>
</div>
</div>
<div class="textarea_box"><span>Տեկստ:</span>
<div class="bg">
<div>
<textarea cols="1" rows="1" name="message" id="message"></textarea>
</div>
</div>
</div>
<div style="clear:both;"></div>
<button id="sub" name="submit">ուղարկել</button>
</div>
</form>
As you see i got no action on it, no method. But when im clicking on button, its refreshing the page (like when it have action) and adding to URL ?contactname=&email=&message=&submit= ... I never met this problem before, why it is sending variables? I dont have any php on page yet...
Beacause the default method is GET and the URL is the same of the page. Use:
<form method="POST" action="/reactor">
<!-- .... -->
</form>
Actually, the action property is mandatory.
Please correct the code with:
<form id="site-contact-form" method="post">
And the submitted values will not be displayed on the URL.
If you do not specify the form method, it will "get" by default.
This is happening here.
Change the type of button as button
<button id="sub" name="submit" type="button">ուղարկել</button>

Form Submit without Refreshing using Plugin (malsup)

I just created a form submit without refreshing using this plugin : http://malsup.github.com/jquery.form.js
The simple example can be seen at
http://jquery.malsup.com/form/
I don't what I was doing, it was worked fine before until I modified something that I don't remember.
When I click SAVE, it should be processed on the background and it should stay on the same page. But now, it doesn't and it redirect me to the action page (process-001.php)
I created another page using the same method, and it works just fine.
Can you see if I'm doing it wrong?
Here is the form :
<div id="submit-form">
<form action="web-block/forms/process-001.php" id="select-block" class="general-form" method="post" enctype="multipart/form-data">
<div class="input-wrap">
<input class="clearme" name="Headline" value="<?php echo $Headline;?>" id="headline"/>
</div>
<div class="input-wrap">
<textarea class="clearme" name="Sub-Headline" id="subheadline"><?php echo $SubHeadline ;?></textarea>
</div>
<label>Bottom Left Image</label>
<div class="up-mask">
<span class="file-wrapper">
<input type="file" name="Pics" class="photo" id="pics" />
<span class="button">
<span class="default-txt">Upload Photo</span>
</span>
</span>
</div><!-- .up-mask -->
<input type="hidden" name="Key" id="Key" value="<?php echo $Key;?>"/>
<input type="submit" class="submit-btn" value="SAVE" />
<span class="save-notice">Your changed has been saved!</span>
</form>
</div>
The Javascript :
<script>
// wait for the DOM to be loaded
$(document).ready(function() {
// bind 'myForm' and provide a simple callback function
$('#select-block').ajaxForm(function() {
alert("Thank you for your comment!");
});
});
</script>

Submtting An animated form using jquery ajax and php to mysql database

I have been having problems for the past two days trying to submit a form using jquery ajax, php to mysql database. I reused a code for an animation form which I found online. The html code as follows (file name: "Slider.html"):
<html>
<body>
<div id="form_wrapper" class="form_wrapper">
<form class="register" style="right:-500px;backgroundcolor:grey;">
<h3>Register</h3>
<div class="column">
<div>
<label>First Name:</label>
<input type="text" />
<span class="error">This is an error</span>
</div>
<div>
<label>Last Name:</label>
<input type="text" />
<span class="error">This is an error</span>
</div>
</div>
<div class="column">
<div>
<label>Username:</label>
<input type="text"/>
<span class="error">This is an error</span>
</div>
<div>
<label>Email:</label>
<input type="text" />
<span class="error">This is an error</span>
</div>
<div>
<label>Password:</label>
<input type="password" id="r_passwordField"/>
<span class="error">This is an error</span>
</div>
</div>
<div class="bottom">
<div class="remember">
<input type="checkbox" />
<span>Send me updates</span>
</div>
<input type="submit" id="registrationform" value="register"/>
<a href="index.html" rel="login" class="linkform">You have an account already? Log in
here</a>
<div class="clear"></div>
</div>
</form>
<form class="login active">
<h3>Login</h3>
<div>
<label>Username:</label>
<input type="text" />
<span class="error">This is an error</span>
</div>
<div>
<label>Password: <a href="forgot_password.html" rel="forgot_password" class="forgot
linkform">Forgot your password?</a></label>
<input type="password" id="l_passwordField" size="10"/>
<span class="error">This is an error </span>
</div>
<div class="bottom">
<div class="remember"><input type="checkbox" /><span>Keep me logged in</span></div>
<input type="submit" id="loginform" value="Login"></input>
<a href="register.html" rel="register" class="linkform">You don't have an account yet?
Register here</a>
<div class="clear"></div>
</div>
</form> <form class="forgot_password">
<h3>Forgot Password</h3>
<div>
<label>Username or Email:</label>
<input type="text" />
<span class="error">This is an error</span>
</div>
<div class="bottom">
<input type="submit" id="forgortform" value="Send reminder"></input>
Suddenly remebered? Log in here
<a href="register.html" rel="register" class="linkform">You don't have an account?
Register here</a>
<div class="clear"></div>
</div>
</form>
</div>
<div id="graph-wrapper" class="graph-container" style="display:none">
<h3>Standard Diviation Bell Curve<h3>
<div class="graph-info">
Visitor
<span></span>
</div>
<div class="graph-container">
<div id="graph-lines"></div>
<div id="graph-bars"></div>
</div>
</div>
</div>
</body>
</html>
jquery ajax code as follows (jquery script on the same file as the html code):
<script>
$(document).ready(function(){
//animated code and other codes omitted
("#registrationform").click(function(){
$.post("postdata.php",
{fname:"Ibrahim",lname:"Ahmadu",email:"ibrostay#yahoo.com",uid:"ibro2stay",pwd:"ibro2stay",mean:"0.1",varience:"0.1",sdev:"0.
1",duration:"0.1"},function(responce){
if(responce==0){
alert("There was an error updating the record");
}else{
alert("update successful");
}
});
});
});
</script>
Below is the php code (php code file name: "postdata.php"):
<?php
$fname=$_REQUEST["fname"];
$lname=$_REQUEST["lname"];
$email=$_REQUEST["email"];
$uid=$_REQUEST["uid"];
$pwd=$_REQUEST["pwd"];
$mean=$_REQUEST["mean"];
$varience=$_REQUEST["varience"];
$sdev=$_REQUEST["sdev"];
$duration=$_REQUEST["duration"];
$con=mysql_connect('localhost','root','');
if(!$con)
{
die("Error Connecting to database;"+mysql_error());
}
$database=mysql_select_db('mydb');
if(!$database)
{
die("Error Connecting to database;"+mysql_error());
}
$update = mysql_query("insert into users values('$fname','$lname','$email','$uid','$pwd','$mean','$varience','$sdev','$duration')");
if(!$update)
{
die("Update wasn't Success full"+mysql_error());
}
echo "update successfull";
mysql_close($con);
?>
Whenever I click the register button nothing happens. The page only refreshes back to the login form since it has class "active" as the default form, and the browser address bar changes from this url: "localhost/slider.html" to this url: "localhost/slider.html?".
I hope my question was explicit enough, because I need an urgent answer, as this is my thesis project and I am running out of options.
and the browser address bar changes from this url: "localhost/slider.html" to this url: >"localhost/slider.html?".
use e.preventDefault();
$("#registrationform").click(function(e){
^
|
|_______________ you forgot the $
then add,
`e.preventDefault();
check javascript console and you will see all the errors. you have to eliminate one by one. For the start, check above.
advice: when you copy/paste codes, atleast try to know the structures. sometimes it wil conflict with your code and may stop running your code.
Looks like your Javascript has some errors and not executing hence. So, the form submit resorts to its default behavior - that is, submit to the tag's action attribute. Since you have not specified the action attribute in the tag, it will post the parameters to the current page.
You are getting the url localhost/slider.html? because of two reasons:
You have not specified method="post" in your . So, the form is trying to submit as GET. That is why the appended "?"
You do not have names for the inputs. So, the query string is empty. You need to specify name attribute for every input.
What you have to do first is to debug your Javascript. Use Firebug in Firefox or the console in Chrome or any similar tools to capture your JS errors and also examine your Ajax requests and responses.
Also, make sure that the last statement in the "click" function is a :
return false;
to force the form to stay on the page.
Another important thing is, as #steve pointed out, add method and action attributes to your tag. This will make sure that your script will not fail entirely on a JS disabled browser or on any such conditions.

Categories