PHP variable in a HTML string created from Javascript - php

I'm trying to use some simple AJAX and then passing my variable through a HTML string.
Struggling to get some PHP included to print the value into the string. Been trying like this and the variable prints blank even though there is a value assigned to $user_id.
xmlhttp.open("GET","update.php?user=<?php $user_id ?>&q="+str,true);
Any suggestions on how I could do this or better solutions.
Thanks

you forgot echo:
xmlhttp.open("GET","update.php?user=<?php echo $user_id; ?>&q="+str,true)

Related

What is the correct syntax in php in passing variable(s) in a HTML link to another form

This is what I am trying to do. I am trying pass variable(s) from one form to another. I can do it in "HTML" I would like to have done in a PHP echo statement
When I call the second program from the first program I have nothing coming back in the "GET" array. I think ($v_resource_id, $v_category_id) are not being translated correctly What I am doing wrong in my syntax ? What you see here is part of the first program or calling program
echo'';
Aside from the noted html error (missing quote), you are not retrieving the correct keys, you have var and var2 but are trying to retrieve v_resource_id. You need to retrieve $_GET['var'] and $_GET['var2'].
Do print_r($_GET); to see what is being sent via $_GET. You should get:
Array (
[var] => whatever
[var2] => something else
)
Also, you may want to use the native query building function by doing http_build_query(array('var'=>'something','var2'=>'what ever')) to get a query string. Makes this easier and I think it takes care of urlencode(), you'd have to double check that.
Try this, it's about proper quoting
echo "here";
you'll use your values as $_GET['var'] and $_GET['var2']

How to store a echo result into a variable in php?

I am a new bee to this. I am trying to store the result of a random value generated into a variable which then I will be storing in MySQL DB.
Tried a code like below
$des_nu1 = echo rand(100,9999);
but looks like the statement isn't correct. Please advise.
I did see an option to use session but is there any alternative than using session ( Ref using Session : How to store a variable in php using session)
Remove the echo
$des_nu1 = rand(100,9999);
The only thing echo does is this Output one or more strings. Since you don't want to output the result of your function you don't need echo. After you assign that value to your variable you can use it anyway you want and you can also use it for echo
echo $des_nu1;
$temp = $des_nu1 + 10; //etc
Always refer to the manual,
echo is language construct echo manual that returns nothing, it will write in standard output (stdout)
I advise you to always use sprintf()
sprintf("%i", rand(100,9999));

getting the count value in PHP using value command

I am making a dashboard using php and displaying it on webpage using html. For this I am taking some count values. Is !$value[" variable"] valid in php? How do i get the not of (!=) value? There is no other way to get the data. I am getting the values from a database.
print "<td><a href=\"$strBase and status!='Closed'\" target=\"_blank\">".$value[" ? "]."</td>";
I need to print !closed. Somehow I do not know how to pass that inside $value[].
If you want to use a variable as a key in order to retrieve a value from an array you can do as follows (I use a snippet of your code):
.$value[$myVar].
Where $myVar should be a string with the name of the key you want to retreive from $value
But anyhow, your question is pretty unclear so I'm not sure if this is what you are looking for.
You can check if the variable is empty(), i.e.:
if(empty($value["variable"])){
echo "variable is empty";
}

How to use variable to get the for loop's value in PHP?

How to use a varible to store the value of the for loop?
for($i=1;$i<=$var;$i++)
How to get $i's value and store into another value and show its result?
I am a beginner of PHP and I want to improve my concept, I am very grateful if anyone can helps, Cheers!
Are you want dynamic variable or just a normal variable which will give you the out put like 12345678910
<?php
$var=10;
$a='';
for($i=1;$i<=$var;$i++){
$a.=$i;
}
echo $a;
?>
output: http://3v4l.org/2p8L6
or if you want dynamic variable then use following code to make a variable dynamic
${"num_" . $i} = $i;
output will be
$num_1=1;
$num_2=2;
$num_3=3;
etc
In plain English; PHP is updating the value stored in lopping variable automatically, and this is the whole idea behind using for loops, not only in PHP, but also in other programming languages.
Usually, you would want to use it (the variable $i) as a kind of a counter, and run code in the loop based on the iteration count.

passing variables in query string

$courses_taken=$row['course_id'];
As you can see from above code, i'm taking a variable and passing it in query string, but this method is wrong.
How can i pass a variable in query string, as each user will have opted for different courses, thus they will have different course_id.
Is there a way to do it?
<?php
$courses_taken=$row['course_id'];
?>
You need an echo statement, and then interpolate the variable inside the string.
$courses_taken = $row['course_id'];
echo "<a href='course.php?course_id=$courses_taken'>click here</a>";
Try this:
You are not using php tag to pass php variable in your anchor tag.
Do it like this:
-
Thanks

Categories