I want to get php booelan value with javascript.
That is my code.
<script type="text/javascript">
var $j = jQuery.noConflict();
var isValue = $j(<?php isset($getResult[]); ?>);
alert(isValue);
</script>
php is a server side programming, and javascript is client side. so you need to "echo" the php value to get result on javascript
var isValue = $j(<?php echo isset($getResult[]) ? "true" : "false"; ?>);
You'll have to convert the boolean result of isset() into a string representation of 'true' or 'false' so that it can be understood by the javascript parser. Like this:
var isValue = $j(<?php isset($getResult[]) ? echo 'true' : echo 'false'; ?>);
It should be like this
<script type="text/javascript">
var $j = jQuery.noConflict();
var isValue = $j(<?php if (isset($getResult)){ echo true;} else {echo false;} ?>);
alert(isValue);
</script>
You cannot use $getResult[] for reading using issest. It will return fatal error or you have to specify some index in the array.
If you want to check if it is an array then you can use :
<script type="text/javascript">
var $j = jQuery.noConflict();
var isValue = $j(<?php if (isset($getResult) && is_array($getResult)){ echo true;} else {echo false;} ?>);
alert(isValue);
</script>
Related
I am trying to assign a php echo value to an input generated from a jquery function. But so far no luck. It breaks the function and no results are displayed along with the input field. What is the proper way for this scenario to display php value inside the query function.
PHP
$tablename = "table";
$next_increment = 0;
//$qShowStatus = "SHOW TABLE STATUS LIKE '$tablename'";
$qShowStatusResult = $db_con->prepare("SHOW TABLE STATUS LIKE '$tablename'");
$qShowStatusResult->execute();
$results = $qShowStatusResult->fetchAll(\PDO::FETCH_ASSOC);
foreach($results as $value){
$next_increment = $value['Auto_increment'];
}
var nextAutoIncrement = '"'<?php echo $next_increment; ?>'"';
Jquery
newSection.children(':nth-child(1)').children(':first').attr('id', 'auto_id_' + newNum).attr('name', 'auto_id_' + newNum).val(nextAutoIncrement).hide();
Try this
<script language="javascript" type="text/javascript">
var nextAutoIncrement = '<?php echo $next_increment;?>';
</script>
Try like this:
<script language="javascript" type="text/javascript"
var nextAutoIncrement = <?php echo $next_increment; ?>;
</script>
<script>
//if it is anumber
var nextAutoIncrement = <?php echo $next_increment; ?>;
// if ity is a string
var nextAutoIncrement = '<?php echo $next_increment;?>';
</script>
In JS code there is require to define the <script> tag:
$tablename = "table";
$next_increment = 0;
$qShowStatusResult = $db_con->prepare("SHOW TABLE STATUS LIKE '$tablename'");
$qShowStatusResult->execute();
$results = $qShowStatusResult->fetchAll(\PDO::FETCH_ASSOC);
foreach($results as $value){
$next_increment = $value['Auto_increment'];
}
<script type="text/javascript" >
var nextAutoIncrement = '<?php echo $next_increment; ?>';
</script>
I created a form with a text field that has Spry Validation (ie javascript). The user can select the number of rows in the form from 1 to 10. I need the code below to also expand but I'm not familiar enough with javascript to make it work.
$divkey is the variable that controls how many rows are in the form.
Original
<script type="text/javascript">
var sprytextfield1 = new Spry.Widget.ValidationTextField("sprytextfield1", "none", {validateOn:["change"], maxChars:20});
var sprytooltip1 = new Spry.Widget.Tooltip("sprytooltip1", "#sprytrigger1");
</script>
so I need the line 'var sprytextfield1...' to repeat based on $divkey with the next line being 'var sprytextfield2...' and so on. Can someone please rewrite this so it will work?
Trying to use php
<script type="text/javascript">
<?php for ($i = 0; $i < $divkey; $i++) { $num=$i+1; ?>
var sprytextfield<?php echo $num;?> = new Spry.Widget.ValidationTextField("sprytextfield<?php echo $num;?>", "none", {validateOn:["change"], maxChars:20});
<?php }?>
var sprytooltip1 = new Spry.Widget.Tooltip("sprytooltip1", "#sprytrigger1");
</script>
Trying to use javascript
<script type="text/javascript">
var numwrestler = <?php echo $wrestlerkey; ?>;
var sprytextfield = [];
for (var i = 0; i < numwrestler; i++) {
var num = i+1;
var sprytextfield[num] = new Spry.Widget.ValidationTextField("sprytextfield"+num, "none", {validateOn:["change"], maxChars:20});
}
var sprytooltip1 = new Spry.Widget.Tooltip("sprytooltip1", "#sprytrigger1");
</script>
I'd recommend that you use a Javascript array for this type of task.
Your code is mostly correct, but the var in your for loop is incorrect, and the creation of the num variable instead of just using i is redundant.
<script type="text/javascript">
var sprytextfield = new Array();
var numwrestler = <?php echo $wrestlerkey; ?>;
for(var i = 0; i < numwrestler; i++){
sprytextfield[i] = new Spry.Widget.ValidationTextField("sprytextfield"+(i+1), "none", {validateOn:["change"], maxChars:20});
}
var sprytooltip1 = new Spry.Widget.Tooltip("sprytooltip1", "#sprytrigger1");
</script>
Be sure that your PHP variable(s) are defined in the file before you include them in your script.
In your PHP code, you never define the variable divkey, by deafault the value will be 0.
Try:
<script type="text/javascript">
<?php $divkey = 10; for ($i = 0; $i < $divkey; $i++) { $num=$i+1; ?>
var sprytextfield<?php echo $num;?> = new Spry.Widget.ValidationTextField("sprytextfield<?php echo $num;?>", "none", {validateOn:["change"], maxChars:20});
<?php }?>
var sprytooltip1 = new Spry.Widget.Tooltip("sprytooltip1", "#sprytrigger1");
</script>
Please, note that the variable that you are using as index $num in every iteration of the loop will be increasing 2, because of the i++ and the $num=$i+1
what is the best way to check a parameter is set in PHP within jQuery template?
I have this code:
var custID = '<?php echo htmlentities( $_GET['custID'] ); ?>';
I need to check that it is set, if it is set then echo that value, if it is not produce an error.
i'd say:
<?php
if(isset($_GET['custID']))echo "var custID = '".$_GET['custID']."';";
else echo "alert('error');";
?>
try this
var custID = <?php echo htmlentities( isset($_GET['custID']) ? $_GET['custID'] : "" ); ?>;
I'll do it like that, first we need to check if the custID exists :
<?php
$custId = isset($_GET['custID'] && !empty($_GET['custId']) ? htmlentities($_GET['custId']) : false;
if(!$custId) {
//cutsId don't exists, show your error
?>
Error
<script type="text/javascript">var custId = false;</script>
<?php } else { ?>
<script type="text/javascript">var custId = '<?php echo $custId; ?>';</script>
<?php } ?>
There you're sure that the javascript var will not throw an error.
There's another way within the script :
<script type="text/javascript">
var custId = '<?php echo isset($_GET['custId']) ? htmlentities($_GET['custId']) : false;?>';
</script>
Note that i'm keeping the quotes or they might be an error because var custId = ; is not valid but var custId = ''; is.
If custId doesn't exists, the Javascript var will be empty (not undefined) and you could check it like that :
if(custId.length)
//custId exists
<?php
$abc=array();
$abc = (abc, cde,fre);
?>
<script language="javascript" type="text/javascript">
for (var i = 0; i < 3; i++) {
var gdf = "<?php echo $lat['i'];?>";
alert("value ="+gdf);
}
</script>
Following your comment, I think this is what you are trying to do:
<?php
$abc = array('abc', 'cde', 'fre');
?>
<script type="text/javascript">
var gdf = '<?php
for ($i = 0; $i < count($abc); $i++) {
echo "{$abc[$i]}";
if ($i != (count($abc)-1)) echo ", ";
}
?>';
</script>
Will output:
http://codepad.org/KjEH5CmN
<script type="text/javascript">
var gdf = 'abc, cde, fre';
</script>
NOTE
Using implode if you want a single variable would also work well:
http://codepad.org/UwukCY4m
<?php
$abc = array('abc', 'cde', 'fre');
?>
<script type="text/javascript">
var gdf = '<?php echo implode(', ',$abc); ?>';
</script>
You're not looking to assign a single value of the array; you're looking for the whole array. Your JavaScript loop is trying to iterate over the entire $abc array from PHP.
Something like this would work:
var abc = <?php echo json_encode($abc); ?>;
for(var i = 0; i < 3; i++)
var gdf = abc[i];
alert("value = " + gdf);
}
Firstly, to build a PHP array you should be using this notation:
<?php
$abc = array('abc', 'cde', 'fre');
?>
Next, it's not possible use JavaScript to directly loop through your variable that is stored in PHP. You can do something like this instead, performing the loop in PHP:
<?php
$abc=array('abc', 'cde', 'fre');
?>
<script language="javascript" type="text/javascript">
<?php foreach ( $abc as $el ): ?>
alert('value=<?php echo $el ?>');
<?php endforeach ?>
</script>
Or, if you'd really like the loop to happen in JavaScript and not PHP, you can "export" the PHP array to JavaScript by converting the array to a JSON string and outputting it.
<?php
$abc=array('abc', 'cde', 'fre');
?>
<script language="javascript" type="text/javascript">
var abc = <?php echo json_encode($abc) ?>;
for ( var i = 0; i < abc.length; i++ ) {
alert('value=' + abc[i]);
}
</script>
I have a PHP file that is going to write a two-dimensional array in JavaScript:
<?php
print "<script language='javascript'>";
print " extra[0][0] = new Array(1,'Bob',12);";
print " extra[0][1] = new Array(2,'Alice',18);";
..
// Need to assign the extra[1][0], extra[1][1] and so on.
print "</script>";
?>
Mu.js:
var extra = new Array();
...
How do I assign the two-dimensional array from PHP to a JavaScript variable?
json_encode is your friend: json_encode in the PHP manual
<script type="text/javascript">
var jsArray = <?= json_encode($my_array) ?>;
</script>
Yes, wvanbergen is right, json_encode is your friend. You can create the array as JSON:
<?php
$extra = array(
array(1,'Bob',12),
array(2,'Alice',18)
);
echo "var extra = " . json_encode($extra) . ";";
?>
And in your javascript it will output:
var extra = [[1,"Bob",12],[2,"Alice",18]];
<script type="text/javascript">
var jsArray = <?php json_encode($my_array); ?>;
</script>