ZF2 how to wrap content in form fieldset? - php

I have form with fieldsets:
$formConfig = array(
'fieldsets' => array(
...
);
);
$factory = new Zend\Form\Factory();
$form = $factory->createForm($formConfig);
echo $this->form($form);
It renders something like this:
<form>
<fieldset>
<legend>Fieldset label</legend>
<label><span>Elem 1</span><input type="text" name="f1[el1]" /></label>
<label><span>Elem 2</span><input type="text" name="f1[el2]" /></label>
<label><span>Elem 3</span><input type="text" name="f1[el3]" /></label>
</fielset>
</form>
The problem is that I need to wrap content after legend:
<form>
<fieldset>
<legend>Fieldset label</legend>
<div class="wrapper">
<label><span>Elem 1</span><input type="text" name="f1[el1]" /></label>
<label><span>Elem 2</span><input type="text" name="f1[el2]" /></label>
<label><span>Elem 3</span><input type="text" name="f1[el3]" /></label>
<div>
</fielset>
</form>
How can I do that?

Once more you need to understand that a Zend\Form\Fieldset does not equal a HTML <fieldset>! A Zend\Form\Fieldset merely is a collection of Zend\Form\Element that usually represent one entity and you could provide several entities with data from one Form.
Now when it comes to rendering the form, the first thing you should learn about are the several Zend\Form\View\Helper-Classes. You are using the form() view-helper, which automatically translates all Zend\Form\Element using formRow() and all Zend\Form\Fieldset using formCollection(). But you don't want to do that!
When wanting your preferred output, you will be needed to render the form yourself. Something like this could be your view-template:
<?=$this->form()->openTag($form);?>
<fieldset>
<div class="wrapper">
<?=$this->formRow($form->get('f1')->get('el1'));?>
<?=$this->formRow($form->get('f1')->get('el2'));?>
<?=$this->formRow($form->get('f1')->get('el3'));?>
</div>
</fieldset>
<?=$this->form()->closeTag();?>
Now, this already has a little comfort within it, as you'd be using formRow(). You could also split up each form-row and go the very detailled way like:
<label>
<span><?=$this->formLabel($form->get('f1')->get('el1'));?></span>
<?=$this->formInput($form->get('f1')->get('el1'));=>
<?=$this->formElementErrors($form->get('f1')->get('el1'));?>
</label>
Even there, formInput() still is a magic that derives into things like formText(), formSelect(), formTextarea(), etc.., etc...

Related

How use $_GET variable in a Wordpress page

