In spite of the fact that I'll probably look like a complete idiot, I'm going to post my entire files to see what I'm doing wrong.
I have form built into the page like so:
<form method="post" class="form-horizontal" id="final_form" action="send_mail.php">
<h2>Search for your dream home<br />
and save now!</h2>
<legend>Which Areas are you interested in?</legend>
<div class="areas row-fluid" style="text-align:left !important;">
<div class='span5' style='margin-left:0px !important;'>
<label>
<input type="checkbox" name="arrayValue[]" id="area[0]" value="Sparks" style='margin-top:-5px !important;'> Sparks</label>
</div>
<div class='span5' style='margin-left:0px !important;'>
<label >
<input type="checkbox" name="arrayValue[]" id="area[1]" value="Stead" style='margin-top:-5px !important;'> Stead</label>
</div>
<div class='span5' style='margin-left:0px !important;'>
<label>
<input type="checkbox" name="arrayValue[]" id="area[2]" value="North Reno" style='margin-top:-5px !important;'> North Reno</label>
</div>
<div class='span5' style='margin-left:0px !important;'>
<label >
<input type="checkbox" name="arrayValue[]" id="area[3]" value="South Reno" style='margin-top:-5px !important;'> South Reno</label>
</div>
<div class='span5' style='margin-left:0px !important;'>
<label >
<input type="checkbox" name="arrayValue[]" id="area[4]" value="Double Diamond" style='margin-top:-5px !important;'> Double Diamond</label>
</div>
</div>
<input type="button" onclick="jQuery('#myModal').modal('show')" value="CONTINUE" />
</div>
</div>
</div>
<!--banner area end-->
<!--content area 1 start-->
<div id="content1">
<div class="content1_in"> <span>
<h2 style="line-height:40px;font-size:40px;padding-bottom:10px">Some Content</h2>
<p style="line-height:30px;font-size:22px;text-align: left">Some Content!</p>
</span>
<div class="img">
<img src="image" alt="" />
</div>
</div>
</div>
<!--content area 1 end-->
<!--content area 1 start-->
<div id="content2">
<div class="content2_in"> <span>
</span>
<div class="img">
<img src="http://f14.co/realtor/assets/images/ipad-img.png" alt="" />
<div class="key"></div>
</div>
</div>
</div>
<!--content area 1 end-->
<!--content area 3 start-->
<div id="content3">
<div class="content3_in">
<div class="img">
<img src="image" alt=""
/>
</div>
<div class="free"></div>
</div>
</div>
<!--content area 3 end-->
<div id="footer">
<div class="footer_in">
This Website Is Brought To You By: Ken Holden</div>
</div>
<!-- Modal -->
<div id="myModal" class="modal hide fade modal-survey" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">x</button>
<h3 id="myModalLabel" class="survey_title">What type of home are you looking for?</h3>
</div>
<input type="hidden" name="template" id="template" value="Buyers" />
<div class="modal-body">
<div id="lead_info_1">
<div class="input select">
<div class=""></div>
<div class="span-4 step-2-dropdown append-bottom control-group">
<select class="span3 required" name="minPrice" id="minPrice">
<option value="">Price</option>
<option value='$50,000 - $150000'>$50,000 - $150000</option><option value='$150,000 -
$300,000'>$150,000 - $300,000</option><option value='$300,000 - $400,000'>
$300,000 - $400,000</option><option value='$400,000 +'>$400,000 +</option>
</select>
</div>
</div>
<div class="input select">
<div class=""></div>
<div class="span-4 step-2-dropdown append-bottom control-group">
<select class="span3 required" name="minBedrooms" id="minBedrooms">
<option value="">Bedrooms</option>
<option value='1-2'>1-2</option><option value='2-4'>2-4</option><option value='4+'>4+
</option> </select>
</div>
</div>
<div class="input select">
<div class=""></div>
<div class="span-4 step-2-dropdown append-bottom control-group">
<select class="span3 required" name="minBathrooms" id="minBathrooms">
<option value="">Bathrooms</option>
<option value='1-2'>1-2</option><option value='2-4'>2-4</option><option value='4+'>4+
</option> </select>
</div>
</div>
<div class="input select">
<div class=""></div>
<div class="span-8 step-2-dropdown2 append-bottom control-group">
<select class="span3 required" name="realtor" id="realtor">
<option value="">Currently Working With a Realtor</option>
<option value="Yes">Yes</option>
<option value="No">No</option>
</select>
</div>
</div>
<div class="input select">
<div class=""></div>
<div class="span-8 step-2-dropdown2 append-bottom control-group">
<select class="span3 required" name="move" id="move">
<option value="">When Do You Plan On Moving?</option>
<option value="30-90 days">30-90 days</option>
<option value="90-120 days">90-120 days</option>
<option value="I'm just looking">I'm just looking</option>
</select>
</div>
</div>
<!--<img src="http://localcommissionsystem.com/assets/app/images/SearchDealsLikeThis.png" />-->
</div>
<div id="lead_info_2" style="display:none">
<center>
</center>
</div>
<div id="lead_info_3" style="display:none">
<textarea class="" id="wantedArea" name="wantedArea" style="display:none;"></textarea>
<div class="control-group">
<label class="control-label" for="fname">First Name</label>
<div class="controls">
<input class="required" type="text" id="fname" name="fname" placeholder="First Name" />
</div>
</div>
<div class="control-group">
<label class="control-label" for="email">Email</label>
<div class="controls">
<input class="required" type="text" id="email" name="email" placeholder="Valid Email Required" />
</div>
</div>
<div class="control-group">
<label class="control-label" for="phone">Cell Phone</label>
<div class="controls">
<input class="required" type="text" id="phone" name="phone" placeholder="Cell Phone" />
</div>
</div>
</div>
</div>
Here is the php
<?php
$webmaster_email = "email#email.com";
$webmaster_email2 = "email#email.com";
$fname = $_REQUEST['fname'];
$email = $_REQUEST['email'];
$phone = $_REQUEST['phone'];
$minBedrooms = $_REQUEST['minBedrooms'];
$minBathrooms = $_REQUEST['minBathrooms'];
$minPrice = $_REQUEST['minPrice'];
$move = $_REQUEST['move'];
$realtor = $_REQUEST['realtor'];
if( !empty($_REQUEST) && is_array($arrayValue) ) {
$arrayValue = array();
$areas = implode("," , $_REQUEST['arrayValue']);
}
$emailfrom = $_REQUEST['email'];
$headers = 'From: '.$emailfrom."\r\n".
$email_to = "test#gmail.com, ";
/* This bit sets the URLs of the supporting pages.
If you change the names of any of the pages, you will need to change the values here.
*/
$feedback_page = "index.html";
$thankyou_page = "thankyou.html";
/*
The following function checks for email injection.
Specifically, it checks for carriage returns - typically used by spammers to inject a CC
list.
*/
function isInjected($str) {
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str)) {
return true;
}
else {
return false;
}
}
// If the user tries to access this script directly, redirect them to the feedback
form,
if (!isset($_REQUEST['email'])) {
header( "Location: $feedback_page" );
}
// If email injection is detected, redirect to the error page.
elseif ( isInjected($email) ) {
header( "Location: $error_page" );
}
// If we passed all previous tests, send the email then redirect to the thank you page.
else {
mail( $email_to, "You Have a New Lead",
"Name: $fname\nEmail: $email\nPhone: $phone\nMinBedrooms: $minBedrooms\nMinPrice:
$minPrice\nMove Date: $move\nRealtor: $realtor\nAreas: $areas", $headers );
}
echo "<script>window.location = 'thankyou.html'</script>";
?>
My checkboxes are not returning any values. Does anyone know why? I've worked to make things correct and I think I've got the array set up right thanks to the help of another question, but I think I'm asking the wrong question in my other post.
Your $arrayValue is not defined before the first if statement which is why the is_array($arrayValue) does not succeed. Please try:
if( !empty($_REQUEST['arrayValue'])) {
$arrayValue = array();
$areas = implode("," , $_REQUEST['arrayValue']);
}
In your HTML this should be the name:
<input type="checkbox" name="arrayValue" id="area[4]" value="Double Diamond" style='margin-top:-5px !important;'> Double Diamond</label>
In your PHP
if( !empty($_REQUEST) && is_array($arrayValue) )
{
$arrayValue = array();
$arrayValue[] = $_REQUEST['arrayValue'];
}
$areas = implode(",",$arrayValue);
put it in the array first then implode it as a string.
just check by printing php global variable $_REQUEST on your php. You can do it by writing print_r($_REQUEST); if it does not shows anything or black array, make use of $_POST instead of $_REQUEST at each place in php file. Hope it will solve your problem.
Related
I am doing a contact us form into my website, and i am now stuck.
Here's the PHP code:
#Receive user input
$Name = $_POST['Name'];
$Email = $_POST['Email'];
$Message = $_POST['Message'];
$palvelu = $_POST['palvelu'];
#Filter user input
function filter_email_header($form_field) {
return preg_replace('/[nr|!/<>^$%*&]+/','',$form_field);
}
$Email = filter_email_header($Email);
#Send email
$headers = "From: $Email\r\n";
$sent = mail('user#example.com', 'Yhteydenotto Pyyntö: ', $Message, $headers);
#Thank user or notify them of a problem
if ($sent) {
?><html>
<head>
<title>Kiitos yhteydenotosta!</title>
</head>
<body>
<h1>Kiitos yhteydenotosta!</h1>
<p>Olemme sinuun yhteydessä mahdollisimman nopeasti!</p>
</body>
</html>
<?php
} else {
?><html>
<head>
<title>Jokin meni vikaan!</title>
</head>
<body>
<h1>Jokin meni vikaan!</h1>
<p>Emme pystyneet lähettämään viestiä?</p>
</body>
</html>
<?php
}
?>
And here's the html code of the form:
<div class="col-lg-5 d-flex align-items-stretch">
<div class="bg-white">
<div class="w-100 heading-title bg-primary text-center">
<h2 class="mb-0">Ota meihin yhteyttä!</h2>
</div>
<form action="email.php" method="post" class="appointment bg-white p-4 p-md-5">
<div class="row">
<div class="col-md-12">
<div class="form-group">
<div class="form-field">
<div class="select-wrap">
<div class="icon"><span class="fa fa-chevron-down"></span></div>
<select name="palvelu" id="" class="form-control">
<option value="">Valitse palvelu</option>
<option value="">Sammaleen poisto</option>
<option value="">Katon pinnoitus</option>
<option value="">Tiilikaton huolto</option>
<option value="">Vuosihuolto</option>
</select>
</div>
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<input type="text" id="Name" name="Name" placeholder="Nimi" class="form-control">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<input type="Email" id="Email" name="Email" placeholder="Sähköposti" class="form-control">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<div class="input-wrap">
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<div class="input-wrap">
</div>
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<textarea id="Message" name="Message" class="form-control" placeholder="Viesti" rows="6" maxlength="3000"></textarea>
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<input type="submit" value="Lähetä viesti" class="btn btn-primary py-3 px-4">
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
So that's in finnish but i have this list where you can select what job you want to discuss "palvelut" and then the basic form. What it should do is to send an email with "who sent it" "the job they want" and the basic form stuff "name" "email" and the "message". But now it only seems to send an blank email with only the "yhteydenotto pyyntö" it means like "a new question". Could someone help me? What am i doing wrong...
Solution Here!!!
* Your filter_email_header is contain Problem so we use Default FILTER_SANITIZE_EMAIL filter.
Remove code
#Filter user input
function filter_email_header($form_field) {
return preg_replace('/[nr|!/<>^$%*&]+/','',$form_field);
}
$Email = filter_email_header($Email);
Replace the Fix
// Remove all illegal characters from email
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
*Your Form select option not present value so whatare the value select empty value to shown email form
<select name="palvelu" id="palvelu" class="form-control">
<option value="Valitse palvelu">Valitse palvelu</option>
<option value="Sammaleen poisto">Sammaleen poisto</option>
<option value="Katon pinnoitus">Katon pinnoitus</option>
<option value="Tiilikaton huolto">Tiilikaton huolto</option>
<option value="Vuosihuolto">Vuosihuolto</option>
</select>
In codeigniter I want to add the product on admin Id which is required in DB Table. when Admin log in his ID must be catch. whenever and where Id is needed it must be available.
here is my VIEW in which
<?php echo form_open_multipart('admin/add_product');?>
<!------------------Product Name------------------------>
<div class="row form-group">
<div class="col-sm-10">
<label>Product Name: *</label>
<?php echo form_input(['name'=>'name','class'=>'form-
control','placeholder'=>'Product name..','value'=>
set_value('name')])?>
<div>
<?php echo form_error('name');?>
</div>
</div></div>
<!------------------Product Cat---------------------------->
<div class="row form-group">
<div class="col-sm-12">
<label>Category: *</label> <br>
<select class="selectpicker" name="pro_cat_id">
<option label="Select Category">Category</option>
<?php
foreach($product_cat as $cat)
{?>
<option value="<?= $cat['pro_cat_id'] ?>"><?= $cat['category'] ?>
</option>
<?php }?>
</select>
<div>
<!------------------Product Price---------------------------->
<div class="row form-group">
<div class="col-md-10">
<label>Product Price: *</label>
<?php echo form_input(['name'=>'price','class'=>'form-
control','placeholder'=>'Product Price..','value'=>
set_value('price')])?>
<div>
<?php echo form_error('price');?>
</div>
</div></div>
<!------------------Product Location---------------------------->
<div class="row form-group">
<div class="col-md-10">
<label>Product Location: *</label>
<?php //echo form_input(['name'=>'location','class'=>'form-
control','placeholder'=>'Product Location'])?>
</div></div>
</div></div>
<!-----------End of body---------------->
</div>
<div class="modal-footer">
<input name="" type="submit" value="ADD" class="btn btn-success">
</div>
<?php echo form_close();?>
</div>
</div>
</div>
Here is my Controller
function add_product(){
$data['product_size'] = $this->show->show_product_size();
$data['product_cat']= $this->show->show_product_category();
$data['product_color'] = $this->show->show_product_color();
$data['pr']= $this->show->show_provider();
$post= $this->input->post();
$ =this->form_validation->set_error_delimiters("<p class='text-danger'>","
</p>");
if($this->form_validation->run('add_product')){
if($this->add->add_product($post)==TRUE){
$this->db->close();
$this->session->set_flashdata('success','Product has been added
successfully');
return redirect('admins/pages/add_product_page');
//$this->load->admin_template('admin/add_product',$data);
}
}
else
{
$this->session->set_flashdata('failled','Sorry, fail to add product');
//return redirect('admins/pages/add_product_page');
$upload_error= $this->upload->display_errors();
$this->load->admin_template('admin/add_product',$data);
}
}
Use this query, preferably in your controller:
$this->session->userdata('id');
i have form in which there are 2 dropdowns which are select batch from first dropdown and according to that batch, students will appear in second dropdown , Right now i am getting dropdown of batch but, not getting dropdown of students using batch
This is my view
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use common\models\TblBetch;
use common\models\TblCompany;
use common\models\TblCourse;
use common\models\TblStudent;
use common\models\TblStudentExpence;
use kartik\widgets\DepDrop;
use yii\db\Query;?><link rel="shortcut icon" href="<?php echo Yii::$app->params['global_theme_path'];?>images/logo1.png" type="image/png"><script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><script src="http://iamrohit.in/lab/js/location.js"></script><div class="pageheader"><h2><i class="fa fa-money"></i>Expense</h2>
<div class="breadcrumb-wrapper">
<span class="label">You are here:</span>
<ol class="breadcrumb">
<li>Westline Shipping</li>
<li class="active">Add Expense</li>
</ol></div></div><div class="contentpanel">
<?php
$session = Yii::$app->session;
if($session->hasFlash('success'))
{
echo $session->getFlash('success');
}
if($session->hasFlash('error'))
{
echo $session->getFlash('error');
}
$site = '';
?>
<div class="row">
<div class="col-md-12">
<div class="container-fluid-50">
<div class="col-md-6">
<div class="panel panel-default">
<div class="panel-heading">
<div class="panel-btns">
−
</div>
<h4 class="panel-title">Add Expense</h4>
</div>
<form class="panel-body" action="dashboard" method="post">
<div class="row">
<?php $form = ActiveForm::begin(); ?>
<div class="col-sm-9">
<div class="form-group">
<label class="control-label"></label>
<?php
//$batch = new TblBetch();
//$quer = new Query();
//$getid = [''];
//$getid = $quer->select('b_sn')->from($batch->tableName())->all();
//echo $form->field($model,'se_b_id')->dropDownList($getid,['id'=>'se_b_id']);
?>
<?=
$form->field($model,'se_s_id')->dropDownList(
ArrayHelper::map(TblBetch::find()->all(),'b_id','b_sn'),
[
'prompt'=>'Select Batch',
'name'=>'s_b_id',
'id'=>'s_b_id',
'onchange'=>'$.post("student?id="+$(this).val(),function(data){$("select#tblstudentexpence-se_s_id").html(data);});'
]);
?>
</div>
</div><!-- col-sm-6 -->
<div class="col-sm-9">
<div class="form-group">
<label class="control-label"></label>
<?=
$form->field($model,'se_s_id')->dropDownList(
ArrayHelper::map(TblStudent::find()->all(),'s_id','s_fname'),
[
'prompt'=>'Select Student',
'name'=>'se_s_id',
'id'=>'se_s_id'
]);
?>
</div>
</div><!-- col-sm-6 -->
<div class="col-sm-9">
<div class="form-group">
<label class="control-label">Payment Mode</label>
<div class="radio"><label><input type="radio" name="num" value="cash" required> Cash </label></div>
<div class="radio"><label><input type="radio" name="num" value="check" required> Check </label></div>
<div class="radio"><label><input type="radio" name="num" value="dd" required> DD </label></div>
</div>
</div><!-- col-sm-6 -->
<div class="col-sm-9">
<div class="form-group">
<label class="control-label">Check / DD No</label>
<input type="text" name="num" id="num" class="form-control" placeholder="Enter your check or DD number" required/>
</div>
</div><!-- col-sm-6 -->
<div class="col-sm-9">
<div class="form-group">
<label class="control-label">Amount</label>
<input type="text" name="lastname" class="form-control" placeholder="Enter your Amount" required/>
</div>
</div><!-- col-sm-6 -->
<div class="col-sm-9">
<div class="form-group"> <br><br>
<input type="submit" value="Submit" class="btn btn-primary">
<input type="reset" value="Cancel" class="btn btn-primary">
</div></div>
</div><!-- row -->
</form>
</div><!-- panel-body -->
</div>
</div>
</div> <div class="col-md-12">
</div>
</div></div></div> <script>
$(function() {
window.invalidate_input = function() {
if ($('input[name=num]:checked').val() == "check" || $('input[name=num]:checked').val() == "dd")
$('#num').removeAttr('disabled');
else
$('#num').attr('disabled', 'disabled');
};
$("input[name=num]").change(invalidate_input);
invalidate_input();
});
This is my controller (sitecontroller.php)
public function actionStudent($id)
{
//p($id);
$student = new TblStudent();
$queryobj = new Query();
$data = [];
$data = $queryobj->select('*')->from($student->tableName())->where(['s_betch'=>$id])->count();
//p($data);
$queryobj1 = new Query();
$getid = [];
$getid = $queryobj->select('*')->from($student->tableName())->where(['s_betch'=>$id])->all();
//p($getid);
if($data > 0)
{
foreach($getid as $gid)
{
echo "<option value='".$gid->s_id."'>".$gid->s_fname."</option>";
}
}
else
{
echo "<option> - </option>";
}
}
I am getting this response in firebug after ajax call
You can implement dependent drop-down much simpler in yii 2 because there is a widget available which you can use.
use kartik widget here -> kartik dependent drop down
When the user picks either phone or email I want the form to then generate a box to enter the details of which they selected. How do I do this? Thanks.
<div class="field-container full clearfix">
<label class="required">What is your prefered method of contact? </label>
<select name="fields[contact]">
<option value="blank"></option>
<option value="phone">Phone</option>
<option value="email">E-mail</option>
</select>
</div>
<div class="field-container full clearfix">
<label class="required">Phone</label>
<input type="text" name="fields[phone]" value="<?= (isset($fields['phone']) ? $fields['phone'] : '' )?>"/>
</div>
<div class="field-container full clearfix">
<label class="required">E-mail Address</label>
<input type="text" name="fields[email]" value="<?= (isset($fields['email']) ? $fields['email'] : '' )?>"/>
</div>
Here's a snippet that shows you how to do that with javascript:
document.getElementById('selection').addEventListener('change',function(){
var inputs = document.querySelectorAll('.hideAndShowInput > input');
for (var i = 0; i<inputs.length;i++) {
inputs[i].setAttribute('type','hidden');
inputs[i].parentNode.style.display= 'none';
}
document.getElementById(this.value).parentNode.style.display='block';
document.getElementById(this.value).setAttribute('type',this.value);
});
<div class="field-container full clearfix">
<label class="required">What is your prefered method of contact? </label>
<select id="selection" name="fields[contact]">
<option value="blank"></option>
<option value="phone">Phone</option>
<option value="email">E-mail</option>
</select>
</div>
<div class="field-container full clearfix hideAndShowInput" style="display:none;">
<label class="required">Phone</label>
<input type="hidden" id="phone" name="fields[phone]" value="">
</div>
<div class="field-container full clearfix hideAndShowInput" style="display:none;">
<label class="required">E-mail Address</label>
<input type="hidden" id="email" name="fields[email]" value=""/>
</div>
<div class="field-container full clearfix">
<label class="required">otherField1</label>
<input type="text" id="other" name="fields[p]" value="otherField">
</div>
<div class="field-container full clearfix">
<label class="required">otherField2</label>
<input type="text" id="lsd" name="fields[em]" value="otherField2"/>
</div>
Please note that I added a Id to the select element and to the input elements.
EDIT | USAGE
Create a file, call it script.js or whatever you like and put this in it:
document.getElementById('selection').addEventListener('change',function(){
var inputs = document.querySelectorAll('.hideAndShowInput > input');
for (var i = 0; i<inputs.length;i++) {
inputs[i].setAttribute('type','hidden');
inputs[i].parentNode.style.display= 'none';
}
document.getElementById(this.value).parentNode.style.display='block';
document.getElementById(this.value).setAttribute('type',this.value);
});
Save the file to a folder on your server that is accesible. For example /public/js/script.js
In your html, add
<script src="/public/js/script.js"></script> <!-- or however your path is -->
to the very end of your file, directly before the closing </body> tag So that it looks like this
<div class="field-container full clearfix">
<label class="required">What is your prefered method of contact? </label>
<select id="selection" name="fields[contact]">
<option value="blank"></option>
<option value="phone">Phone</option>
<option value="email">E-mail</option>
</select>
</div>
<div class="field-container full clearfix hideAndShowInput" style="display:none;">
<label class="required">Phone</label>
<input type="hidden" id="phone" name="fields[phone]" value="">
</div>
<div class="field-container full clearfix hideAndShowInput" style="display:none;">
<label class="required">E-mail Address</label>
<input type="hidden" id="email" name="fields[email]" value=""/>
</div>
<div class="field-container full clearfix">
<label class="required">otherField1</label>
<input type="text" id="other" name="fields[p]" value="otherField">
</div>
<div class="field-container full clearfix">
<label class="required">otherField2</label>
<input type="text" id="lsd" name="fields[em]" value="otherField2"/>
</div>
<script src="/public/js/script.js"></script> <!-- here is the script included -->
</body>
That's it.
This worked for me:
Add an id to your select: select name="fields[contact]" id="contact"
Set phone and email fields to hidden by using a class or inline style: div class="field-container full clearfix hide-this" id="phone-container"
--or just set style: display:none;
Javascript:
$('#contact').change(function(){
if(this.selectedOptions[0].text == 'phone') {
$('#phone-container').show().focus();
}else{$('#phone-container').hide();}
});
This should do the trick... let me know?
//CODE
<!DOCTYPE html>
<html>
<head>
<style>
.field-hide{
display: none;
}
</style>
<script>
function valChange(ele){
var fields = document.getElementById("container").children;
for(var i=0; i < fields.length; i++){
fields[i].classList.add("field-hide");
}
switch(ele.value){
case "phone":
document.getElementById("phone").classList.remove("field-hide");
break;
case "email":
document.getElementById("email").classList.remove("field-hide");
break;
}
}
</script>
</head>
<body>
<div class="field-container full clearfix">
<label class="required">What is your preferred method of contact?</label>
<select type="text" name="fields[contact]" onchange="valChange(this)">
<option value="blank" selected disabled>Choose</option>
<option value="phone">Quoted</option>
<option value="email">Labour Only</option>
</select>
</div>
<div id="container">
<div class="field-container full clearfix field-hide" id="phone">
<label class="required">Phone</label>
<input type="text" name="fields[phone]" placeholder="enter phone number">
</div>
<div class="field-container full clearfix field-hide" id="email">
<label class="required">E-mail Address</label>
<input type="text" name="fields[email]" placeholder="enter email">
</div>
</div>
</body>
</html>
I am trying to create an array of objects which I can then display, the objects being created when a form has been submitted.
The first object gets successfully added, but when I add another object, it simply overwrites the last created object. Can anyone see where I am going wrong?
<?php require_once $_SERVER['DOCUMENT_ROOT'].'/includes/classes/Goal.php'; ?>
<?php require_once $_SERVER['DOCUMENT_ROOT'].'/global/head.php'; ?>
<?php require_once $_SERVER['DOCUMENT_ROOT'].'/config/init.php'; ?>
<?php
$input['title'] = "";
$input['deadline'] = "";
$input['description'] = "";
if(!isset($_SESSION['goals'])) {$_SESSION['goals'] = array();}
if (isset($_POST['submit'])) {
$_SESSION['goalCount'] ++;
$input['title'] = htmlentities($_POST ['title'],ENT_QUOTES);
$input['deadline'] = htmlentities($_POST ['deadline'],ENT_QUOTES);
$input['description'] = htmlentities($_POST ['description'],ENT_QUOTES);
convertDate($input['deadline']);
${'goal'. $_SESSION['goalCount']} = new Goal($input['title'], $input['description'], $_SESSION['username'], $input['deadline']);
array_push($_SESSION['goals'], ${'goal'. $_SESSION['goalCount']});
?>
<div class="top">
<p>h</p>
</div>
<div class="container">
<div class="sixteen columns topbar">
<?php require $_SERVER['DOCUMENT_ROOT'].'/global/header.php'; ?><!-- Content Begins -->
<div class="content">
<h1> OO Test Page - Batch add goals</h1><hr/>
<div class="six columns">
<form action="" method="post">
<fieldset>
<div>
<h4>Title</h4>
<span id='title-result'></span>
<label for="title"></label><br />
<input type="text" id="title" name="title" placeholder="e.g. Graduate" value="" required aria-required="true">
</div>
<div>
<h4>Description</h4>
<span id='description-result'></span>
<label for="description"></label>
<textarea type="description" id="description" placeholder="e.g. with first-class honours" name="description" value="" required aria-required="true"></textarea>
</div>
<div>
<h4>Deadline</h4>
<span id='description-result'></span>
<label for="deadline"></label>
<input rows="2"type="date" id="datepick" placeholder="" name="deadline" value="" required aria-required="true"/>
</div>
<div class="submit">
<input type="submit" name="submit" value="Add">
</div>
</fieldset>
</form></div>
<div class="ten columns">
<?php
foreach ($_SESSION['goals'] as $goal)
{
echo '<div class="goal"><h4>'. $goal->title .'</h4>'. $goal->desc .'</h4><p>'. $goalCount .'</p></div>';
}
echo Goal::$counter;
var_dump($_SESSION['goals'])
?>
</div>
</div>
<!-- Content Ends -->
<?php require $_SERVER['DOCUMENT_ROOT'].'/global/footer.php'; ?>
</div>
</div>
</body>
</html>
Simply initialize that session variables that you need. And no need to use variable variables and using it as a counter to push inside. Just normally push those object inside the session.
Example:
<?php require_once $_SERVER['DOCUMENT_ROOT'].'/includes/classes/Goal.php'; ?>
<?php require_once $_SERVER['DOCUMENT_ROOT'].'/global/head.php'; ?>
<?php require_once $_SERVER['DOCUMENT_ROOT'].'/config/init.php'; ?>
<?php
if(!isset($_SESSION['goals'], $_SESSION['goalCount'])) {
$_SESSION['goals'] = array();
$_SESSION['goalCount'] = 0;
}
if (isset($_POST['submit'])) {
$_SESSION['goalCount'] += 1;
$input['title'] = htmlentities($_POST ['title'],ENT_QUOTES);
$input['deadline'] = htmlentities($_POST ['deadline'],ENT_QUOTES);
$input['description'] = htmlentities($_POST['description'],ENT_QUOTES);
convertDate($input['deadline']);
$goal = new Goal($input['title'], $input['description'], $_SESSION['username'], $input['deadline']);
$_SESSION['goals'][] = $goal;
// ^ add another dimension
} // missing closing curly brace
?>
<div class="top">
<p>h</p>
</div>
<div class="container">
<div class="sixteen columns topbar">
<?php require $_SERVER['DOCUMENT_ROOT'].'/global/header.php'; ?><!-- Content Begins -->
<div class="content">
<h1> OO Test Page - Batch add goals</h1><hr/>
<div class="six columns">
<form action="" method="POST">
<fieldset>
<div>
<h4>Title</h4>
<span id='title-result'></span>
<label for="title"></label><br />
<input type="text" id="title" name="title" placeholder="e.g. Graduate" value="" required aria-required="true">
</div>
<div>
<h4>Description</h4>
<span id='description-result'></span>
<label for="description"></label>
<textarea type="description" id="description" placeholder="e.g. with first-class honours" name="description" value="" required aria-required="true"></textarea>
</div>
<div>
<h4>Deadline</h4>
<span id='description-result'></span>
<label for="deadline"></label>
<input rows="2"type="date" id="datepick" placeholder="" name="deadline" value="" required aria-required="true"/>
</div>
<div class="submit">
<input type="submit" name="submit" value="Add">
</div>
</fieldset>
</form>
</div>
</div>
<div class="ten columns">
<?php
foreach ($_SESSION['goals'] as $goal) {
echo '<div class="goal"><h4>'. $goal->title .'</h4>'. $goal->desc .'</h4><p>'. $goalCount .'</p></div>';
}
?>
</div>
<!-- Content Ends -->
<?php require $_SERVER['DOCUMENT_ROOT'].'/global/footer.php'; ?>
</div>
</div>
Sidenote: Always turn on error reporting.