I'm trying to include an image inside a button using symfony1.4 with this code:
<?php
echo button_to(image_tag('icon.png')."button_name",'url-goes-here');
?>
But the result i get, instead of what i want is a button with "img src=path/to/the/icon.png button_name" as the value of the button. I've google'd it long enought and found nothing, so i'll try asking here.
In other words:
i'd like to find the way to generate html similar to:<button><img src=..>Text</button> but with a symfony url associated in the onclick option
How can i do it to put an image inside a button with symfony? Am i using the helpers wrong?
Thank you for your time!
You are using Symfonys button_to function incorrectly. From the documentation:
string button_to($name, $internal_uri, $options) Creates an
button tag of the given name pointing to a routed URL
As far as I can tell, the button_to function does not allow for image buttons. Instead, you will probably create the button tag yourself and use symfonys routing to output the url.
I finally created my own helper to display this kind of buttons. I know is not very efficient and flexible but works in my case. Here is the code
function image_button_to($img,$name,$uri,$options){
$sfURL = url_for($uri);
$sfIMG = image_tag($img);
if(isset($options['confirm'])){
$confirm_text = $options['confirm'];
$jsFunction = 'if(confirm(\''.$confirm_text.'\')){ return window.location=\''.$sfURL.'\';}else{return false;}';
}else{
$jsFunction = 'window.location="'.$sfURL.'";';
}
$onclick = 'onclick="'.$jsFunction.'"';
if(isset($options['title'])){
$title = 'title=\''.$options['title'].'\' ';
}else{
$title = '';
}
if(isset($options['style'])){
$style = 'style=\''.$options['style'].'\' ';
}else{
$style = '';
}
return '<button type="button" '.$onclick.$title.$style.' >'.$sfIMG." ".$name.'</button>';
}
With this function as helper, in the templates i just have to:
<?php echo image_button_to('image.png',"button_name",'module/actionUri');?>
hope this be useful for someone ;)
Related
I would like a simple PHP captcha script that would require the user so solve a captcha to load the website page. Preferably a captcha that doesn't need a third party such as reCAPTCHA, all done locally.
EDIT: Ended up making a text box to enter the link you would like to protect and it would automatically generate a captcha protected link, and a deletion link to delete the protected link.
You can look at the example here:
https://code.tutsplus.com/tutorials/build-your-own-captcha-and-contact-form-in-php--net-5362
I would also recommend using the timer on your page since bots are becoming better at passing capchas. By timer, I mean time between landing on the page and creating an account/submitting the form.
This is VERY simple. Usually no one is going to put any effort to bypass the captcha unless you have something worth while aka $$$$.
Just post an image with a question. Like What is 345 x 100,000 + 957?
The have an small form with a <input> for them to answer.
You could have any number of images and answers.
$captcha[] = array($answer,$image);
$captcha[] = array($answer,$image);
$captcha[] = array($answer,$image);
Then
$cnt = count($captcha) - 1;
$rec = rand(0,$cnt);
$answer= captcha[$rec][0];
$image = captcha[$rec][1];
echo '<img scr="$image"/>';
echo '<form action="$url" method="post">';
echo '<input type = "text" name="answer" /></form>';
$id = intval(str_replace('.','',$_SERVER['REMOTE_ADDR']));
file_put_contents("$id.txt",$answer);
The in the $url page:
$id = intval(str_replace('.','',$_SERVER['REMOTE_ADDR']));
$answer = intval(file_get_contents("$id.txt"));
unlink("$id.txt");
$pass = false;
if $answer == intval($_POST['answer']){$pass = true;}
Hope you are all doing well.
I need some help from you regarding my question that how can I get single data by CHtml Query into Text Field.
for example I am using
<?php echo $form->dropDownList($model,'im_costprice', CHtml::listData(Purchaseorddt::model()->findAll(" pp_purordnum = '$pp_purordnum' "), 'pp_purchasrate', 'pp_purchasrate'), array('id'=>'purchasrate')); ?>
BUT I want to get single data into textfiled so that user can edit/ change the data. Such as:
<?php echo $form->textField($model,'im_costprice', CHtml::Data(Purchaseorddt::model()->findByAttributes(" pp_purordnum = '$pp_purordnum' "), 'pp_purchasrate', 'pp_purchasrate'), array('id'=>'purchasrate')); ?>
It is showing ERROR. Fatal error: Call to undefined method CHtml::Data()...
How Can I solve this. Please help me with some idea.
Helps are highly appreciated .
Just store the value of the Purchaseorddt::model()->findByAttributes part in a separate variable and replace that variable in the place of your CHtml::Data section.
Edit - Something on these lines would work. Do remember though that it is a better approach to do your queries in the controller or the model.
<?php $x = Purchaseorddt::model()->findByAttributes( array('pp_purordnum' => $pp_purordnum));
$y = $x['pp_purchasrate'];
$model->im_costprice = $y;
echo $form->textField($model,'im_costprice',array('id'=>'purchasrate')); ?>
In your controller you can put code like that
$purchaseorddt = Purchaseorddt::model()->findByAttributes("pp_purordnum = '$pp_purordnum'");
and then assign like
if(!empty($purchaseorddt)) {
$model->in_costproce = $purchaseorddt->pp_purchasrate;
}
then in view use like
echo $form->textField($model,'im_costprice', array('id'=>'purchasrate'));
for more information on cactiveform textField http://www.yiiframework.com/doc/api/1.1/CActiveForm#telField-detail
just simple call the model in controller like this :
$data = Purchaseorddt::model->findByPK($pp_purordnum);
$model->costprice=$data->pp_purordnum;
that code will automaticly fill the value in textfield
Is it possible to create an HREF link that calls a PHP function and passes a variable along with it?
<?php
function sample(){
foreach ($json_output->object ){
$name = "{$object->title}";
$id = "{$object->id}";
print "<a href='search($id)' >$name</a>";
}
}
function search($id){
//run a search via the id provide by the clicking of that particular name link
}
?>
You can do this easily without using a framework. By default, anything that comes after a ? in a URL is a GET variable.
So for example, www.google.com/search.html?term=blah
Would go to www.google.com/search.html, and would pass the GET variable "term" with the value "blah".
Multiple variables can be separated with a &
So for example, www.google.com/search.html?term=blah&term2=cool
The GET method is independent of PHP, and is part of the HTTP specification.
PHP handles GET requests easily by automatically creating the superglobal variable $_GET[], where each array index is a GET variable name and the value of the array index is the value of the variable.
Here is some demo code to show how this works:
<?php
//check if the get variable exists
if (isset($_GET['search']))
{
search($_GET['search']);
}
function Search($res)
{
//real search code goes here
echo $res;
}
?>
Search
which will print out 15 because it is the value of search and my search dummy function just prints out any result it gets
The HTML output needs to look like
anchor text
Your function will need to output this information within that format.
No, you cannot do it directly. You can only link to a URL.
In this case, you can pass the function name and parameter in the query string and then handle it in PHP as shown below:
print "<a href='yourphpscript.php?fn=search&id=$id' >$name</a>";
And, in the PHP code :
if ($_GET['fn'] == "search")
if (!empty($_GET['id']))
search($id);
Make sure that you sanitize the GET parameters.
No, at least not directly.
You can link to a URL
You can include data in the query string of that URL (<a href="myProgram.php?foo=bar">)
That URL can be handled by a PHP program
That PHP program can call a function as the only thing it does
You can pass data from $_GET['foo'] to that function
Yes, you can do it. Example:
From your view:
<p>Edit
Where 1 is a parameter you want to send. It can be a data taken from an object too.
From your controller:
function test($id){
#code...
}
Simply do this
<?php
function sample(){
foreach ($json_output->object ){
$name = "{$object->title}";
$id = "{$object->id}";
print "<a href='?search=" . $id . "' > " . $name . "</a>";
}
}
if (isset($_REQUEST['search'])) {
search($_REQUEST['search']);
}
function search($id){
//run a search via the id provide by the clicking of that particular name link
}
?>
Also make sure that your $json_output is accessible with is the sample() function. You can do it either way
<?php
function sample(){
global $json_output;
// rest of the code
}
?>
or
<?php
function sample($json_output){
// rest of the code
}
?>
Set query string in your link's href with the value and access it with $_GET or $_REQUEST
<?php
if ( isset($_REQUEST['search']) ) {
search( $_REQUEST['search'] );
}
function Search($res) {
// search here
}
echo "<a href='?search='" . $id . "'>" . $name . "</a>";
?>
Yes, this is possible, but you need an MVC type structure, and .htaccess URL rewriting turned on as well.
Here's some reading material to get you started in understanding what MVC is all about.
http://www.phpro.org/tutorials/Model-View-Controller-MVC.html
And if you want to choose a sweet framework, instead of reinventing the MVC wheel, I highly suggest, LARAVEL 4
i'm building a form + form validation class , and i also wanted to add captcha to this.
The captcha image is showing, however it itsn't storing anything in the $_SESSION.
I am using this captcha script:
https://github.com/gesf/captcha.class.php
Now in my controller i use this :
$data['regform']->addfield('user_captcha', 'Human verification', 'captcha', 'captcha' );
And that generates the following :
<label>
<span>Human verification</span>
<img name="user_captcha" src="http://www.websiteurl.com/dev/misc/captcha.php?c=1"><input type="text" name="user_captcha" value="" />
</label>
The image is showing like it should. However i'm not able to validate the input because it's not writing to the session. Now in the image file captcha.php it loads the class Captcha , and in this class constructor it tries to write to the session :
function Captcha($letter = '', $case = 5) {
$this->_capCase = $case;
if (empty($letter)) {
$this->StringGen();
} else {
$this->_capLength = strlen($letter);
$this->_capString = substr($letter, 0, $this->_capLength);
}
#session_start();
$_SESSION['asd'] = 'asd';
$_SESSION["CAPTCHA_HASH"] = sha1($this->_capString);
$this->SendHeader();
$this->MakeCaptcha();
}
My session always stays empty. But when i try the following :
<?php $_SESSION['bleh'] = 'asd'?>
<?php echo $form; ?>
It adds 'bleh' to the session like it should.
I really can't see why it won't write to the session..
could someone help me out ??
Thanks!!
Make sure, that session_start() is called before any output for every single page. As I can see, you are using # operator, that shuts up some errors. Can you remove it and tell us what does it output?
Also, your sessiaon_start() call is somewhere in the middle of the script. Perhaps there are some other output before that.
I am new to PHP and I was hoping someone could help me determine what the best way to go about creating a reusable view would be. What I have is a form that will be used to Add or Edit data. Therefore the view will be identical except in the case of an Edit the form fields will be populated.
So I am creating an HTML helper that takes 2 parameters. The first will be form data (if there is any{edit}) and the second will be a bool that flags whether this is an insert or and edit(to change form action).
My question is... how should I handle the first parameter if the form is to used to Add data and therefore does not contain data? Optional parameter?
EDIT--
I am using CodeIgnitor as my MVC framework. That is what those form functions are being inherited.. fyi
Thanks..
<?php
if(!defined('BASEPATH') ) exit('No direct script access allowed');
if(!function_exists('WorkOrderForm'))
{
function WorkOrderForm($array = array('$query'),$edit)
{
$formHtml = "";
$attributes = array('class'=>'order','id'=>'orderForm');
if($edit)
{
$formHtml += form_open('order/update',$attributes);
}
else
{
$formHtml += form_open('order/add',$attributes);
}
$formHtml += form_input('name',$query[0]->name);
$formHtml += form_textarea('name',$query[0]->description);
$dropOptions = array('nstarted'=>'Not Started','complete'=>'Done','started'=>'In Progress');
$formHtml += form_dropdown('status',$dropOptions,$query[0]->status);
$formHtml += form_input('name',$query[0]->startDate);
$formHtml += form_input('name',$query[0]->endDate);
$formHtml += form_close();
return $formHtml;
}
}
?>
What are you guys doing? A reusable view is so much easier than this. Simply create a view and save it in the views folder. Add the fields that will appear both when adding and editing data and use an if statement in the value parameter to determine if it has data.
E.g.
Controller:
public function add()
{
$data['method'] = 'add';
$data['form_data'] = $this->some_model->get_something();
$this->load->view('reusable_view', $data);
}
public function edit($id)
{
$data['method'] = 'edit';
$data['form_data'] = $this->some_model->get_something($id);
$this->load->view('reusable_view', $data);
}
View:
<form method="post" action="my_controller/" . <?php echo $method; ?>>
<input type="text" value="<?php if ( isset($form_data['something']) ) {echo $form_data['something'];} " />
</form>
I see no reason to populate a form in a controller as that's not the way MVC works. Using a helper in order to populate the form is also weird, I think you've slightly missed the point of how Codeigniter works.
First off, the default argument should be on the right. And I would default it to 'false' or NULL.
function WorkOrderForm($edit, $array = false)
Then maybe check if $array is not true, and set all the $query[0] to NULL? So something like...
if(!$array) {
$query[0]->name = $query[0]->description = $query[0]->status = null;
}
There might be a more direct approach, but that's one way.
Firstly, if you're sending your query to the helper function as an array, there is no reason to turn it into another array. ie $array = array('$query') should just be $query this way you can access properties like this: $query->name as opposed to $query[0]->name. Secondly, if you're not editing the form entry, your $query would be empty, so you can use that as the trigger for what to return (either the blank form or the populated form):
function WorkOrderForm($query)
{
if($query!='')
{
//$formHTML=populated form
} else {
//$formHTML=empty form
}
return $formHTML;
}
Okay? But, there's a problem... The code you have in your helper won't work. You're using an arithmetic operator += to (assuming) concatenate the form data. What this does is try to add 1 to a string, which will always equal 0. What you're looking for is the .= operator; this will concatenate the form as would be expected. However, this offers you little control over how the form will look (as is, it will put all the form elements side-by-side -- not too pretty). What you could do is instead of concatenating them all together, push them into an array, then echo the form elements out one-by-one:
if($query!=''){
$form_array=array();
array_push($form_array,form_open('order/update',$attributes));
array_push($form_array,form_input('name',$query->name));
array_push($form_array,form_textarea('name',$query->description));
$dropOptions = array('nstarted'=>'Not Started','complete'=>'Done','started'=>'In Progress');
array_push($form_array,form_dropdown('status',$dropOptions,$query->status));
array_push($form_array,form_input('name',$query->startDate));
array_push($form_array,form_input('name',$query->endDate));
array_push($form_array,form_close());
}else{
$form_array=array();
array_push($form_array,form_open('order/add',$attributes));
array_push($form_array,form_open('order/update'));
array_push($form_array,form_input('name'));
array_push($form_array,form_textarea('name'));
$dropOptions = array('nstarted'=>'Not Started','complete'=>'Done','started'=>'In Progress');
array_push($form_array,form_dropdown('status',$dropOptions));
array_push($form_array,form_input('name'));
array_push($form_array,form_input('name'));
array_push($form_array,form_close());
}
return $form_array;
Then to present the form elements, iterate through the $form_array array that was returned:
$form_data='';//blank for new item, or data to populate form with to edit an item
$form_el = WorkOrderForm($form_data);
foreach($form_el as $key=>$val){
echo $val.'<br>';//I just added a <br> here so each form element will be on a new line; change to fit your needs
}
Hope this helps...