I have a form for asking information about courses , every course has it page, but the information page is one for all.
The form should be something like that:
<form action="#" method="POST">
<label for="name">Name</label>
<input name="name" type="text">
<label for="email">Email</label>
<input name="email" type="email">
<input type="hidden" id="code" value="<?php echo $course_code; ?>">
<input id="submit" type="submit" value="Invia" />
</form>
I wish to change the var $course code according to the referrer page. (With a $_GET var)
I tried "Shortcode Exec PHP" plugin to execute php in wp pages, but doesnt work.
When you POST the form, the variable won't be set in $_GET but in $_POST. It's either one or the other, so if you want to read the $_GET var, you must also use GET on the form, like this:
<form action="#" method="GET">
<label for="name">Name</label>
...
(this is what Fred commented on, but I couldn't expand upon that comment due to my low rep)
I was wrong to use "Shortcode Exec PHP" plugin.
I set a shortcode:
$course_name = $_GET['cn'];
$courses= array("courses1","courses2","couses3");
if (in_array($course_name, $courses)) {
echo $course_name:
}
and the in the wordpress page can be used the name of the shortcode
[couse_name]
Now its work!
You can just use $_REQUEST so it doesn't matter if its a POST or a GET from the form. But I wouldn't use GET from a form unless it was a search or something where the user could bookmark the url and see the result. Mostly use POST for all other instances.
HTML form...
<form method="post">
<label>Name<br>
<input type="text" name="name">
</label>
...
<input type="submit" value="Invia">
</form>
PHP page that handles the form...
<?php
// $_REQUEST will contain POST, GET & COOKIE
echo $_REQUEST['name'];
?>

How to create a yii form with HTML tag without using CAactive and CHtml?

I am new to Yii and have created a form in pure HTML code
Here TesterUserReg is the different controller..
<form id="signup" action="/xyzApp/index.php?r=testerUserReg/create" method="post" accept-charser="UTF-8">
<fieldset>
<div id='solidLine'>
<p>
<label for="name">Name</label><br>
<input id="name" name="username" type="text" class="validate[required,custom[onlyLetter],length[0,100]] text" value="" placeholder="your full name"/>
</p>
<p>
<label for="email">Email</label><br>
<input id="email" name="email" type="text" class="validate[required,custom[email]] text" value="" placeholder="hello#testunity.com"/>
</p>
.................
...............
Sign up
and validating the form by using jQuery validation engine plugin..
and my controller looks like
public function actionCreate()
{
$model=new TesterUserReg;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['TesterUserReg']))
{
$model->attributes=$_POST['TesterUserReg'];
$request = Yii::app()->getRequest();
//$model->ut = $request->getPost('username');
$model->ut_pwd = $request->getPost('password');
$model->ut_email_id = $request->getPost('email');
$model->ut_user_id = md5(uniqid(rand(), true));
$model->ut_reg_date = new DateTime();
if($model->save()){}
//$this->redirect(array('view','id'=>$model->ut_user_id));
}
$this->render('create',array(
'model'=>$model,
));
}
When Im submitting the form it renders to Yii default Login page. Its not calling this controller.
Please help me to come out of this problem.
Thanks.
It is not regarding about your form whether is using or not using CHtml or CActiveForm. it is your controller problem, please check your filter and accessRules inside your Controller.
If you are not sure about what does that do, you can remove it temporarily, and test whether you still face this problem or not.
Learn more? Check out the following link. Some people do suggest beginers read through the Guide before they get their hands dirty. Yii team do maintain a great documentation. Once you have problem on coding you can simply check out the guide or Class Reference, and you'll find out what you need.
http://www.yiiframework.com/doc/guide/1.1/en/basics.controller#filter
Happy coding ^_^
You problem is in your form because in your controller you are trying to get
if(isset($_POST['TesterUserReg']))
Which is never coming from your form then how your form will be processed in your controller.
So what exactly you have to do is change the name of all form inputs as i have mentioned
<form id="signup" action="/xyzApp/index.php?r=testerUserReg/create" method="post" accept-charser="UTF-8">
<fieldset>
<div id='solidLine'>
<p>
<label for="name">Name</label><br>
<input id="name" name="TesterUserReg[username]" type="text" class="validate[required,custom[onlyLetter],length[0,100]] text" value="" placeholder="your full name"/>
</p>
<p>
<label for="email">Email</label><br>
<input id="email" name="TesterUserReg[email]" type="text" class="validate[required,custom[email]] text" value="" placeholder="hello#testunity.com"/>
</p>
By changing the form input names in this manner your controller condition
if(isset($_POST['TesterUserReg']))
will be satisfied and your further process will go on.
Happy Coding :)

keep form value after submit it

I know there are lots others question like this and i found 1.000 answer on the web but none of those work with my code :(
So can someone help me with how to keep email value after submit?
<form name="login-registration" onSubmit="return validateForm()" method="post" action="" >
<label>Email</label>
<input type="email" name="emailinput" id="emailinput" value ="" />
<p id="emptyEmail" class="hidden">Email field is required</p>
<p id="invalidEmail" class="hidden">Email you insert is invalid!</p>
<label>Your password</label>
<input type="password" name="pswinput" id="pswinput" value=""/>
<p id="pswMinMax" class="hidden">Password should be from 4 to 8 caracters</p>
<p id="pswLettNum" class="hidden">Password should be letters and numbers. No special caracters are allow</p>
<label>Repeat password</label>
<input type="password" name="pswrepeatinput" id="pswrepeatinput" value="" onblur="isValidPswRep()"/>
<p id="pswR" class="hidden">Your passwords is different</p>
<input type="checkbox" id="policy" name="policy" value="policy" /> <label>I agree</label>
<p id="checkN" class="hidden">You must agree to our term</p>
<input type="submit" name="submit" value="Login" />
</form>
I try to put some code like:
<input type="email" name="emailinput" id="emailinput" value = "<?php echo htmlspecialchars($_GET['lastname']); ?>" />
But that php line is just displayed inside the field input.
try using $_POST['lastname'] instead of $_GET['lastname']
1)If i am not wrong,i don't see any field with name="lastname" in your code above.
2)Use $_POST because you are posting your form data with method="post".
Assuming that your file have .php as extension and php is installed on your server i would like you to notice that you have an error because you used a POST form while when you apply value to your input field you are trying to use $_GET Further more you did not assign lastnameto any input field, so use emailinput as you apply to this field name="emailinput". You should change htmlspecialchars($_GET['emailinput']); to be htmlspecialchars($_POST['emailinput']);
So your code would look like this
<input type="email" name="emailinput" id="emailinput" value = "<?php echo htmlspecialchars($_POST['emailinput']); ?>" />
This should print your variable inside your input field
There are at least 2 problems there.
The fact that the php is displayed inline suggests that either you have wrong file extension (like the comments suggested), either file is not included in your "know how to read by php" (for lack of a better way to say it, my English is not perfect) directory.
You echo a get when you sent a post (as other answer suggested)
Also... WHY DOES YOUR QUESTION HAVE JS TAG (sorry for caps, wanted to be sure it sticks out)?

