Form functions contain - forms

How I can send a form using functions php?
Example
function xxx1() { echo '<input type="text" name="xxx1" />'; }
function xxx2() { echo '<input type="text" name="xxx2" />'; }
PHP code
<form method="POST" action="">
<?php
xxx1();
xxx2();
?>
</form>
It that possible?

Add php tags around the functions.
I tested your code in the following matter and everything is working correctly.
<?php
function xxx1(){ echo '<input type="text" name="xxx1" />'; }
function xxx2(){ echo '<input type="text" name="xxx2" />'; }
?>
<form method="POST" action="">
<?php
xxx1();
xxx2();
?>
</form>

Related

Form button not working properly in IE11(and probably more)

I've made page where you can get an overview over invoices.
Everything works great, except from with Internet Explorer...
When I click the see order button, nothing happens in IE.
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" onSubmit="window.location.reload()">
<tr><td><?php
$Date = $row['date'];
echo $Date;
?></td><td><?php echo $row['autoid'];?></td>
<input name='implodehideu' type='hidden' value='<?php echo $row['implodeuke'];?>;'>
<input name='implodehidew' type='hidden' value='<?php echo $row['implodehelg'];?>;'>
<input name='personid' type='hidden' value='<?php echo $row['primeid'];?>;'>
<input name='invoiceid' type='hidden' value='<?php echo $row['autoid'];?>'>
<td><input type="submit" name="seeorder" id="submit" value="Order overview"></td>
<td><?php echo $row['firstname'] . " " . $row['surname'];?></td>
<td><?php echo $row['amount'];?></td>
<?php
if ($paidboolean)
echo "<td bgcolor='#3AA849'>YES</td>";
else
echo "<td bgcolor='#e94336'>NO</td>";
if (!$paidboolean) {
?>
<td><input type="submit" name="confirm" id="submit" value="Mark as paid"></td>
<?php
} else {
?>
<td><input type="submit" name="notconfirm" id="submit" value="Mark as not paid"></td>
<?php
}
?>
<td><input type="submit" name="slett" id="submit" value="Slett"></td>
</form>
<?php
if (isset($_POST['seeorder'])) {
.....Things to show up
}
Try to set the id argument of the submit button to something else than 'submit'. That helped me with the same problem.
<input type="submit" name="slett" id="btnSubmit" value="Slett" />

Joomla - ignored form fields with $_POST

I've been trying to create a joomla form in my view. But when I use the "apply" function and try to retrieve information from the $_POST global I lack two out of three fields. I can't for my life figure out what's wrong.
Since I get one of the fields I assume that I've created the form in a somewhat Joomla fashion.
<form action='<?php echo JRoute::_('index.php?option=com_mycomponent'); ?>' method='post' name="adminForm">
<fieldset><legend><?php echo JText::_('COM_MYCOMPONENT_SETTING_LEGEND'); ?></legend>
<label for=' <?php echo JText::_('COM_MYCOMPONENT_FORM_NAME'); ?>'> <?php echo JText::_('COM_MYCOMPONENT_USE'); ?>: </label>
<select name=' <?php echo JText::_('COM_MYCOMPONENT_FORM_NAME'); ?>' id=' <?php echo JText::_('COM_MYCOMPONENT_FORM_NAME'); ?>'>
<option value='0'> <?php echo JText::_('COM_MYCOMPONENT_NO')?></option>
<?php
if ($this->m_use_osn)
echo "<option value='1' selected>";
else
echo "<option value='1'>";
?>
<?php echo JText::_('COM_MYCOMPONENT_YES')?></option>
</select>
<br />
<label for='<?php echo JText::_('COM_MYCOMPONENT_URL_FORM_NAME'); ?>'> <?php echo JText::_('COM_MYCOMPONENT_URL'); ?>: </label>
<?php
if (!empty($this->m_osn_url))
echo "<input type='text' value='".$this->m_osn_url."' /><br />";
else
echo "<input type='text' placeholder='".JText::_('COM_MYCOMPONENT_URL_PLACEHOLDER')."' /><br /> ";
?>
<label for='<?php echo JText::_('COM_MYCOMPONENT_KEY_FORM_NAME'); ?>'> <?php echo JText::_('COM_MYCOMPONENT_KEY'); ?>: </label>
<?php
if (!empty($this -> m_osn_key))
echo "<input type='text' value='" . $this -> m_key . "' /><br /> ";
else
echo "<input type='text' placeholder='".JText::_('COM_MYCOMPONENT_KEY_PLACEHOLDER')."' /><br />";
?>
<input type="hidden" name="task" value="" />
<input type='submit' value=' <?php echo JText::_('COM_MYCOMPONENT_SUBMIT'); ?> />
</fieldset>
</form>
However, what a var_dump($_POST) gives me is this:
array(3) {
["mycomponent_use"]=>
string(1) "0"
["task"]=>
string(5) "apply"
["option"]=>
string(14) "com_mycomponent"
}
So, for some reason I'm not getting any of the text input fields. I've tried to get the data through JFactory::getApplication()->input... as well, for some reason it ignore my <input type='text' />. Do I have to register these in the $_POST global in some way? Since I cannot use a regular submit button but must depend on the joomla bar I am getting a bit frustrated with getting a semi empty $_POST.
You have forgot to give the name of input fields in form. Please assign the distinct name for each field.You may refer this.
Dealing with Forms

