Undefined variable with string [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Inside profuser variable i have string "nick".
echo a href="#" onclick="prof('.$profuser.')">
#UP I DELETED THE SIGN BEFORE a BECOUSE IT WAS DESTROYING WHOLE CODE IN STACK OVERFLOW
It should be sent here:
<script>
function prof(profuser){
var xmlhttp=new
window.XMLHttpRequest(); xmlhttp.open("GET", "user.php?user=" +
profuser, true); xmlhttp.send(); }
</script>
And then to this file called user.php:
$thisuser = $_GET['user']; echo $thisuser;
But this code shows me that string "nick" is not defined.
Could anyone tell me what is wrong about it please?
And if there are more errors in this code tell me please.

As error say nick is treated as undefined variable. It should be seen as string so you need to add quotes:
onclick="prof(\'' . addslashes($profuser) . '\')"
According to #p.s.w.g, you should use addslashes() to escape quotes from php variable in case there were any;)

try this.
onclick="prof('<?php echo $profuser; ?>')"
or
onclick="prof('<?= $profuser; ?>')"
I think you are mixing PHP and javascript? Hard to tell from the limited code that was posted.
Your issue is most likely a quoting one, if you are printing the whole onclick bit try this
echo 'onclick="prof(\''.$profuser.'\')"';
This is because you are probably getting something like this in your source.
onclick="prof(nick)";
And nick is a string not a javascript variable, which will be undefined.

Related

Problem adding Js variables in HTML with getElementById [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
<body>
<h1>Operadores aritméticos</h1>
<h3>+ Adición (102+103)=</h3><p id="suma1"></p>
<h3>- Substracción</h3><p id="resta1"></p>
<h3>* Multiplicación</h3><p id="multi1"></p>
<h3>/ División</h3><p id="div1"></p>
<h3>% Módulo</h3><p id="mod1"></p>
<script>
var suma = (102+103);
var resta = (36-20);
var multiplicación = (27*30);
var división = (900/30);
var módulo = (106%3);
document.getElementById("suma1").innerHTML = suma;
document.getElementById("resta1")innerHTML = resta;
document.getElementById("multi1")innerHTML = multiplicación;
document.getElementById("div1")innerHTML = división;
document.getElementById("mod1")innerHTML = módulo;
</script>
Hello guys, I have a problem, im pretty new at this (programming with HTML, Js, etc.).The issue is that when I try to make my Js variables appear on HTML (with document.getElementById), they do not appear. Nevertheless, if erase every document.getElementById except the one containing "suma1", the browser displays me the result of the sum (205), but if I add even one of them, the browser doesn´t display anything.
I hope I was clear with my problem, it seems very simple but hard to explain.
Any suggestions?
Thanks in advance
The reason you're not seeing anything when you add the statements to populate resta1, multi1, div1, and mod1 is probably because they all have a syntax error. This is likely causing even the first statement (suma1, which is syntactically valid) not to work.
Valid Statement
The 1 statement that is working is document.getElementById("suma1").innerHTML = suma;
Invalid statements
All the other statements follow this pattern:
document.getElementById("id")innerHtml = variable;
Note that you're missing the . between getElementById("id") and innerHtml. If you add the missing . then it should all work as expected.

Pass jquery variable to php [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have a question I tried to pass a jquery variable to php but when I look in console I got an error:
SyntaxError: missing ) after argument list):.append('Edit') and I don't understand where is the problem
var items=[];
var link = "<?php echo base_url()?>firm/editFirm/";
$.each(obj, function(i,val)
{
$('#finalResult').text("Results");
items.push($('<li/>').text
(
val.name_firm + "---" +
val.idno+"---" +
val.adresa+ "---" +
val.cont_banca+ "---" +
val.swit+ "---" +
val.banc_name+"---"
).append("<a href='"link+val.idno"'>Edit</a>")
);
});
Help me please.
You've missed the concatenation in the last bit:
append("<a href='"link+val.idno"'>Edit</a>")
Should be
append("<a href='" + link + val.idno + "'>Edit</a>")
I hope you aren't calling the php inside a .js file. I find it's cleaner to retrieve server-side variables using data-attributes inside DOM elements.
In the case of storing URLs for your use in Javascript, I'm really fond of the localize_script function of Wordpress, you just render the variables in a tag in the header or footer via PHP and you have them all available in a nicely separated place.

Will a ternary operator work inside .text()? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I want to append this to my DOM for every 'answer_' in my DB.
.append('<span>')
.text(format_date(answer_.LastModifiedDate))
But .LastModifiedDate won't always exist. Can I check for .LastModifiedDate in the text field? Maybe like this?
.append('<span>')
.text((answer_.LastModifiedDate) ? format_date(answer_.LastModifiedDate) : '')
Which doesn't work...
EDIT
I was stupidly checking for answer_.LastModifiedDate, instead of just answer.
So the following line works. Thanks for all the responses!
.append('<span>')
.text((answer_) ? format_date(answer_.LastModifiedDate) : '')
Of course that works. Ternary operators work anywhere you could normally place a variable. They evaluate to a value, just as if you used a string literal.
As #FreeAsInBeer pointed out, ternary works everywhere.
The only problem with your code is that you can't just use a (maybe) non-existant value as a boolean to check whether it is defined or not; How would you check if a variable holding "false" exists?
Instead you need to check the variables type:
.text(typeof answer_.LastModifiedDate !== 'undefined' ? format_date(answer_.LastModifiedDate) : '')
I was stupidly checking for answer_.LastModifiedDate, instead of just answer. So the following line works. Thanks for all the responses!
.append('<span>')
.text((answer_) ? format_date(answer_.LastModifiedDate) : '')

accessing a PHP variable in javascript [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
In my controller, I'm passing the image which is an array in this way:
$this->load->model('site_model');
$data['images'] = $this->site_model->fetch_images();
$data['main_content'] = 'my_view';
$this->load->view('includes/template', $data);
Everything up to know is perfect and even I can have the following in my view which shows the path of the first element:
echo $images[0]['path'];
But in view in script I'm trying to do the following which gives me Uncaught SyntaxError: Unexpected token ILLEGAL"
alert('<?PHP echo $images[0]['path']; ?>');
Why is that? Is not that possible?
Thanks
The path you're outputting contains backslashes, which in JavaScript strings begin an "escape sequence" -- basically a typable representation of a character that otherwise you'd have trouble putting into a string. But when they appear naked in a string, JS often chokes on them.
Rather than echoing the path directly, try this:
alert(<?= json_encode($images[0]['path']) ?>);
That will make PHP format the text in a way that'll cause fewer problems in JS.
(In some versions and configurations of PHP, <?= might not work. You can use <?php echo in its place; it does the same thing. It's just wordier. :P )
Try this
alert("<?php echo $images[0]['path'] ?>");
or try putting you php variable into a Javascript variable
to do so simply:
var data = "<?php echo $images[0]['path'] ?>";
alert(data);
cheers!
Try to JavaScript alert this way to store into $path variable,
$path = echo $images[0]['path'];
Now you print this $path into alert box
echo '<script type="text/javascript"> alert("' . $path . '");</script>';

How to add property to a global Javascript object inside a function [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I am tying to add a property to a JS object inside a function. I can do it outside but not inside. Please explain. Sorry. I am missing something very basic here.
var newobj = {'prop1' : 12, 'prop2' : 25};
myfunc(newobj);
function myfunc(someobj) {
someobj.prop3 = 45;
}
This gives a syntax error.
Chances are something else is interfering because it works for me.
If you dump newobj before the function call you get:
{"prop1":12,"prop2":25}
And after the function call:
{"prop1":12,"prop2":25,"prop3":45}
As you can see, the new property has been added.
I would suggest either looking at what you have more closesly (make sure you're not copying the value and then passing it) or add some console.log call in your code as it goes through. You can also, in most of the browsers, use the debugger to step through the code to see where it may be fouled.

Categories

Resources