Error in a php echo in onchange function parameter - php

<?php echo form_dropdown('ord_id',$pacc_ord, $value['pac_cod'], 'id="IdPac" onchange="func1(this),func2(this, echo $value['prod_id'];);"')?>
i need to print the value of $value['prod_id'] (number)
in my function arguments. but it doesn't work
how can i solve that?

<?php echo form_dropdown('ord_id',$pacc_ord, $value['pac_cod'], 'id="IdPac" onchange="func1(this),func2(this, '.$value['prod_id'].');"')?>
You don't have to use echo when passing the values as parameters. Just concatenate (using '..') the prod_id into the last parameter.

Related

passing of two php variables from one php page to next php page with the help of SQL

I want to pass the two variable from the below php script to the next php page that is pid and title. pid is working fine but I don't know how to take the title from SQL query and pass it to the next php script.
$result= mysqli_query($bd, "select p.title, p.description from POI p where p.pid='$pid'");
while($row=mysqli_fetch_array($result))
{
echo "<i><h2><a href=link_comment.php?pid=$pid&title='$row[0]'>$row[0]</a></h2></i>";
echo $row[1];
echo "<br>";
echo "<br>";
}
the below code is for getting the values from the above php script:
$pid=$_REQUEST['pid'];
$title=$_REQUEST['row[0]'];
Change this
echo "<i><h2><a href=link_comment.php?pid=$pid&title='$row[0]'>$row[0]</a></h2></i>";
to this
echo "<i><h2><a href='link_comment.php?pid={$pid}&title={$row[0]}'>{$row[0]}</a></h2></i>";
When you build the url, you are passing parameters named pid and title.
Change
$title=$_REQUEST['row[0]'];
to be
$title=$_REQUEST['title'];
Instead of the ' you are using in title='$row[0]', use {}, title={$row[0]}:
echo "<i><h2><a href=link_comment.php?pid={$pid}&title={$row[0]}>{$row[0]}</a></h2></i>";
And on the next page, $_REQUEST['title'];
Try this:
echo "<i><h2>".$row[0]."</h2></i>";
Then on the link_comment.php script:
$pid=$_GET['pid'];
$title=$_GET['title'];

HREF to call a PHP function and pass a variable?

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

How to use trim() function in CakePHP 2 fotm when adding data?

How to use trim() function in CakePHP 2 form when adding data?
<?php
echo $this->Form->input('service_id');
echo $this->Form->input('name');
echo $this->Form->input('unit');
echo $this->Form->input('price');
echo $this->Form->input('date');
?>
In your controller, before you save() the data:
$this->data['YourModelName'] = array_map('trim', $this->data['YourModelName']);
You can also do it for each field individually instead, if you wish.

Variable inside a php echo function

I have a php function which displays a rating bar with the arguments. I have a variable called itemID inside my php page which holds the unique item number. I need to send this value to my function and also echo command must stay. Is there a way to achieve this?
Here is the code, which does not work. When I try it on the server, it does not show the id of item, it prints the variable name as it is.
<?php echo rating_bar('$as',5) ?>
What I get at html file:
<div id="unit_long$as">
instead of the item id in place of $as.
Single Quotes do not support variable replace,
$as = "test";
echo '$as'; //$as in your end result
echo "$as"; // test in your end result
echo $as; // test in your end result
//For proper use
echo " ".$as." "; // test in your end result
Update for newer PHP versions you should now use Template Syntax
echo "{$as}"
If I get what you are saying, this is what you are asking.
<?php echo rating_bar($itemID,5); ?>
With the limited code you are providing, thats what looks like you are asking.

Codeigniter Calling Controller with data from a form issue

Trying to call the projects controller and the editproject function and pass an id number. Can anyone tell me why the second line doesn't work? When I echo the value in the first line, it does give me the correct integer as a string
<?php echo $list[0]->id; ?>
<?php echo form_open('projects/editproject/',$list[0]->id ) ;?>
The error I keep getting is "Missing argument 1 for Projects::editproject()" My editproject function is function editproject($id).
I did try:
<?php echo $list[0]->id; ?>
<?php $pdata = (int)$list[0]->id; ?>
<?php echo form_open('projects/editproject/',$pdata ) ;?>
Thinking the call to the controller needed a variable for the data. Same error message as above. THanks for any help.
Did you mean to do this instead ?
<?php echo form_open('projects/editproject/'.$list[0]->id ) ;?>
The second argument of form_open() accepts an associative array of attributes, hence, you're mistakenly passing in the id into the second argument when it needs to be concatenated with the url instead.

Categories