Codeigniter How to save data from a form to a database

Codeigniter
Hi. I want to send my form data to a database. The data are checked ok but what to do next?
My controlls
<?php
class Form extends CI_Controller {
function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Imie', 'required|min_length[5]|max_length[25]|required|alpha');
$this->form_validation->set_rules('surname', 'Nazwisko', 'required|min_length[5]|max_length[25]|required|alpha');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('number', 'Numer telefonu', 'required|alpha_numeric|max_length[10]');
$this->form_validation->set_rules('brand', 'Marka', 'required|alpha');
$this->form_validation->set_rules('model', 'Model', 'required|alpha');
$this->form_validation->set_rules('year', 'Rok produkcji', 'required|alpha_numeric|max_length[5]');
$this->form_validation->set_rules('km', 'Ilosc KM', 'required|alpha_numeric|max_length[5]');
$this->form_validation->set_rules('licenceseplate', 'Tablica rejestracyjna', 'required|max_length[15]');
$this->form_validation->set_rules('description', 'Opis', 'required|max_length[300]');
$this->form_validation->set_rules('city', 'Miasto', 'required|max_length[30]');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform');
}
else
{
$this->load->view('formsuccess');
}
}
}
?>
My view
<html>
<head>
<title>My Form</title>
</head>
<body>
<?php echo form_open('form'); ?>
<h5>Wpisz swoje imiÄ™</h5>
<?php echo form_error('name'); ?>
<input type="text" name="name" value="<?php echo set_value('name'); ?>" size="50" />
<h5>Nazwisko</h5>
<?php echo form_error('surname'); ?>
<input type="text" name="surname" value="<?php echo set_value('surname'); ?>" size="50" />
<h5>Email Address</h5>
<?php echo form_error('email'); ?>
<input type="text" name="email" value="<?php echo set_value('email'); ?>" size="50" />
<h5>Numer telefonu</h5>
<?php echo form_error('number'); ?>
<input type="text" name="number" value="<?php echo set_value('number'); ?>" size="50" />
<h5>Marka</h5>
<?php echo form_error('brand'); ?>
<input type="text" name="brand" value="<?php echo set_value('brand'); ?>" size="50" />
<h5>Model</h5>
<?php echo form_error('model'); ?>
<input type="text" name="model" value="<?php echo set_value('model'); ?>" size="50" />
<h5>Rok produkcji</h5>
<?php echo form_error('year'); ?>
<input type="text" name="year" value="<?php echo set_value('year'); ?>" size="50" />
<h5>km</h5>
<?php echo form_error('km'); ?>
<input type="text" name="km" value="<?php echo set_value('km'); ?>" size="50" />
<h5>Rejestracja </h5>
<?php echo form_error('licenceseplate'); ?>
<input type="text" name="licenceseplate" value="<?php echo set_value('licenceseplate'); ?>" size="50" />
<h5>Opis </h5>
<?php echo form_error('description'); ?>
<input type="text" name="description" value="<?php echo set_value('description'); ?>" size="50" />
<h5>Miasto </h5>
<?php echo form_error('city'); ?>
<input type="text" name="city" value="<?php echo set_value('city'); ?>" size="50" />
<div><input type="submit" value="Submit" /></div>
</form>
</body>
</html>
I don't expect the final solution, but I need help what is the next step
My datebase:
After the form validation you can save data
In your controller
if ($this->form_validation->run() == FALSE){
$this->load->view('myform');
}
else {
$this->your_model->save($data);
}
your_model.php
public function save($data) {
$this->db->set($data);
$this->db->insert(table name here);
$this->db->insert_id();
}
ok
My view looks like this:
<?php
class Form extends CI_Controller {
function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Imie', 'required|min_length[5]|max_length[25]|required|alpha');
$this->form_validation->set_rules('surname', 'Nazwisko', 'required|min_length[5]|max_length[25]|required|alpha');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('number', 'Numer telefonu', 'required|alpha_numeric|max_length[10]');
$this->form_validation->set_rules('brand', 'Marka', 'required|alpha');
$this->form_validation->set_rules('model', 'Model', 'required|alpha');
$this->form_validation->set_rules('year', 'Rok produkcji', 'required|alpha_numeric|max_length[5]');
$this->form_validation->set_rules('km', 'Ilosc KM', 'required|alpha_numeric|max_length[5]');
$this->form_validation->set_rules('licenceseplate', 'Tablica rejestracyjna', 'required|max_length[15]');
$this->form_validation->set_rules('description', 'Opis', 'required|max_length[300]');
$this->form_validation->set_rules('city', 'Miasto', 'required|max_length[30]');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform');
}
else
{
/*$this->load->view('formsuccess');*/
$this->load->model ( 'form' );
$this->form->save($data);
}
}
}
?>
my model
class form extends Model{
function form(){
parent::Model();
$this->load->helper('url');
}
public function save($data) {
$this->db->set($data);
$this->db->insert(form);
$this->db->insert_id();
}
}
table in date base name form
I have no idea how you want it
You have to send the data to the model
There you can use active record or normal query to save the data
It must be noted you need to include the model in the controller
$this->load->model ( 'Your model name' ); this should be included in the controller
$this->yourmodelname->functioninmodel this is the controller function call to the model

