My problem is I have a php variable $ba with a value of 147 retrieved from a DB the below snipets of my code does not work, no image is shown. If I add $ba=147; before the fill_div call it works perfectly. I am stumped as to why it dos'nt work when $ba populated from the DB.
In both cases a check of the source code for the page shows the call being filled correctly fill_div("147");
<script type='text/javascript'>
function fill_div(ba)
{
document.getElementById("ba").innerHTML="<img src='admin/images/image.gif'/>";
}
</script>
<script>
fill_div("<? echo stripslashes($ba); ?>");
</script
<div id="<? echo $ba ?>" style="border:1px solid; width:120px; height:40px"></div>
You're passing the string literal"ba" to getElementById when you need to pass the variable ba
document.getElementById(ba).innerHTML="<img src='admin/images/image.gif'/>";
Also the stripslashes might not be a good idea if $ba has quotes in it, if $ba has a " in it will cause an error in your JavaScript if it is not escaped.
try ..
document.getElementById(ba).innerHTML
document.getElementById("ba")
should be
document.getElementById(ba)
Try changing your document.getElementById to:
document.getElementById(ba)
If it has quotes (as in your example code) "ba" is treated as a string, not a variable.
Related
I'm trying to retrieve the "custom Phone" value from a form where the variables are being passed through a URL and then retrieved using the PHP Get method.
Here is the URL with the pass through variables:
http://domain.com/redirect.php?custom%20phone=555-555-5555&email=jk%40blackdiamondacademy%2ecom&from=jk%40blackdiamondacademy%2ecom&listname=fgxpressusa&meta_adtracking=&meta_message=1&meta_required=name%2cemail%2ccustom%20Phone&name=Jordan&submit=Submit&submit.x=121&submit.y=27
Here is the code on my landing page that has the Get PHP code:
<script type="text/javascript">
var sale = PostAffTracker.createAction('lead');
sale.setOrderID('<?php echo $_GET['meta_message']; ?>');
sale.setProductID('<?php echo $_GET['unit']; ?>');
sale.setData1('<?php echo $_GET['name']; ?>');
sale.setData2('<?php echo $_GET['email']; ?>');
sale.setData3('<?php echo $_GET['custom%20phone']; ?>');
PostAffTracker.register();
</script>
The code retrieves the "name", and "email" but not the "custom%20phone". I tried putting 'custom phone' as the get value as well as 'custom+phone' and still it doesn't retrieve it.
The issue seems to be the space in the value from my web form ("custom Phone") that is being passed through the URL as "custom%20phone".
Any help would be greatly appreciated!
I just found the answer to my own question through trial and error.
The PHP GET method translates %20 as an underscore so I put:
<?php echo $_GET['custom_phone']; ?>
Works perfect!
hi guys im trying to show and hide div according to mysql value but i couldnt do it can you help me what im doing wrong
here is my code thanks a lot for your ideas
var Value = <?php echo json_encode($valuek) ?>;
if (Value==1){
$('#show_hide').show();
}
else{
$('#show_hide').hide();
}
<?php
$valuek = $session->userinfo['vcc'];
?>
<div id="show_hide">
some code
</div>
<?php echo json_encode($valuek) ?>
will return a json string, instead try just using "echo"
<?php echo $valuek ?>
If all you are going for is a boolean value then there is simply no need for JSON.
Echo the value directly into the JavaScript. Remember to ensure you are passing a valid boolean value.
PHP code -
<?php
$showDiv = ($dbValue == 1? 'true' : 'false');
?>
JavaScript + PHP injection -
<script>
var value = '<?php echo $showDiv; ?>';
<script>
Don't forget to wrap the PHP injected value with quotes.
$valuek = $session->userinfo['vcc'];
I'm not sure if you have the code in this order in your php file, or just showed pieces of code in this order, but Should go BEFORE your js code. It has no value when js code is run.
To see what $valuek is, just echo it on top of the screen
<?php echo "<h1>$valuek</h1>" ?>.
Or just look at the source - at your js function, to see what is printed after 'var Value ='
That's the main thing really - to make sure that you getting what you expect from session.
And as been said, you don't need jason_encode, but you do need a semi-colon after echo command.
Also, I hope your jquery code is within $(document).ready function, not as is.
I've seen numerous posts on how to do this either saying to use rawurlencode in the php and decodeURIComponent in javascript or just use json_encode. Neither work for me. Hoping someone can see what I'm doing wrong:
I have an html button like this:
<button id="editbutton" onClick='edit(this, "<?php echo htmlentities($_SERVER['REQUEST_URI']); ?>", "<?php echo $result_cameras[$i]["camera_type"]; ?>", "<?php echo rawurlencode($result_cameras[$i]["camera_name"]); ?>")'>Edit</button>
I pass that into the javascript edit button:
var edit = function(t, to, cameratype, cameraname, cameraquality, camerahash, camerastatus, emailnotice, camerasensitivity, axisuser, axispass, axisip, axisport)
{
if (mydiv.find("form").length) {
mydiv.show();
} else {
// fields
var $myform = $("<form id='EditCameraForm' name='' method='post' action='" + to + "'></form>");
var $myfieldset = $("<fieldset><legend>Edit camera settings</legend></fieldset>");
var $myinput = $('<input/>').attr('type','hidden').attr('name','camera_type').val(cameratype);
var $mylabel = $("<label for='CameraName'>Camera name: </label>");
var $myinput2 = js('<input/>').attr('size','25').attr('name','camera_name').attr('id','CameraName').val(decodeURIComponent(cameraname));
$myform.append($myinput, $mylabel, $myinput2);
...
}
...
}
I've tried using rawurlencode/decodeURIComponent as above and when I hit the edit button if the camera name is called: a"a (just testing the quotes) I get a"a. Backslashes such as a\b just returns some weird characters back.
If I try:
<?php echo json_encode($result_cameras[$i]["camera_name"]); ?>
and don't put anything in the javascript code I get this error:
missing ) after argument list
edit(this, "/dashboard", "WEBCAM", ""a"a"", "0", "3dd10c49784e2207de1e1932958bfb...
Where it is pointing to the ""a"a"".
Any suggestions?
You are in effect outputting a javascript string literal, so using htmlentities is not the correct thing to do. What you need instead is addslashes:
<?php echo addslashes($_SERVER['REQUEST_URI']); ?>
Edit: It goes without saying that you also need to do this for the other two strings you are echoing.
The reason this is correct is that the escape sequences for Javascript string literals are compatible with what addslashes does, if you ignore the fact that addslashes also escapes the "null" character. However, there's no way that character will be part of your URL so there is a perfect match between what addslashes does and what Javascript expects from its string literals.
For completeness I should mention that an appropriate usage of htmlentities is to process text that is being sent as part of HTML content; even then, htmlspecialchars (which performs a small subset of the work of htmlentities) is almost always the better fit.
rawurlencode() is for converting a string to an URL argument
htmlentities() is for converting a string to an HTML content
If you'd like to convert a string to a Javascript string it should be :
function f_str_2js($x) {
return str_replace( array("\n","\r","\t","'",'"') , array('\n','\r','\t',"\\'",'\"') , $x)
}
the above answers are correct, but you should consider to add your data to a php array or object, than jsonencode and add it to a date-attribute date-camera='{... than do eventbinding by using jQuery .on(.
This way you can access the data via $(this).data('camera'). It will be easy to extend you app without adding more and more parameters to the onclick-function.
for more information see:
http://api.jquery.com/jQuery.data/
http://api.jquery.com/on/
I'm trying to pass a parameter from php into my javascript function inside html. Is this at all possible? This is what I've got so far, but it seems to crash once it hits the condition:
$str="<input type='submit' value='-' onclick='Call(".$row['field1'].");'/>";
I hope that I won't have to find a work around for this.
Thanks.
EDIT:
This is the function that I'm trying to call
function Call(stuff)
{
alert(stuff);
$.get('reports.php',
{'param':'section', 'text':stuff},
function(returned_data)
{
alert(returned_data);
});
//alert('end');
}
And this is the function that I'm populating a table with.
function PopTable()
{
alert('end');
document.getElementById('table').innerHTML = 'Loading...';
$.get('reports.php',
{'param':'getstuff'},
function(returned_data)
{
document.getElementById('table').innerHTML = returned_data; // Clear the select
});
alert('end');
}
This is the php that I'm sending back population the table:
$str.= '<tr>';
$str.='<td bgcolor="#ffffff">' . $row['stuff'] .'</td>';
$str.='<td><input type='submit' value='-' onclick="Call('$row['stuff']');"/></td>';
$str.='</tr>'; //accumulate table
I can't seem to get a return value for Call(), and the alert doesn't even pop up
Try:
$str='<input type="submit" value="-" onclick="Call(\''.$row['field1'].'\');"/>';
I would bet you need quotes around the value if it is a string value
For example if $row['field1'] = 'test'; then:
Your version: <input type='submit' value='-' onclick='Call(test);'/> which would fail because test is not a valid variable
My Version <input type="submit" value="-" onclick="Call('test');"/> which would work becase 'test' is a string
What you're trying to do is possible, whereas it is not possible to pass a parameter from JavaScript into a PHP function.
When you say it crashes once it hits the condition, do you mean when you click on the input on the page? In that case, it's an error in your JavaScript syntax. I would try using Firebug with Firefox to track down the issue.
My first guess is there are no quotation marks inside the Call() method. So you're doing this:
Call(something)
and it should be like this:
Call('something')
This is possible, but I would be very careful about mixing PHP echos and javascript inline with strings because you need to escape javascript datatypes properly
In your example, $row['field1'] is probably from a database, so it's a string, so you need to surround the value with quotes in your javascript call. But that's not all, because what if there's a quote in your string, or a special character like a newline which needs to be escaped in javascript? And then what about html escaping?
A better approach is to import your javascript values in one place using json_encode(), then use your variables from there.
E.g.:
<?php
$jsonrow = json_encode($row);
?>
<script type="text/javascript">
var jsrow = <?=htmlspecialchars($jsonrow);?>;
</script>
<?php // some time later... ?>
<input type="submit" value="-" onclick="Call(jsrow.field1);" />
I tried this in my php site <a href=?command=value>click here</a>
And this to get the value :
if(isset($_POST['command'])){
$command=strip_tags($_GET['command']);
Does not seem to work.. Ps. The url have one this value also: ?lang=en_US
Use $_GET instead of $_POST. And also put double quotes around the value of href attribute.
Try this....this will work...
'click here'
this is to get the value from site:
<?php
if(isset($_REQUEST['command'])){
$command=($_GET['command']);
echo $command;
}
?>
the output: value
Note:Both the codes(php and html) are written in same page(company.php)
we can also write the php code in seperate page. Difference is,the 'isset' function is not needed there.And URL should change.
code will be the following.
'click here'
next_php_page.php code:
<?php
if($_REQUEST['command']){
$command=($_GET['command']);
echo $command;
}
?>