Jquery ajax responsed content does not write into the html form - php

HTML code :
I echo this content in php file, In result i get only the 'Contact Us' Remaining part does not display the html form
<div > Contact Us
<form name="profile" action="" method="post">
Name : <input type="text" name="fullname" id="fullname" />
Email : <input type="text" name="email" id="email" />
Mobile : <input type="text" name="mobile" id="mobile" />
</form>
</div>
Jquery code response :
$.post("contact_us.php",{ user_id:$('#user_id').val(),typ:'contact_us' },
function(data) {
$('#contact_us').html(data);
});
In contact_us id does not write the contact us form, Please anyone help me

Have you tried using Firebug?
http://getfirebug.com/
That will let you see what the script is returning.
UPDATE:
You are referencing a container with ID = "contact_us"
$('#contact_us').html(data);
You need to set the ID of the div you want to replace the content in.
<div id="contact_us"></div>

Your HTML isn't valid. You need to close the div tag
<div >
Contact Us
<form name="profile" action="" method="post">
Name : <input type="text" name="fullname" id="fullname" />
</form>
</div>

Related

Saving and displaying user input using HTML and PHP

I am trying to save a Users data from an input field so that it can be displayed later in their profile of a webpage, for example the user inputs data of a cinema(name, address) and can see it later under Saved Restaurants and call up the previously saved information. Can the PHP and HTML code be written together in one .PHP file?
So far I have this:
<html lang>
<head>
<link rel="stylesheet" href="css/addOrEditCinemaPage.css">
</head>
<?php include "php/head.php" ?>
<?php include "php/navigation.php" ?>
<body>
<div class="myForm">
<form>
<h2>Add or Edit a Cinema</h2>
<label for="name"><b>Name of Cinema</b></label>
<input type="text" placeholder="Enter name">
<label for="str"><b>Street</b></label>
<input type="text" placeholder="Enter street">
<label for="nr"><b>Nr.</b></label>
<input type="number" placeholder="Enter Nr.">
<label for="plz"><b>Post Code</b></label>
<input type="number" placeholder="Enter Post Code"><br><br>
<label for="ct"><b>City</b></label>
<input type="text" placeholder="Enter City">
<label for="sta"><b>State</b></label>
<input type="text" placeholder="Enter State">
<label for="descr"><b>Description</b></label><br>
<textarea placeholder="Enter Discription"></textarea>
<div class="imagebutton">
Add Image
</div>
<button type="submit">Save</button>
</form>
</div>
<?php include "php/footer.php" ?>
</body>
</html>```
Can the PHP to save and display to input infomration also be written here?
Yes, you can by setting the 'action' attribute of the form to the same file, and by setting the 'method' attribute to POST.
Instead of using
<form>
use
<form action="<?PHP echo $_SERVER['php_self'];?>" method="POST">
Then, set the 'name' attribute of each input.
For example, Instead of using
<input type="text" placeholder="Enter name">
use
<input type="text" name="name" placeholder="Enter name">
You'll also have to set the 'name' attribute of the submit button to 'submit':
<button type="submit" name="submit">Save</button>
Once you've done that, the PHP code to access the form data would be:
if (isset($_POST['submit'])) {
echo $_POST['name'];
}
send your data with html to another php page
you must use get or post for sending form data to php page
like below

HTML and PHP - How to seperate 2 forms on a single page?

I'm pretty new to HTML and currently I'm making an assignment where I have to make a HTML Form for data input and write the input to a file using PHP.
The writing to file works, but I getting the following comment on my code:
You have tested whether something has been posted, but what happens to your code if there are 2 forms on your page?
I kinda know what is being meant with this, but I am not that fluent to know how to solve this issue or where this comes from in my code.. Does this has to do with the action of the form set to $_SERVER["PHP_SELF"], the name of the submit button set wrong or anything else?
I've tried looking online for HTML forms and how to have 2 on the same page.. but I could not find anything really helpfull. If anyone can help or maybe point me to some info that explains this in detail (and with examples preferably) that would be great!
Here is just my HTML form as I have it with the PHP part checking for the submit. Rest of the code I left out as it is not relevant..
<html>
<head>
<title>Save and show data</title>
</head>
<body>
<h2>Fill in the form below</h2>
<form method="post" action="<?php $_SERVER["PHP_SELF"]; ?>">
<label for='name'>Naam:</label><br>
<input type="text" id="name" name="name" placeholder = "John Doe" required><br>
<label for='address'>Adres:</label><br>
<input type="text" id="address" name="address" placeholder = "Sunset lane 10" required><br>
<label for='zip'>Postcode:</label><br>
<input type="text" id="zip" name="zip" placeholder = "15922" required><br>
<label for='residence'>Woonplaats:</label><br>
<input type="text" id="woonplaats" name="woonplaats" placeholder = "Somewhere" required><br>
<label for='phone'>Telefoonnummer:</label><br>
<input type="tel" id="phone" name="phone" placeholder = "0678945124" required><br>
<label for='email'>E-mail:</label><br>
<input type="email" id="email" name="email" placeholder = "johndoe#email.com" required><br><br>
<input type="submit" name="submit_data" value="Save"><br>
</form>
<?php
if(isset($_POST["submit_data"]))
{
// magic stuff happens here
}
Do you mean something like this? Each submit button has its own validation.
<?php
if (isset($_POST['submit_data'])) {
//first form
}
if (isset($_POST['edit_data'])) {
//second form
}
?>
<form method="post" action="<?php $_SERVER["PHP_SELF"]; ?>">
<label for='name'>Naam:</label><br>
<input type="text" id="name" name="name" placeholder = "John Doe" required><br>
<input type="submit" name="submit_data" value="Save"><br>
</form>
<form method="post" action="<?php $_SERVER["PHP_SELF"]; ?>">
<label for='name'>Naam:</label><br>
<input type="text" id="name" name="name" placeholder = "John Doe" required><br>
<input type="submit" name="edit_data" value="Edit"><br>
</form>

Ajax call on submit refreshing page

Problem
My page seems to be defaulting to my PHP code instead of being handled asynchronously via ajax. As of now, my page just reloads as a blank screen, the input array successfully got passed over to the PHP, which is comforting, but I seem to just be doing something wrong with the ajax.
What I've tried
After viewing this link and this link, I still cant seem to get this figured out.
Current Standing
Here is the HTML:
<form class="cmxform" id="commentForm" method="" action="">
<fieldset>
<legend>Please provide your name, email address (won't be published) and a comment</legend>
<p>
<label for="spinner">How much do you love science? (optional)</label>
<input id="spinner" name="spinner">
</p>
<p>
<label for="cname">Name (required, at least 2 characters)</label>
<input id="cname" name="name" minlength="2" type="text" required>
</p>
<p>
<label for="cemail">E-Mail (required)</label>
<input id="cemail" type="email" name="email" required>
</p>
<p>
<label for="curl">URL (optional)</label>
<input id="curl" type="url" name="url">
</p>
<p>
<label for="aoi">Area of Interest (optional)</label>
<input id="aoi" type="text" name="aoi">
</p>
<p>
<label for="currprobs">Current Problems (optional)</label>
<input id="currprobs" type="text" name="currprobs">
</p>
<p>
<label for="ccomment">Your comment (required)</label>
<textarea id="ccomment" name="comment" required></textarea>
</p>
<p>
<input id="launch" type="submit" value="Submit">
</p>
</fieldset>
</form>
<div id="results"></div>
Here is the javascript:
$(document).ready(function(){
$("#commentForm").submit(function(event){
/*
var formData = {
'name' : $('input[name=name]').val(),
'email' : $('input[name=email]').val(),
};
*/
alert("SUCCESS?");
});
});
PHP code to return a div after the user has been validated:
<?php
$username = isset($_POST['name'])? trim($_POST['name']):'';
$email= isset($_POST['email'])? trim($_POST['email']):'';
echo '<div id=dynamicDiv>
<p>Hello, '.$username.'!</p>
<p>We look forward to contacting you at, '.$email.'</p>
</div>';
?>
Feedback
Any thoughts are appreciated, even intellectual conversation. Code snippets are idea, but I appreciate anything the community is able
Edit 1
I motified the javascript and php to reflect knowledgable input and best practice for PHP and AJAX calls, however the issue still remains.
Edit 2
My goal now is to just get an alert() statement working inside of my javascript, ajax will be the next step.
I think problem is here
$("#results").html = html_i;
You try change to
$("#results").html(html_i);
And in php you should check name and email
$username = isset($_POST['name'])? trim($_POST['name']):'';
$email= isset($_POST['email'])? trim($_POST['email']):'';
You should change input type="submit" to input type="button" and call Ajax on onclick function, Its resolve your problem because submit button submit form and Ajax call pending not reached to 400 state.

PHP Form Redirect to error link

Using HTML form with action = "www.google.com", but it redirect to link including localhost/www.google.com . My example code is for your reference. Please anyone help me.
My Forms as follows :
<form method="POST" action="www.google.com">
<p>
<label for="name">Name:</label>
<input type="text" name="name" id="name">
</p>
<p>
<label for="email">Email:</label>
<input type="email" name="email" id="email">
</p>
<p>
<label for="comments">Comments:</label>
<textarea name="comments" id="comments"></textarea>
</p>
<p>
<input type="submit" name="send" id="send" value="Send Comments">
</p>
</form>
But, the file when click send button, it redirects to
" http://localhost/basic/www.google.com" error page. Can any one suggest me to resolve the issue.
It is not allowed to send POST request directly to google.com. Why do you want to do this? In the action argument you should have own script which can process request from your form.
Replace your form action
From
action="www.google.com"
To actual link
action="https://www.google.com"

unsubscribe html form using php and my sql

I have an html form where people can subscribe to a mailing list. The form includes form validation and when the form is submitted, the data is stored in a database using My SQL.
Here is the code on the index.html page where the form is
<form id="subscribe-form" action="send.php" method="post">
<p id="status"></p>
<div>
<label for="title">Title:</label>
<select class="uniform" name="title" id="title">
<option>Please Choose</option>
<option>Mr</option>
<option>Mrs</option>
<option>Miss</option>
<option>Ms</option>
</select>
</div>
<div>
<label for="firstName">First name:</label>
<input type="text" id="firstName" name="firstName" />
</div>
<div>
<label for="surname">Surname:</label>
<input type="text" id="surname" name="surname" />
</div>
<div>
<label for="email">Email:</label>
<input type="text" id="email" name="email" />
</div>
<div>
<label for="phone">Contact Number:</label>
<input type="text" id="phone" name="phone" />
</div>
<div>
<label for="title">How did you hear about us?</label>
<select class="uniform" name="refer" id="refer">
<option>Please Choose</option>
<option>Google</option>
<option>Yahoo</option>
<option>Word of Mouth</option>
<option>Others</option>
</select>
</div>
<div>
<input type="checkbox" name="news_updates" value="1" />
I'd like to hear about the latest news and events updates</div>
<div>
<input class="button" type="submit" value=""/>
</div>
</form>
Here is the code for send.php
<?php
include ('connection.php');
$sql="INSERT INTO form_data (title,firstName, surname, email, phone, refer, news_updates)
VALUES
('$_POST[title]', '$_POST[firstName]','$_POST[surname]','$_POST[email]','$_POST[phone]','$_POST[refer]','$_POST[news_updates]')";
if (!mysql_query($sql, $connected))
{
die('Error: ' . mysql_error());
}
mysql_close($connected);
?>
I would like to make another html (unsubscribe.html) page where people can unsubscribe by entering their email address so that their email address would match the corresponding email that is in the database already and remove it from the My Sql database .
I found this tutorial which was kind of helpful -
http://www.phpsuperblog.com/php/delete-records-from-mysql-database-with-html-form-and-php/
and this is the form on my unsubscribe.html page.
<form id="unsubscribe_form" action="delete.php" method="post">
<div>
<label for="email_remove">Email:</label>
<input type="text" id="email_remove" name="email_remove" />
</div>
<div>
<input name="delete" type="submit" id="delete" value="" class="unsubscribe_btn">
</div>
</form>
but when I enter method="post" in the unsubscribe form. The data from the form on the subscribe / index.html does not get stored in My Sql, instead they come up as blank.
So I am guessing I can't have two "post" method maybe??
If someone could guide me in the right direction that would be much appreciate. Thanks.
I guess you are at your learning stage. So, I will suggest you to have a check for POST method being called on the page which receives the post.
Example: in your subscribe.php
you should have :
<input class = "button" type = "submit" value = "Subscribe" name = "subscribe" />
in send.php
you must do:
if(!isset($_POST['subscribe'])
{
header('location: subscribe.html');
}
You must use isset for your pages.
If you could display your delete.php, perhaps I can edit this post and assist you further but, so far... A check is required and you can use as many forms as many you like (even on one page) but, make sure they all have different id/names.
Your delete.php script should be:
<?php
require ('connection.php'); // User require for important functions so that if not found, it throws fatal error
$email = $_POST['email_remove'];
// Check for isset POST
$query = "DELETE from form_data WHERE email = '".$email."'";
if(mysql_query($query)){ echo "deleted";} else{ echo "fail";}
?>
your delete.php seems OK to me.
Can add the following to Line 2
echo "";
print_r($_POST);
and post array in comments?

Categories