How to send a result to a form with php?

I'm really new to PHP but it's awesome language :)
I created simple mathematical calculation script that takes inputs from forms in terms of variables and perform simple calculations. I would like a result to be displayed in another form instead of simply being displayed at the bottom of the page.
<html>
<body>
<p>Simple PHP script for cloning calculations</p>
<form name="form1" method="post" action="">
<p>
<label for="vector_lenght">La taille du vecteur (kb)</label>
<input type="text" name="vector_lenght" id="vector_lenght">
</p>
<p>
<label for="insert_lenght">La taille de l'insert (kb)</label>
<input type="text" name="insert_lenght" id="insert_lenght">
</p>
<p>
<label for="conc_insert">La concentration de l'insert</label>
<input type="text" name="conc_insert" id="conc_insert">
</p>
<p>
<input type="submit" name="ratio_calc" id="ratio_calc" value="Calculer">
</p>
<p>
<label for="ratio">Le ratio à utiliser</label>
<input type="text" name="ratio" id="ratio">
</p>
<p>
<label for="ratio_1">ratio 1:1</label>
<input type="text" name="ratio_1" id="ratio_1">
<br>
<label for="ratio_3">ratio 1:3</label>
<input type="text" name="ratio_3" id="ratio_3">
<br>
<label for="ratio_5">ratio 1:5</label>
<input type="text" name="ratio_5" id="ratio_5">
</p>
</form>
<?php
if($_POST['ratio_calc']!=''){
define("conc_vector", 100);
$vector_lenght = $_POST['vector_lenght'];
$insert_lenght = $_POST['insert_lenght'];
$conc_insert = $_POST['conc_insert'];
$result = (conc_vector * $insert_lenght) / $vector_lenght;
echo "<h1>$result</h1>";
}
?>
</body>
</html>
In your opening form tag, the 'action' attribute specifies what file should receive the HTTP request created when you submit the form. As it is now the value is an empty string, and the script is defaulting to POSTing the data back to the current script.
(I assume this other form is in a different page - if not, skip to the last code snippet)
So, create another PHP script that represents the page you want to contain your results, and put the name of the new script as the value of the 'action' attribute. In the new script, you can retrieve the values sent by your form via the $_POST[] superglobal (in the same way that you already are in the code you posted).
If the new page is named resultPage.php (and is in the same directory as the script you posted), your form tag should look like this:
<form name="form1" method="post" action="resultPage.php">
If you want the data to display inside a form in the new page, do something like this:
<input type="text" value="<?php echo $_POST['conc_insert'] ?>">

Form is returned with HTML tags (for use as a template)

I would like to make a form where users can enter text, and when the form is submitted it will return an output in HTML. The form is for use by me and my colleagues, not end users. We are looking for this solution in order to better style our website's content. We use Wordpress but have failed to find a solution to this issue. (We currently use a basic HTML template, but that makes for time-consuming work when you look at the example below).
Here's how I envisage this form:
<form>
Artist name: <input type="text" name="artist"><br />
Track name: <input type="text" name="track"><br />
Artist link: <input type="text" name="artistlink"><br />
Lyrics name: <input type="text" name="lyrics">br />
</form>
I can do the above part fine. Then the more complicated is making the output contain HTML tags. I want something like this:
<strong>Artist:</strong> <div id="artistname">artist's name</div>
<strong>Track:</strong> <div id="trackname>track name</div>
<strong>Lyrics:</strong> <div id="lyricstext" style="overflow: auto; height: 360px; width: 640px;">
<ul>
<ul>
<li>Its the two kids</li>
<li>New shit improving the movement</li>
<li>We prove this with music</li>
<li>Words for the chosing</li>
</ul>
</ul>
</div>
This way, I can add a jQuery snippet like so:
<script type="text/javascript">
$(function() {
$("ul li:nth-child("odd")").addClass("even");
});
</script>
To be able to style the odd and even <li> differently. I also have greater control over each aspect of the output by creating divs for each part.
I essentially need this as a template for putting our content on the site.
Hope my question is understandable!
Thanks.
Are you looking for something like this?
<form method="POST" action="">
Artist name: <input type="text" name="artist"><br />
Track name: <input type="text" name="track"><br />
Artist link: <input type="text" name="artistlink"><br />
Lyrics name: <input type="text" name="lyrics"><br />
<input type="submit" value="OK" />
</form>
<?php
//check if the submit button has been pressed and if the artist value is not empty
if(isset($_POST['artist']) && $_POST['artist'] != "") {
echo '<strong>Artist:</strong><div id="artistname>"'.$_POST['artist'].'</div>';
}
?>

Categories