So I am dealing with some survey pages. First my makesurvey.php reads all questions and then create page for editing questions and creating new questions. Each question is in own form and it has own submit. Then admin of survey can on page makessurvey.php fix the existing questions and also can add new question which is subbmited to handlesurvey.php wich makes updates to database. That means on one page I have let's say 20 forms with submit button. Now I realise that I have encountered more problems. The survey will be handle only by one admin. And this survey apllication needs to work dynamic, because admin can enter some new question, later can delete some questions. After some time add new questions, then delete and so on.
PROBLEMS:
1.Problem: On page makesurvey when admin wants to edit survey how to make just one jQuery function, which will first validate then submmit form to handlesurvey.php. (admin will edit one question at time and press submit). The other option is to generate so many js functions for each form but I think that isn't appropriaty way, but I think it would work fine.
2.Problem: When admin adds/edits some question how should my handlesurvey.php know which variable is in $_POST[<some_variable>].
Generated makesurvey.php page:
<script type="text/javascript">
$("form#updateQuestion").submit(function(){
var SloQuestion = $("#SloQuestion").val();
var typeQuestion=$('#typeQuestion').val();
var RadioValue1=$('#RadioValue1').val();
var RadioValue2=$('#RadioValue2').val();
var RadioValue3=$('#RadioValue3').val();
var RadioValue4=$('#RadioValue4').val();
//HERE I WILL DO SOME VALIDATION
/* $.post( "editSurveyHandle.php", $("form#updateQuestion").serialize())
.done(function( data ) {
alert("You have changed question!");
/* });
return false;*/
});
</script>
</head>
<html>
<body>
<form id='updateQuestion1' name='updateQuestion1' method='post' action=''>
<input type='hidden' name='hidden' value='question1'/><table width='600px'><tr></tr>
<tr><td width='50%' valign='top'><label for='question1'><h2>CURRENT QUESTION: </h2></label></td></tr>
<tr><td>SLOVENIAN </td><td width='50%' valign='top'> <label for='question1'>Some question 1 *</label></td></tr>
<tr><td width='50%' valign='top'>TYPE QUESTION</td><td width='50%' valign='top'>RADIO BUTTON</td></tr>
<tr><td><h4>DELETE QUESTION</h4></td><td><input type='checkbox' id='deleteQuestion' name='deleteQuestion' value='delete'/></td></tr>
<tr><td><h4>CHANGE QUESTIOn</h4></td></tr>
<tr><td><label>SLOVENIAN</label></td><td><input type='text' id='SloQuestion' name='SloQuestion' size='50' value ='Velikost sobe *'></td></tr>
<tr><td>TYPE QUESTION </td><td><select id='typeQuestion' name='typeQuestion'>
<option value='text'>Malo vnosno polje</option>
<option value='textarea'>Textarea</option>
<option selected='selected' value='radio'>Izbirino polje</option>
</select></tr>
<tr><td></td><td><input type='text' id='RadioValue0' name='RadioValue10' size='50' value ='Bad'></td></tr>
<tr><td></td><td><input type='text' id='RadioValue1' name='RadioValue11' size='50' value ='Sufficiently'></td></tr>
<tr><td></td><td><input type='text' id='RadioValue2' name='RadioValue12' size='50' value ='Dobro'></td></tr>
<tr><td></td><td><input type='text' id='RadioValue3' name='RadioValue13' size='50' value ='Excelent'></td></tr>
<tr><td></td><td><input type ='submit' id='submit' name='submit' value='Izvedi spremembe'/></td></tr>
</table>
</form>
<form id='updateQuestion2' name='updateQuestion2' method='post' action=''>
<input type='hidden' name='hidden' value='question2'/><table width='600px'><tr></tr>
<tr><td width='50%' valign='top'><label for='question2'><h2>CURRENT QUESTION:</h2></label></td></tr>
<tr><td>SLOVENIAN </td><td width='50%' valign='top'> <label for='question2'>Some question 2</label></td></tr>
<tr><td width='50%' valign='top'>TYPE QUESTION</td><td width='50%' valign='top'>RADIO BUTTON</td></tr>
<tr><td><h4>DELETE QUESTION</h4></td><td><input type='checkbox' id='deleteQuestion' name='deleteQuestion' value='delete'/></td></tr>
<tr><td><h4>CHANGE QUESTIOn</h4></td></tr>
<tr><td><label>SLOVENIAN</label></td>
<td><input type='text' id='SloQuestion' name='SloQuestion' size='50' value ='Some question 2'></td></tr>
<tr><td>TYPE QUESTION </td><td><select id='typeQuestion' name='typeQuestion'><option value='text'>Text field</option>
<option value='textarea'>Textarea</option><option selected='selected' value='radio'>Radio</option></select></tr>
<tr><td></td><td><input type='text' id='RadioValue0' name='RadioValue10' size='50' value ='Bad'></td></tr>
<tr><td></td><td><input type='text' id='RadioValue1' name='RadioValue11' size='50' value ='Sufficiently'></td></tr>
<tr><td></td><td><input type='text' id='RadioValue2' name='RadioValue12' size='50' value ='Dobro'></td></tr>
<tr><td></td><td><input type='text' id='RadioValue3' name='RadioValue13' size='50' value ='Excelent'></td></tr>
<tr><td></td><td><input type ='submit' id='submit' name='submit' value='Izvedi spremembe'/></td></tr>
</table>
</form>
Still editSurvey.php page (I have add two because I can't put all text just in one textarea)
<form id='updateQuestion3' name='updateQuestion3' method='post' action=''>
<input type='hidden' name='hidden' value='question3'/>
<table width='600px'><tr></tr>
<tr><td width='50%' valign='top'><label for='question3'><h2>CURRENT QUESTION:</h2></label></td></tr>
<tr><td>SLOVENIAN </td><td width='50%' valign='top'> <label for='question3'>Some question 3</label></td></tr>
<tr><td width='50%' valign='top'>TYPE QUESTION</td><td width='50%' valign='top'>RADIO BUTTON</td></tr>
<tr><td><h4>DELETE QUESTION</h4></td><td><input type='checkbox' id='deleteQuestion' name='deleteQuestion' value='delete'/></td></tr>
<tr><td><h4>CHANGE QUESTIOn</h4></td></tr>
<tr><td><label>SLOVENIAN</label></td><td><input type='text' id='SloQuestion' name='SloQuestion' size='50' value ='Some question 3'></td></tr>
<tr><td>TYPE QUESTION </td><td><select id='typeQuestion' name='typeQuestion'><option value='text'>Textfield</option>
TextareaRadio
And my handlesurvey.php file:
if(isset($_POST["hiddenUpdateQuestion"])){
$username = 'root';
$password='';
$hostname='localhost';
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Ni se mogoče povezati na MySQL");
$selcetedDB = mysql_select_db("amon_survey",$dbhandle);
mysql_query("set names 'utf8'");
// HERE I WILL ENTER LATER SOME QUERY
mysql_close($dbhandle);
}
In your handlesurvey.php:
if(isset($_POST["hidden"]))
{
$username = 'root';
$password='';
$hostname='localhost';
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Ni se mogoče povezati na MySQL");
$selcetedDB = mysql_select_db("amon_survey",$dbhandle);
mysql_query("set names 'utf8'");
if($_POST["hidden"]=='question1'){
//Here will be question1's query;
}
if($_POST["hidden"]=='question2'){
//Here will be question2's query;
}
}
I think it's self-explanatory, comment if something's unclear.
Edited:
Am sorry, the $_POST was getting the right values, so I assumed the jQuery did too. Please see the updated code below:
In makesurvey.php:
<script type="text/javascript">
$(document).ready(function() {
$('form').submit(function(e){
e.preventDefault();
form_id = (this.id);
var SloQuestion =$("#"+form_id+" #SloQuestion").val(); // it's basically saying to fetch the input value of the submitted form
var typeQuestion=$("#"+form_id+" #typeQuestion").val();
var RadioValue1 =$("#"+form_id+" #RadioValue1").val();
var RadioValue2 =$("#"+form_id+" #RadioValue2").val();
var RadioValue3 =$("#"+form_id+" #RadioValue3").val();
var RadioValue4 =$("#"+form_id+" #RadioValue4").val();
alert(SloQuestion);
//If custom validation:
//if(form_id='updateQuestion1')
//{//validations for #updateQuestion1}
//if(form_id='updateQuestion2')
//{//validations for #updateQuestion2}
$.post( "handlesurvey.php", $("form").serialize())
.done(function( data ) {
alert("You have changed question!");
}); });
});
</script>
Edited (2nd time):
I'm guessing as per your comment below, you need to know whether the delete question checkbox is checked or not.
You can get to know through:
var delVal = $("#"+form_id+" #deleteQuestion ").is(':checked');
alert(delVal); //returns true if checked, false if not checked
A slight correction, please change your $.post to this so that it sends only the submitted form:
$.post( "handlesurvey.php", $("#"+form_id).serialize())
.done(function( data ) {
alert("You have changed question!");
});
And on your php:
if($_POST["hidden"]=='question1'){
if(isset($_POST["deleteQuestion"]) && ($_POST["deleteQuestion"]=="delete"))
{
//Your delete query for question1
}
}
if($_POST["hidden"]=='question2'){
if(isset($_POST["deleteQuestion"]) && ($_POST["deleteQuestion"]=="delete"))
{
//Your delete query for question2
}
}
You can use some common classes let say "form" for form tag and "input" for each input, textarea or radio. Then loop on each input element validate and send data.
This should be pretty generic:
$(".form").submit(function(){
var data = [];
var valid = true;
// get data & validate
$(this).find('.input').each(function () {
if ($(this).val().length)
data[] = $(this).val();
else
valid = false;
});
if (!valid)
// set some errors
return false;
// send form data ....
});
I am not going to give you the full answer but will guide you.
Step 1: Add a class to the form:
<form id='updateQuestion1' class="sq_form" name='updateQuestion1' method='post' action=''>
</form>
<form id='updateQuestion2' class="sq_form" name='updateQuestion1' method='post' action=''>
</form>
Step 2: bind onsubmit event to the form using the class selector
$(".sq_form").submit(function(event) {
var indata = $(this).serialize(); //the form data
//do whatever validations you need to do by using using this keyword
//Example: $(this).find('input')
});
Related
Thanks in advance.I have a popup window which has a dynamic text box fields.These textboxes will multiple according to the selected combo box values from the first form.The dynamic textboxes are displayed from jquery. Please anyone help me how to validate a dynamic text boxes on clicking the submit button. Actually I have to validate the textboxes before sending the mail. I have written a code which will validate only static textboxes. My code as below
<head>
<script>
$(document).ready(function () {
$(".myformid").click(function(){
var nameVal = $('.names').val();
var emailVal = $('.emails').val();
var phoneVal = $('.phones').val();
if(nameVal == "")
{
$('#errmsg').html("<p style='color:red;font-weight:bold'>Please enter the Name</p>");
}
else if(emailVal == ""){
//alert("A textbox is required");
$('#errmsg').html("<p style='color:red;font-weight:bold'>Please enter the email Id</p>");
}
else if(!ValidateEmail(emailVal))
{
$('#errmsg').html("<p style='color:red;font-weight:bold'>Invalid Email Id</p>");
}
else if(phoneVal == "")
{
$('#errmsg').html("<p style='color:red;font-weight:bold'>Please enter the Phone Number</p>");
}
else if(isNaN(phoneVal))
{
$('#errmsg').html("<p style='color:red;font-weight:bold'>Please enter the Valid Phone Number</p>");
}
else if(emailVal !="" && phoneVal != "")
{
$('#errmsg').text(" ");
var username = $('#usernameId').val();
var length = $('#lengthId').val();
var nameArray = [];
var emailArray = [];
var phoneArray = [];
$('.names').each(function(){
nameArray.push(this.value);
});
var nameboxVal = nameArray.join(",");
//alert(nameboxVal);
$('.emails').each(function(){
emailArray.push(this.value);
});
var emailboxVal = emailArray.join(",");
//alert(emailboxVal);
$('.phones').each(function(){
phoneArray.push(this.value);
});
var phoneboxVal = phoneArray.join(",");
//alert(phoneboxVal);
$.ajax({
type: "POST",
url: "/invl_exams/popSubmit",
data: {user:username,name:nameboxVal,email:emailboxVal,phone:phoneboxVal,lengths:length},
success: function(result){
console.log(result);
$('#mailSuccess').text('Mail Send Successfully');
$('#mailSuccess').fadeOut(5000);
}
});
}
});
});
// Passing dynamic textboxes inside the dialog box
$(".create-user").change(function(){
var selVal = $(this).val();
$('#lengthId').val(selVal);
$("#textboxDiv").html('');
if(selVal > 0) {
for(var i = 1; i<= selVal; i++) {
var sno = i;
$("#textboxDiv").append('<tr><td>'+sno+'. </td><td>Name:<input type="text" name="names" class="names" value="" required="required" /></td><td> </td><td>Email:<input type="email" name="emails" class="emails" value="" required="required" /></td><td> </td><td>Phone:<input type="text" name="phones" class="phones" value="" required="required" minlength="10" maxlength="16"/><br/></td></tr>');
}
}
});
});
</script>
</head>
<body>
<div id="dialog" title="Enter details to send Mail">
<!--<form id="myformid" method="post" action="<?php //echo $this->webroot?>users/sendmail">-->
<div id="mailSuccess" style="color:#019002;font-weight:bold"></div>
<form id="myformid" method="post">
<table id="examtable">
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<div id="textboxDiv"></div>
<input type="hidden" name="username" id="usernameId" value="<?php echo $this->Session->read('Auth.User.username'); ?>">
<input type="hidden" name="length" id="lengthId" value="">
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td>
<!--<input type="submit" name="btnSubmit" value="Submit">-->
<input type="button" name="btnSubmit" value="Send Mail" id="popSubmit">
</td>
</tr>
</table>
</form>
</div>
</div>
</body>
I don't think any validation is happening at all, whether the elements are static or dynamic.
$(".myformid").click(function(){
will not bind to anything because there are no elements with the class "myformid". The "." at the start of a selector indicates a class.
However you do have an element with an id "myformid". If you change your selector from . to # to indicate an id, then it will bind the event to the form. However, "click" is not the correct event to bind to a <form> element. You want to handle the form's "submit" event:
$("#myformid").submit(function(event){
Lastly, as it stands, your form will do a regular (non-ajax) postback as well as running your function, because the default behaviour of the submit event is not suppressed. Add this line as the first line of the above function:
event.preventDefault();
This will stop a regular postback from happening and allow your validation function to execute. At that point you should have a working solution, assuming the logic in your validation code is what you want.
If your validations are right you just need to attach event in way that dinamicly created elements will be supported too (jQuery on)
$( selector ).live( events, data, handler ); // jQuery 1.3+
$( document ).delegate( selector, events, data, handler ); // jQuery 1.4.3+
$( document ).on( events, selector, data, handler ); // jQuery 1.7+
for example
from
$(".myformid").click(function(){/*Some action*/});
to
$("body").on('click', ".myformid", function(){/*Some action*/});
from
$(".create-user").change(function(){/*Some action*/});
to
$("body").on('change', ".create-user", function(){/*Some action*/});
Small advice: Try to avoid using $("body") selector you can see what is your good dom element witch is parent to your dynamically generated contend/elements.
I have a HTML inputting page that can disable a input field by checking a check box named undefined.
When I check undefined and go to next page and come back to the inputting page have to checked the check box and disable the input field.
Now when I come back to input page the checkbox is checked but input field is not disabled.
Below is my javascript function code and the inputting page code segment. Please help me on this.
javascript function:
function disable_input_field(value){
var input_field = document.getElementById("person_" + value);
if ($("#undefined_" + value).is(":checked")) {
input_field.disabled = true;
input_field.style.backgroundColor = "#e6e6e6";
document.getElementById(value).value = 'undifined';
}else{
input_field.disabled = false;
input_field.style.backgroundColor = "#ffffff";
document.getElementById(value).value = '';
}
}
Inputting page code segment :
<tr>
<td nowrap class="auto-style34" colspan="3" style="height: 40px;">data</td>
<td nowrap class="auto-style39" colspan="3">
<input type="text" style="background-color: white;color: black;border-style:solid;height: 25;ime-mode: active;" id="person_data" name="person_data" size="50" value='<?php echo person_data ?>'>
</td>
<td nowrap class="auto-style39" colspan = "3">
<?php if($check_data != ''){ print_r("if");?>
<input type="checkbox" checked name="undefined_data" id="undefined_data" onclick="disable_input_field('data')" >undefined
<?php }else{ print_r("else");?>
<input type="checkbox" name="undefined_data" id="undefined_data" onclick="disable_input_field('data')" >undefined
<?php }?>
<input type="hidden" name="data" id="data">
</td>
When coming back to the inputting page $check_data becomes not null.
$(document).ready(function(){
if($("#undefined_" + value).is(":checked")){
input_field.disabled = true;
input_field.style.backgroundColor = "#e6e6e6";
document.getElementById(value).value = 'undifined';
}
});
I'm using codeigniter and i have some issue with radio button, javascript, and php.
The radio button is used for selecting type of customer. This is my radio button on view file
<input type="radio" name="customerType" value="Reseller" /> Reseller
<input type="radio" name="customerType" value="Dropshipper" /> Dropshipper
<input type="text" class="form-control" value ="" name="customerCode" aria-describedby="basic-addon1" />
When Reseller radio button selected, I want the value of customerCode will showing codename like "RSL", and when Dropshipper selected, the value of customerCode showing "DRP" automatically.
Please advice,
Thank you very much :)
============================AFTER SOLVED============================
Hi guys, after this case was solved i want to share my code.
as we know, this thread will auto generating value of form input depends by radio button. And we got it! Thanks to Ashwani Goyal :)
And now, I want to give a numerical character on this form input. So this is my model code :
function getCustomerCodeRSL(){ //this function will generating when "RSL" radio button selected
$q = $this->db->query("select MAX(RIGHT(customer_code,3)) as codeMax from customer where customer_code like 'RSL%'");
$code = "";
if($q->num_rows()>0){
foreach($q->result() as $k){
$tmp = ((int)$k->codeMax)+1;
$code = sprintf("%03s", $tmp);
}
}else{
$code = "001";
}
return $code;
}
function getCustomerCodeDRP(){ //this function will generating when "DRP" radio button selected
$q = $this->db->query("select MAX(RIGHT(customer_code,3)) as codeMax from customer where customer_code like 'DRP%'");
$code = "";
if($q->num_rows()>0){
foreach($q->result() as $k){
$tmp = ((int)$k->codeMax)+1;
$code = sprintf("%03s", $tmp);
}
}else{
$code = "001";
}
return $code;
}
And this is my controller :
function cCustomer(){
$data = array(
'data_customer' => $this->Model_App->getAllData('customer'),
'total_customer' => $this->Model_App->counterAllRowTable('customer'),
'cust_codeRSL' => $this->Model_App->getCustomerCodeRSL(),
'cust_codeDRP' => $this->Model_App->getCustomerCodeDRP(),
);
$this->load->view('admin/header');
$this->load->view('admin/vCustomer', $data);
$this->load->view('admin/footer');
}
So, the output will be like this :
if DRP selected then the output is; DRP001, DRP002, ... and more
if RSL selected then the output is; RSL001, RSL002, ... and more.
hope it will helping someone else out there who need the same thing with me :)
Try the following:
$("input[name='customerType']").click(function(){
$("input[name='customerCode']").val( $(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="radio" name="customerType" value="RSL" id="rsl" /><label for="rsl">Reseller</label>
<input type="radio" name="customerType" value="DSL" id="dsl"/> <label for="dsl">Dropshipper</label>
<input type="text" class="form-control" value ="" name="customerCode" aria-describedby="basic-addon1" />
i think thats it you want fiddle
<input type="radio" name="customerType" value="RSL" /> Reseller
<input type="radio" name="customerType" value="DSL" /> Dropshipper
<input type="text" class="form-control" value ="" name="customerCode" aria-describedby="basic-addon1" /
$("input[name='customerType']").click(function(){
$("input[name='customerCode']").val( $(this).val());
});
if($_POST['customerType'] == "Reseller"){
$_POST['customerCode'] = "RSL";
}elseif($_POST['customerType'] == "Dropshipper"){
$_POST['customerCode'] = "DRP";
}
Easiest way with only php
Hi I have a form that has a button used to prefill my form with data from my database. Using Json It works fine to populate text inputs but how do I get it to select a radio button based on the value returned from my database?
FORM
<form action="#">
<select id="dropdown-select" name="dropdown-select">
<option value="">-- Select One --</option>
</select>
<button id="submit-id">Prefill Form</button>
<input id="txt1" name="txt1" type="text">
<input id="txt2" name="txt2" type="text">
<input type="radio" id="q1" name="q1" value="4.99" />
<input type="radio" id="q1" name="q1" value="7.99" />
<button id="submit-form" name="Submit-form" type="submit">Submit</button>
</form>
SCRIPT
<script>
$(function(){
$('#submit-id').on('click', function(e){ // Things to do when
.......
.done(function(data) {
data = JSON.parse(data);
$('#txt1').val(data.txt1);
$('#txt2').val(data.txt2);
$('#q1').val(data.q1);
});
});
});
</script>
/tst/orders2.php
<?php
// Create the connection to the database
$con=mysqli_connect("xxx","xxx","xxx","xxx");
........
while ($row = mysqli_fetch_assoc($result))
{
echo json_encode($row);
die(); // assuming there is just one row
}
}
?>
Don't use ID because you have same ID of both radio buttons
done(function(data) {
data = JSON.parse(data);
$('#txt1').val(data.txt1);
$('#txt2').val(data.txt2);
// Don't use ID because the name of id is same
// $('#q1').val(data.q1);
var $radios = $('input:radio[name=q1]');
if($radios.is(':checked') === false) {
$radios.filter('[value='+data.q1+']').prop('checked', true);
}
});
You currently have both radio buttons using the same ID. ID's should be unique.
You can use the [name] attribute to do this, or you can set a class on the element. Here is an example:
$('input[name=q1][value="'+ data.q1 +'"]').prop('checked', true);
You can do it based on the value of said input:
instead of
$('#q1').val(data.q1);
Try
$('input:radio[value="' + data.q1 + '"]').click();
On a side note, you have both radios with the same ID, the results of an id based selector are going to vary from browser to browser because an id should be UNIQUE
you can refer the following link to solve your problem. works fine for me
JQuery - how to select dropdown item based on value
I am trying to incorporate a popup into my project. It works up until I try and pass a value from javascript to the php script. I have included a link to the example I used and link to the original script. There were slight changes made to the original script. The area I am having an issue with is marked with a ---->
full script can be found here:
http://www.webdeveloper.com/forum/showthread.php?279111-Contact-form-popup-window
working example can be found here:
http://ilolo.ru/wd/contact_form/
I have the following in hello.html
<form id='form1' style='width: 1100px'>
<table>
<th>Email</th>
<th>Options</th>
<tr>
<td>
<input type='text' id='email1' size='25' value='bob#mail.com'>
</td>
<td><a id='1' href='#null' class='contactus'><img src= 'images/emailbutton.jpg' title='Email' border='0' height='24' width='24'></img></a>
</tr>
</table>
</form>
The following js is in a .js that I reference in my html.
I use the following since I have more than one form. Just showed one form in my example.
The alert below works....shows the correct email address when I click on the image link in the html.
function findCenter(obj) {
var deltaX = parseInt(obj.css('padding-left')),
deltaY = parseInt(obj.css('padding-top'));
obj.css('left', $(window).width() / 2 - obj.width() / 2 - deltaX + 'px').css('top', $(window).height() / 2 - obj.height() / 2 - deltaY + 'px');
}
$(document).ready(function () {
$('.contactus').click(function () {
var element = $(this);
var J = element.attr('id');
var email = document.getElementById('email'+J).value;
alert(email);
$('#result').html('<h3>Loading</h3>').css('display', 'block');
findCenter($('#result'));
$.get('email.php', function (data) {
$('#result').html(data);
findCenter($('#result'));
$('#snd').click(function () {
var subject = document.getElementById('subject').value;
var addy = document.getElementById('addy').value;
var comments = document.getElementById('comments').value;
$('#result').append('<br /><br /><div><i>Sending...</i></div>');
$.post('lbi_email.php',{mode: 'snd', subject: subject, addy: addy, comments: comments},function(data){
$('#result').html(data);
findCenter($('#result'));
});
});
});
});
});
email.php
This value below is not getting sent by the javascript and the input value=$email is not written correctly (I think)
<?php
include( 'connection.php');
function showForm(){
$email=$_POST[ 'email'];
echo '<form name="mf" id="mf" action="" method="post">
<h2>Send Email To Customer</h2><br />
<p><label>Address :</label><input type="text" size="35" name="addy" id="addy" value="'.$email.'"/></p>
<p><label>Subject : </label><input type="text" size="35" name="subject" id="subject" value=""/></p>
<label>Message:</label><textarea style="width: 100%;" cols="20" rows="10" id="comments" name="comments"></textarea>
<p><img src="submitbutton.jpg" title="Submit Email" border="0" height="25" width="75"></img></p></form>';
}
function sndForm(){
/* here goes checking data and so on... then sending if everything 's good and sending done show message to the user*/
echo '<h3>Everything\'s cool.
<br />
<br />Viva Cuba!</h3>
<script type="text/javascript">
setTimeout(\'$("#result").fadeOut("fast")\',3000);
</script>';
}
/*---------------------------------------------------------------------*/
$mode=(!empty($_GET['mode']))?$_GET['mode']:$_POST['mode'];
switch($mode)
{
case 'snd':sndForm();break;
default: showForm();break;
}
?>
In your javascript you are sending literal email instead of your email value -
$.post('email.php',{mode: 'snd', email: 'email'},function(data){
Change this to -
$.post('email.php',{mode: 'snd', email: email},function(data){
In your php code you have a variable scope issue - $email is outside the scope of your function
$email=$_POST[ 'email'];
function showForm(){
echo $email;
Try setting it inside
function showForm(){
$email=$_POST[ 'email'];
echo $email;
Finally, you have $email inside single quotes-
echo '<form name="mf" id="mf" action="" method="post">
<h2>Send Email To Customer</h2><br />
<input type='text ' value='$email '/>
...
Need to update to
echo '<form name="mf" id="mf" action="" method="post">
<h2>Send Email To Customer</h2><br />
<input type="text" value="'.$email.'"/>
...
Additionally, it looks like when you are submitting the for you are not sending the comments textarea, so you probably need to add that as well.
$('#snd').click(function () {
$('#result').append('<br /><br /><div><i>Sending...</i></div>');
var comments = document.getElementById('comments').value;
$.post('email.php',{mode: 'snd', email: email, comments: comments},function(data){
$('#result').html(data);
findCenter($('#result'));
});
});
Edit
You need to add the email value to your $.get() -
$.get('email.php', email: email, function (data) {
and then change it to $_GET['email'] in your php -
function showForm(){
$email=$_GET[ 'email'];