I am trying to call the form_open function and pass a value to the function it's calling. I'm trying to pass the value for $data->advisee_id. Once it calls the setAdvisees function I want it to echo the value. I've tried a number of things and I'm thinking I might need to pass it into the array which is the second parameter for form open. Does anyone know how I would do this? The below code I have currently gives an error when form_open is called stating:
"An Error Was Encountered. The URI you submitted has disallowed characters."
eligibletoenroll_view.php
<?= form_open('app/staff/advisees/setAdvisees($data->advisee_id)', array(
'class' => 'user-contact',
)) ?>
advisees.php
function setAdvisees($advisee_id) {
echo $advisee_id;
Just send like this
<?= form_open('app/staff/advisees/setAdvisees/$advisee_id)'
This sill act as
<form action="app/staff/advisees/setAdvisees/$advisee_id"
If advise id coming from array then before form open
$advisee_id = "some thing"
Example
<form action="app/staff/advisees/setAdvisees/25"
Related
Let's say my URL is the following:
https://www.example.com/downloads?query=RobinHood
In my template (.phtml file), I have the following input:
<input type="text" placeholder="Query for books" name="query">
What I want to do is check if the URL parameter called query actually exists, and if it does, I want to put it as the value for the input.
How can I do this? Thanks for any help.
Since .phtml is an extension used for php2, here's what the docs say:
Any component of the GET data (the data following a '?' in the URL) which is of the form, word=something will define the variable $word to contain the value something. Even if the data is not of this form, it can be accessed with the $argv built-in array
So based on this, php2 will automatically create variables for the GET data:
https://www.example.com/downloads?query=RobinHood
$query // should contain "RobinHood"
$argv[0] // should contain "query=RobinHood"
To check if a variable has been set you can use IsSet() function:
The IsSet function returns 1 if the given variable is defined, and 0 if it isn't.
if( IsSet($query) )
{
//...
}
Note: All of the above is purely based on docs. I haven't tested this.
You can check the query parameter from $_GET variable:
<input type="text" placeholder="Query for books" name="query" <?php echo isset($_GET['query']) ? '"value"="'.$_GET['query'].'"' : '' ?>>
Ok so I have the code for a form that is called and works but it needs two varibles grabbed from the string of a url. I have the first and the second is the same for what im doing on any page that I am creating which is alot. Here is the code at the url: collabedit.com/9g99j
Question if Get <?php echo $_GET['id']; ?> is grabbing my id string from the url how do I use this in the echo of my function I just defined at the bottom of the code? Instead of having this: echo DescriptionGet(1256124, 50874); can someone tell me how to put something like this: echo DescriptionGet(1256124, $id);
This would make it so i dont' have to enter that id value for every page I want to create.
Thanks,
Thanks everyone for your replies and I was able to figure it out on my own and actually used exactly what the first reply was.
Now I have a new question about this function. How do I make it grab the image from that same page its grabbing the form code from? I can't figure this part out and its keeping me from doing mass automation for this site.
Anyone help?
Try this:
$id = $_GET['id'];
echo DescriptionGet(1256124, $id);
You can change your function definition from:
function DescriptionGet($c, $id)
to
function DescriptionGet($c, $id=50874)
Each time when you will call DescriptionGet($c) it will behave as you passed $id=50874 but also if you need you can call DescriptionGet($c, 20) and $id in the function will be set to 20.
And in case you want to simple use $_GET['id'] as function parameter you can simple run
echo DescriptionGet(1256124, intval($_GET['id']));
you don't even need to use extra variable.
i just realize ,that i can't insert the current user session that logged in after doing create on the table i am prefer to,
i cook the codes on the _form.php like this , then that warning appear (Cannot use a scalar value as an array)
$use_id=Yii::app()->user->id;$use_id=intval($use_id);
echo $form->hiddenField($model,'operator_id',$use_id); ?>
<?php echo $form->error($model,'operator_id'); ?>
i have tried to not using 'intval' , but it appears the same, please help me to find the solution
You can assign the session value in your controller function. you need not specified in form page or any other view pages.
example :
In controller->create function
before your save()
$model->operator_id = Yii::app()->user->id;
i think its best way to assign values without using hidden fields
according to documentation of CActiveForm
public string hiddenField(CModel $model, string $attribute, array $htmlOptions=array ( ))
You need to change these
$use_id=Yii::app()->user->id;$use_id=intval($use_id);
echo $form->hiddenField($model,'operator_id',$use_id);
//to
$use_id=Yii::app()->user->id;
echo $form->hiddenField($model,'operator_id',array('value'=> $use_id));//
Usually I used Zend Form's messages in the following way:
Code in form:
$element = new Zend_Form_Element_Text('form_resource_type');
$validator = new Zend_Validate_NotEmpty();
$validator->setMessages(
array('isEmpty' => 'Please choose type of resource')
);
$element->addValidator($validator);
$element->setRequired(true);
$this->addElement($element);
Code in view:
<?php foreach($subForm->getElementsAndSubFormsOrdered() as $element):?>
<?php echo $element?>
<?php foreach($element->getMessages() as $errorMsg):?>
<?php echo $this->escape($errorMsg);?>
<?php endforeach;?>
<?php endforeach;?>
So, for outputting error messages I used getMessages() function. But right now under certain circumstances (in case of special combination of fields' values) I need to mark element as invalid and add custom error message. I tried to use addError($message) function, but it adds message to _errorMessages property, while getMessages output _messages Zend_Form_Element property.
I didn't find function of adding messages to the _messages property. How can I do this? Or I should not work with this property directly and change a way of outputting error messages in view?
UPD:
I use Zend Framework 1.12
Since you are accessing the error messages from the form element. Then you can try to set message in the element by using the following statement in the controller:
$form->getElement('elementName')->addErrorMessage('custom Message');
You will then be able to print the message in your way.
You can use markAsError() for marking an element as invalid Custom Error Messages
I think this will do the trick for you
if($error)
{
$element->addErrorMessage('Custom Error');
$element->markAsError();
}
In my project, I am using pagination and I used these statements to get the page number detail:
$page=$this->uri->segment(3);
$this->session->set_userdata('page',$page);
echo $this->session->userdata('page');
When I print this session value in that page itself, I get the value correctly and when I click on the particular link and then print that data, I am getting the value like 'images'.
Why is this happening?
However, when I write the statements like
$page=$this->uri->segment(2);
$this->session->set_userdata('page',$page);
echo $this->session->userdata('page');
it's working fine.
My URL is: http://localhost/CI/user/index/4
I have the same problem with you, the session variable always get value 'images'. Then i realize that: if we made a session variable, then we must call redirect() directly.
This is my example:
$page=$this->uri->segment(3);
$this->session->set_userdata('page',$page);
redirect('control/function2');
Then, you can get continue your code in the 'function2'.
Solved the issue by writing another function, setting the session and redirecting to above function like this:
function set_session(){
$id=$this->uri->segment(3);
$this->session->set_userdata('id',$id);
redirect('set_uri_session');
}
function set_uri_session(){
$id=$this->uri->segment(3);
$this->session->set_userdata('id',$id);
echo $this->session->userdata('id');
}