angularjs ng-click or ng-init? php variables - php

I'm new in angularjs and after looking in every site and forum without answer, somebody can help me to selve this?
i have this html with php code, and i want to pass php var into angularjs event.
<a ng-init="changeCat=<?php echo $id_cat, $category; ?>" id="cat_<?php echo $id_cat; ?>" class="cat-list" href="#"><?php echo $category->name; ?></a>
thanks in advance!

assuming you have a method changeCat() you can pass it php variables using something like ng-init="changeCat(<?php echo $id_cat; ?>)". I am assuming you want ng-int at this point but it should give you enough to get going with.
also try searching 'pass php to ng' or 'pass php variable to angular controller'... results should be similar.
EDIT:
As #Kevin B said you may want to change the way you are doing things but that said.
If you are to use more that one variabel then have a look at this:
Laravel/Angular: passing data within php page to angularjs

Related

Make ID attribute HTML5 <section> PHP dynamical

Im trying to get dynamic with php. And I wonder if this is possible. However i pass a $var to ID the section does not show up. Lets say I have this:
<?php
$var = "welcome";
$html = '<section id="'.$var.'">Im glad you are here</section>';
echo $html;
?>
So this is the basic thing and I cant find any other reference to this. Note: Im writing this from my phone so forgive me if there is a syntax error.
Thanks in advance and wish you a good day.
Well the first thing I see is there is a mismatch between your variable name and the variable you pass.
Variable decaration is as V
then you pass var

Multiple URL parameters?

I am using Dreamweaver and would like to add multiple parameters to my my link. Right now I have one parameter which works fine, but I would like to add multiple parameters to the same link, how do I do that?
The code to the link is as follows:
<a href="Patient.php?Patient=<?php echo
$row_Patienter['Patient']; ?>">Visa</a>
This does links to another page's Recordset.
My question is:
How do I add multiple parameters to the same link? How would it look like?
You can separate parameters by adding a & at the end of each parameter.
Something like:
http://www.domain.com?parameter1=value1&parameter2=value2
But in your case I would recommend to do it like this:
<?php
echo "<a href='patien.php?parameter1=".$value1."&parameter2=".$value2."'>Link</a>";
?>
You add extra parameters by starting with ? and adding & each time you start a new parameter. Remember in HTML to use &
Visa
<a href="bar.php?foo=<php echo $foo; ?>&bar=<?php echo $bar; ?>">
or whatever the values are. Better use echo, not sure what you wrote works

How to create a dynamic link?

I'm trying to create a link that takes the user to two different pages depending is the user logged or not. Problem is I'm still new to programming and this is quite big bite for beginner like me but its something I have to do. I created something like this so far but either way I suck at searching or there just isnt specific information for what I need
<?php if($userLogged){
echo '<a href="index.php" class="stylelink">';
}
else
{
echo '<a href="index1.php" class="stylelink">';
}
echo "Etusivu</a>";
?>
I'm also using Dreamweaver's login function that creates the MM_Username session and such, and Im not sure how to make the condition. userLogged is still an empty variable. Id appreciate any advice.
Thanks
-John
well, instead of using echo statements in the php tag you can write html and use php for outputting the value of the page like this
Etusivu
The $_SESSION['MM_Username'] works if you have included session_start(); at the beginning of the page and you can use the condition as above instead of $userLogged.

Pass php variable though open.window

I'm pretty new to javascript so please excuse this question if it seems pretty nooby. I am writing an online calendar to book engineer's jobs so we need to be able to look at the calendar whilst looking at and editing individual jobs. After much deliberation I decided the best way to do this is by opening the jobs in a new window using window.open. However, I would like to pass the job id via the url in order to pass it to the new window but for love nor money I can't work it out or find a solution. Here is my code so far
function open_win()
{
window.open('job_detail.php', '_blank','toolbar=0,location=no,menubar=0,height=400,width=400,left=200, top=300');
}
<a href="#" onclick="open_win()" ><? echo $row['name']; ?></a>
The $row['name'] is the value of the link and the value I want to pass will be held in $row['id'] or $job_id. I just can't work out where to put it so it passes on.
Any help will be greatly appreciated
As a parameter?
function open_win(id) {
window.open('job_detail.php?id=' + id, '_blank','toolbar=0,location=no,menubar=0,height=400,width=400,left=200, top=300');
}
<? echo $row['name']; ?>

Get php variable within javascript

I have run into an interesting problem. I am currently developing php page and need to access a php variable within the javascript onload.
$(document).ready(function() {
var temp = <?php $page_id ?>
}
is this valid? I know that this might seem weird and not be allowed but I am developing a page that has two popup windows. The windows are created using the same view template and there is no way to distinguish between each other. If I stored a hidden value on the page with information unique to the page like so
<input type="hidden" value="<?php $page_id ?> id="page_id" />
if there are two views open at the same time there is no way for me to get a unique page id like so
var temp = $("#page_id").val();
Because there are two views with the same input id that is not unique. Long story short, is it valid to reference a php variable in the javascript?
Long story short is it valid to
reference a php variable in the
javascript.
Short answer, yes you can...PHP is server-side language, you can use it where you want.
Note: I assume that you are doing this in a file with php extension.
Long story short is it valid to reference a php variable in the JavaScript?
You are not referencing a PHP variable in JavaScript. You are simply generating the JavaScript code dynamically through PHP, where the value of the PHP variable $page_id gets hardcoded into the JavaScript code.
If you generate your JavaScript code through PHP, and you use var temp = <?php echo $page_id ?> it will work, but I wouldn't consider it best practice for bigger projects. I prefer my JavaScript code to remain static.
Your first piece of code is valid as long as you are generating the javascript. The same wont work if you put your js code in a separate .js file. Generating dynamic js is not a good practice for several reasons, like js browser caching and reuse for example.
If you want to completely separate the js code of php, you can create a client-server communication where js will ask for a specific value from a php script through ajax and later play with it in js environment.
The only thing you need is some clarification.
As a matter of fact, you cannot pass a variable. You can pass only it's value.
Also, one cannot "pass" anything from PHP to javascript. Javascript being generated by PHP. It is like HTML. You just generate any code you want. And you can use any variables, of course, with this code generation.
Your second example will work too, but you need to echo the value of the PHP variable to the page so that JavaScript can read from it. Also use htmlspecialchars to make sure you don't end up with invalid html.
<input type="hidden" value="<?php echo htmlspecialchars($page_id, ENT_QUOTES) ?>" id="page_id" />
You will find your answer in this question.

Categories