I would like to send a var from a php script to javascript. Here is what I have so far:
<script>
function showEditMenuItemForm(name) {
document.getElementById('editMenuItemForm').style.display = 'block';
document.editMenuItemForm.menuitem_edit.value == '+name+';
document.editMenuItemForm.menuitem_edit.focus();
document.getElementById('linkOpenAddMenuListForm').style.display = 'none';
document.getElementById('addNewMenuItemForm').style.display = 'none';
}
</script>
<?php
if($sygroup == '3') {
$valueRname = $extract['name'];
echo '
<p>
'.$extract['name'].'
<a onClick="return returnDeleteSure()" href="systemdeletemenuitem.php?id='.$idfinal.'">
<img src="img/deleteicon.png" class="icon12">
</a>
<img onClick="showEditMenuItemForm('.$valueRname.')" src="img/Edit File-32.png" class="icon12">
</p>
';
}
?>
I don't know why but the value isn't the php var (It is empty instead of 'Test' e.g). So the script doesn't work.
I hope you can help me!
Sorry for my bad English btw, I hope you understand the question :)
<?php
if($sygroup == '3') {
$valueRname = $extract['name'];
echo '
<p>
'.$extract['name'].'
<a onClick="return returnDeleteSure()" href="systemdeletemenuitem.php?id='.$idfinal.'">
<img src="img/deleteicon.png" class="icon12">
</a>
<img onClick="showEditMenuItemForm('.$valueRname.')" src="img/Edit File-32.png" class="icon12">
</p>
';
}
?>
change to this.if you pass the text to javascript function it must be between ''
<?php
if($sygroup == '3') {
$valueRname = $extract['name'];
echo '
<p>
'.$extract['name'].'
<a onClick="return returnDeleteSure()" href="systemdeletemenuitem.php?id='.$idfinal.'">
<img src="img/deleteicon.png" class="icon12">
</a>
<img onClick="showEditMenuItemForm(\''.$valueRname.'\')" src="img/Edit File-32.png" class="icon12">
</p>
';
}
?>
Assuming:
$sygroup == '3'
And:
$extract['name'] = 'Test';
And:
$idfinal = 'something';
You've still got a couple of problems here:
1) This part of the code right here:
<img onClick="showEditMenuItemForm('.$valueRname.')"
...isn't going to do what you want. This is going to print something like this:
<img onClick="showEditMenuItemForm(test)"
The code is going to treat 'test' here as a variable, which it is not, presumably it's a string you want to provide to the function, you are a little unclear on this. It would need to be wrapped in quotes to be a useable value inside your function later on.
2) I don't know what this is:
document.editMenuItemForm.menuitem_edit.value == '+name+';
But it's a literal string in this case, you probable want something more like this:
document.editMenuItemForm.menuitem_edit.value == "'" + name + "'";
...which would be proper syntax for concatenating your variable with a string and ensuring it has quotes around it. I am not really sure what else you are doing here.
Look at the HTML and JS you are generating. Don't just look at the PHP and then wonder why the JS doesn't work.
Look at the JavaScript console. It will give you useful error messages.
This:
"showEditMenuItemForm('.$valueRname.')"
Looks like it is going generate something like:
"showEditMenuItemForm(Bob)"
which is going to throw a reference error because you want a string literal and not a variable name there.
Use json_encode
to convert PHP variables to JavaScript literals.
Use htmlspecialchars
to convert JavaScript literals to something you can put safely in an HTML attribute value.
Avoid mashing strings together.
if($sygroup == '3') {
$valueRname = $extract['name'];
?>
<p>
<?php echo htmlspecialchars($extract['name']); ?>
<a onClick="return returnDeleteSure()"
href="systemdeletemenuitem.php?id=<?php echo htmlspecialchars(urlencode($idfinal)); ?>">
<img src="img/deleteicon.png" class="icon12">
</a>
<img onClick="showEditMenuItemForm(<?php htmlspecialchars(json_encode($valueRname)); ?>)"
src="img/Edit File-32.png" class="icon12">
</p>
<?php
}
?>