Codeigniter update mysql table data from form with checkbox

New to codeigniter and trying to get my head around updating checked rows from a user form.
My view generates a form with MySQL data as below:
<?php
echo form_open('masterdata/update_customers');
?>
<table>
<tr>
td> </td><td>Customer Name</td><td>postalcode</td>
<tr>
<?php if(isset($records)) : foreach ($records as $row) : ?>
<tr>
<td>
<input type=checkbox name="editcustomer[]" id="editcustomer[]" value="<?php echo $row->id ?>">
</td>
<td>
<input type="text" name="customername_<?php echo $row->id ?>" id="customername_<?php echo $row->id ?>" value="<?php echo $row->customer_name ; ?>" >
</td>
<td>
<input type="text" name="postalcode_<?php echo $row->id ?>" id="postalcode_<?php echo $row->id ?>" value="<?php echo $row->postalcode ; ?>" >
</td>
</tr>
<?php endforeach ; ?>
</table>
<input type="submit" value="Update Selected">
<?php else : ?>
<h2> No Records Found</h2>
<?php endif; ?>
<?php echo form_close(); ?>
This works perfectly well as I get my unique name and values for all input fields.
My issue is now trying to pass the selected checkboxes and there values to codeigniter and get it to update each row.
traditionally I would use foreach($_POST['editcustomer'] as $editcustomer){ but cant get my head around this in codeigniter.
my controller function, update_customers at this stage is very basic:
function update_customers()
{
$this->form_validation->set_rules("customer_name","`Customer Name`","required|min_length[6]|xss_clean");
$this->form_validation->set_rules("postalcode","`Postal Code`","required|xss_clean|min_length[6]");
if ($this->form_validation->run() == FALSE){
$data["message"]="";
$data['title']="Master Data Home Page";
$this->load->view("master_data/view_master_data_header",$data);
$this->load->view("master_data/view_master_data_nav");
$this->load->view("master_data/view_content_master_data_manage_customers");
$this->load->view("master_data/view_master_data_footer");
} else {
$data = array(
'customer_name' => $this->input->post('customer_name'),
'postalcode' => $this->input->post('postalcode'),
);
$this->model_master_data->update_customer_records($data);
$this->customers_updated();
}
}
My model function, update_customer_records is:
function update_customer_records($data)
{
$this->db->where('id',$this->input->post('id'));
$this->db->update('customers',$data);
}
I know there is quite a bit missing here, like processing only the rows checked. but not sure how to add this in. secondly, my unique name and id's being generated, as per my view is:
name="customername_<?php echo $row->id ?>" id="customername_<?php echo $row->id ?>"
how do get the update to apply to the unique form input name? I need to append the prefix customername_ and postalcode_ to the input->post(?) value?
Any direction or assistance here will be appreciated. thanks a million in advance.
With CodeIgniter, if you have to get checkbox values, you have to use $this->input->post('name_of_your_checkbox'). It's return an array.
For example, if you have that in a view:
<input type="checkbox" name="editcustomer[]" value="1" />
<input type="checkbox" name="editcustomer[]" value="2" />
<input type="checkbox" name="editcustomer[]" value="3" />
From the controller:
public function update_customers() {
$editcustomer = $this->input->post('editcustomer');
}
Return:
Array {
[0] => 1
[1] => 2
[2] => 3
}

Add or remove class with prototype

Cant figure the PROTOTYPE script for adding or removing a the css class name "selected" to img element based in click function (already done for Jquery...) but it must be in Prototype. And its driving me crazy. Cant make it work for prototype....
My original code is (Magento store)
<div class="block block-poll">
<div class="block-title">
<strong><span><?php echo $this->__('Community Poll') ?></span></strong>
</div>
<form id="pollForm" action="<?php echo $action ?>" method="post" onsubmit="return validatePollAnswerIsSelected();">
<div class="block-content">
<p class="block-subtitle"><?php echo $this->htmlEscape($poll->getPollTitle()); ?></p>
<?php if( $poll_answers ): ?>
<ul id="poll-answers">
<?php foreach( $poll_answers as $answer ): ?>
<li>
<input type="radio" name="vote" style ="display:none;" class="radio poll_vote" id="vote_<?php echo $answer->getId() ?>" value="<?php echo $answer->getAnswerId() ?>" />
<?php
$stripped = $answer->getAnswerTitle();
$stripped_final = str_replace(" ", "_", strtolower($stripped)); //Value (simplified)
?>
<span class="label"><label for="vote_<?php echo $answer->getId() ?>"><img src="http://www.passione.pt/media/poll/<?php echo $stripped_final; ?>.png" id="chooser" class="chooser" alt="<?php echo $this->htmlEscape($answer->getAnswerTitle()) ?>" onClick="document.getElementById('vote_<?php echo $answer->getId() ?>').checked =true;"/></label></span>
</li>
<?php endforeach; ?>
</ul>
<script type="text/javascript">decorateList('poll-answers');</script>
<?php endif; ?>
<div class="actions">
<button type="submit" title="<?php echo $this->__('Vote') ?>" class="button"><span><span><?php echo $this->__('Vote') ?></span></span></button>
</div>
</div>
</form>
</div>
<?php endif; ?>
For Jquery.
Could you transform for Prototype 1.7...?
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script type="text/javascript">
var Chooser = $('#poll-answers img');
Chooser.click(function() {
$('.chooser').removeClass('selected');
if(!Chooser.hasClass('selected')) {
$(this).addClass('selected');
} else {
$(this).removeClass('selected');
}
});
</script>
Prototype version of that click handler (untested):
$('poll-answers').on('click', 'img', function(event, element) {
$$('.chooser').invoke('removeClassName', 'selected');
element.toggleClassName('selected');
});
Edit: Changed to toggleClassName as per #Victor's suggestion. +7 points for him, and from each to invoke thanks to #Geek Num. +7 points for him and 1 left over for me.