How to call js function from php? - javascript

I have a php page, a html button on it and there I should call a JS function with a php variable. And I get the error
the variable is not defined
Here is the code:
<body>
<form class="form">
<?php
if(file_exists('megjelenitendo.txt')){
$mappak=array();
$mappakdb=0;
$megjelenitendo = fopen("megjelenitendo.txt", "r") or die("Unable to open file!");
while(!feof($megjelenitendo)) {
$mappak[$mappakdb]=fgets($megjelenitendo) ;
$mappakdb++;
}
fclose($megjelenitendo);
$j=0;
foreach(glob('*') as $filename){
for($i=0; $i<$mappakdb;++$i){
//echo $filename."==".$mappak[$i];echo "<br>";
if(strtoupper($filename)==strtoupper(trim($mappak[$i]))){
//echo '<button type="submit" id='.$i.' class="button" formaction='.$filename.' />'.$filename;//substr($filename, 3,strlen($filename));
echo '<button type="button" id='.$i.' class="button" OnClick=mappanyitas('.trim($mappak[$i]).')>'.trim($filename).'</button>';
echo '<br>';
}
else{}
}
}}
else{
echo "A mappa elemei:<br>";
}
?>
</form>
<script type="text/javascript">
function mappanyitas(filename){
alert(filename);
}
</script>
</body>

You can call javascript function like this :
<?php
echo "<script>functionName();</script>";
?>
But function should be defined already.

The way to get PHP variables to javascript that I tend to use is just set the variable :)
<script type="text/javascript">
var fromPHP = '<?= $phpVariable; ?>';
</script>
Ti's as simple as that.

echo '<button type="button" id='.$i.' class="button" OnClick=mappanyitas("'.trim($mappak[$i]).'")>'.trim($filename).'</button>';

try this line
echo '<button type="button" id='.$i.' class="button" onclick="mappanyitas('.trim($mappak[$i]).')">'.trim($filename).'</button>'
Let me know if this solved your problem!
P.S.
Check the rendered page's HTML and check the button's code, to check if the variable's value was successfully injected into the code

Related

On Click not working with JS

The code shown here is meant to take me to the next part of checkout process however I encounter the error that billing is not defined? I've tried to put a function and call that function via the onclick but this has no effect.
The code that isn't working shall be the bottom section of the code which is the JS along with the button just above
Below is the where you can see the button and the onclick callout
<div class="buttons-set form-buttons btn-only" id="billing-buttons-container">
<button type="button" class="btn" onclick="billing.save()"><span><span><?php echo $this->__('Continue') ?></span></span></button>
<span id="billing-please-wait" class="please-wait" style="display:none;">
<img src="<?php echo $this->getSkinUrl('images/opc-ajax-loader.gif') ?>" alt="" class="v-middle" /> <?php echo $this->__('Loading next step...') ?>
</span>
</div>
<p class="required"><?php echo $this->__('* Required Fields') ?></p>
<?php echo $this->getBlockHtml('formkey') ?>
</form>
The JS below is not working correctly
<script type="text/javascript">
//<![CDATA[
var billing = new Billing('co-billing-form', '<?php echo $this->getUrl('checkout/onepage/getAddress') ?>address/', '<?php echo $this->getUrl('checkout/onepage/saveBilling') ?>');
var billingForm = new VarienForm('co-billing-form');
//billingForm.setElementsRelation('billing:country_id', 'billing:region', '<?php echo $this->getUrl('directory/json/childRegion') ?>', '<?php echo $this->__('Select State/Province...') ?>');
$('billing-address-select') && billing.newAddress(!$('billing-address-select').value);
var billingRegionUpdater = new RegionUpdater('billing:country_id', 'billing:region', 'billing:region_id', <?php echo $this->helper('directory')->getRegionJson() ?>, undefined, 'billing:postcode');
if ($('onepage-guest-register-button')) {
Event.observe($('onepage-guest-register-button'), 'click', function(event) {
var billingRememberMe = $('co-billing-form').select('#remember-me-box');
if (billingRememberMe.length > 0) {
if ($('login:guest') && $('login:guest').checked) {
billingRememberMe[0].hide();
} else if ($('login:register') && ($('login:register').checked || $('login:register').type == 'hidden')) {
billingRememberMe[0].show();
}
}
});
}
//]]>
</script>
jQuery was blocking another jQuery file and therefore it wasn't working correctly as intended, but since removing one of jQuery files it now works :D

How to create insert and print using one submit button

How can I create a submit button that allow multiple action? In this case I would like the button to add new data and print the new data using one single submit button.
However, after added onclick="printContent('div1')" on my submit button, the INSERT query does not work. Thus, it will print old latest info because no new data was added.
index.php:
<html>
<?php
// Turn off all error reporting
error_reporting(0);
include 'dbconnect.php';
?>
<head>
<script>
// script to print partial information only
function printContent(el){
var restorepage = document.body.innerHTML;
var printcontent = document.getElementById(el).innerHTML;
document.body.innerHTML = printcontent;
window.print();
document.body.innerHTML = restorepage;
}
</script>
</head>
<body>
<form method="POST" action="">
<button name="submit" type="submit" id="printStuff" class="btn v-btn no-three-d" onclick="printContent('div1')">PRINT!</button>
</form>
<?php
if(isset($_POST["submit"])){
$sql2 = "INSERT INTO Q_postitem (service_ID, created_time, queue_No)
SELECT 1, CONVERT_TZ(CURRENT_TIMESTAMP,'+00:00','+8:00'), max(queue_No)+1 FROM Q_postitem WHERE service_ID=1";
if (mysqli_query($con, $sql2)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql2 . "" . mysqli_error($con);
}
echo "<br>".$sql2;
mysqli_close($con);
}
?>
<div id="div1" style="display: none;">
//ECHO NEW ADDED DATA HERE TO PRINT
</div>
</body>
</html>

I want to hide the submit button based on a PHPcondition through javascript

<?php
if(mysql_num_rows($result5)>0)
{
echo 'condition matched';
// the above echo is just for testing
?>
<script type='text/javascript'>
document.getElementById("submitbtn").style.display = "none";
</script>
<?php
}
?>
here I am evaluating an if clause, if the condition is true, I want to hide the submit button of the form.
<form action='xyz.php'>
<input type='text'>
<input type='text'>
<input type='text'>
<input type='submit' id='submitbtn' name='save' value='SAVE' class='form_btn' >
</form>
The condition is evaluating to true, but the submit button is still visible. How do I achieve this??
try using this way....
<input type='submit' <?php if($result5->num_rows>0) {?> disabled="disabled" <?php } ?> id='submitbtn' name='save' value='SAVE' class='form_btn' >
may be it will help
I had tied your code,and these worked ok, so I think must have other problems to affected your js code, such as a same name Id of submitbtn, or a js error.
If you want to use js try this :
<?php
if(mysql_num_rows($result5)>0)
{
echo 'condition matched';
// the above echo is just for testing
?>
<script type='text/javascript'>
$(document).ready(function(){
document.getElementById("submitbtn").style.display = "none";
});
</script>
<?php
}
?>
You can achieve this by using php too.
<input type='submit' id='submitbtn' name='save' value='SAVE' class='form_btn' <?php if(mysql_num_rows($result5)>0) echo 'style="display:none"';?>>
Posting the answer because all others are just disabling the button. It will still be visible. So to hide the button, use:
window.onload = function(){
document.getElementById("<id of your submit button>").style.display = "none";
}
inside the if(condition=true)
OR
<input type='submit' name="sbmt" id="id_1"<?php if(condition=true) { ?> style="display: none" <?php } ?> />
but since your question has asked exceptionally for "javascript" i will go one step further and write
<input type='submit' name="sbmt" id="id_1"
<?php if(condition=true) { ?>
<script type='text.javascript'>
document.getElementById("id_1").style.display = "none";
</script>
<?php } ?>
/>
Try as below : jQuery must be there on your page.
On calling the statement on page ready will solve the problem.
<?php
if(mysql_num_rows($result5)>0)
{
echo 'condition matched';
// the above echo is just for testing
?>
<script type='text/javascript'>
$( document ).ready(function() {
document.getElementById("submitbtn").style.display = "none";
});
</script>
<?php
}
?>
I Think you added your php codes before that form loads!
So you need window.onload OR Replace your php codes to after form
<?php
if(mysql_num_rows($result5)>0)
{
echo 'condition matched';
?>
<script type='text/javascript'>
window.onload = function(){
document.getElementById("submitbtn").style.display = "none";
}
</script>
<?php
}
?>
<?php
if(mysql_num_rows($result5)>0)
{
echo'<div style='visibility:hidden' id="del">';
echo'<script>
$(document).ready(function(e) {
$("#del").css("visibility", "visible");
});
</script>';
}
?>
It is not none it's :
<script type='text/javascript'>
document.getElementById("submitbtn").style.visibility = "hidden";
</script>

how to reveal coupon code using php

i Want to reveal coupon code on button click
<script type='text/javascript' >
jQuery('#id1').click(function(){
jQuery(this).replaceWith("<?php echo $_coupon?>");
})
</script>
<?php if ($_coupon != '' ):?>
<button id="id1" type="button" class="but" value="Button Name"> </button>
<?php endif; ?>
This is my code it works only with first button click that means show only one value not works as a loop please help me to solve my problem
I would do it this way:
<script type='text/javascript' >
jQuery('#id1').click(function(){
jQuery(this).replaceWith(jQuery(this).val());
})
</script>
<?php if ($_coupon != '' ):?>
<button id="id1" type="button" class="but" value="<?php echo $_coupon ?>"></button>
<?php endif; ?>
I would add a hidden input like this:
<?php if ($_coupon != '' ):?>
<input type="hidden" id="coupon" value="<?php echo $_coupon ?>" />
<button id="id1" type="button" class="but" value="Button Name"></button>
<?php endif; ?>
Then in the jquery get the value from the object:
<script type='text/javascript' >
jQuery('#id1').click(function(){
jQuery(this).replaceWith($('#coupon').val());
})
</script>
<script type='text/javascript' >
jQuery('#id1').click(function(){
jQuery(this).replaceWith("<?php echo $_coupon; ?>");
});
</script>
<?php if ($_coupon != '' ):?>
<button id="id1" class="but" value="Button Name"></button>
<?php endif; ?>
It's working correctly.
Check the id of the button is already defined in various place of this page. because the id must be unique.
Also try this one,
<script type='text/javascript' >
$(document).ready(function(){
$('#id1').click(function(){
$(this).replaceWith("<?php echo $_coupon; ?>");
});
});
</script>
Also check jquery library included or not

Passing a variable (php) between html

I am trying pass a parameter in action tag in form, just the way I am doing in href tag but this isn't working can I know why? or should I just use a href tag in form, will that overwrite action in form?
Here is my code:
<!DOCTYPE html>
<html lang="en">
<?php
$ty=$_GET['param'];
$name=$_GET['param1'];
if($ty=='teacher')
{
$web = "<a href='teacherrepute.php?a=$name'>My repute score</a>";
$rep = "<a href='teacherreported.php?a=$name'>My reported sites</a>";
$blk = "<a href='newblocktryteacher.php?a=$name'>Block this site</a>";
$unblk = "<a href='newtryunblockteacher.php?a=$name>Unblock this site";
}
else
{
$web = "<a href='pupilrepute.php?a=$name'>My repute score</a>";
$rep = "<a href='pupilreported.php?a=$name'>My reported sites</a>";
$blk = "<a href='newblocktrypupil.php?a=$name'>Block this site</a>";
$unblk = "<a href='newtryunblockpupil.php?a=$name>Unblock this site";
}
// $type=$_GET['param2'];
$courseA='A';
$courseB='B';
?>
<body>
<?php echo $rep; ?>
<FORM action = <?php echo $blk; ?> method ="POST";>
Block : <input type ="text" name = "url" /></br>
<br>
<input type="submit" value="block" />
<br>
</FORM>
</body>
</html>
1) As you already creating link based on condition then you can directly echo that variable inside markup. e.g.
<?php echo $rep; ?>
2) Instead of passing html in form action just you pass that script name. e.g
newblocktryteacher.php?a=somename or newblocktrypupil.php?a=somename
Based on these two points your code will be
<!DOCTYPE html>
<html lang="en">
<?php
$ty=$_GET['param'];
$name=$_GET['param1'];
if($ty=='teacher')
{
$web = "<a href='teacherrepute.php?a=$name'>My repute score</a>";
$rep = "<a href='teacherreported.php?a=$name'>My reported sites</a>";
$blk = "newblocktryteacher.php?a=$name";
$unblk = "<a href='newtryunblockteacher.php?a=$name>Unblock this site";
}
else
{
$web = "<a href='pupilrepute.php?a=$name'>My repute score</a>";
$rep = "<a href='pupilreported.php?a=$name'>My reported sites</a>";
$blk = "newblocktrypupil.php?a=$name";
$unblk = "<a href='newtryunblockpupil.php?a=$name>Unblock this site</a>";
}
// $type=$_GET['param2'];
$courseA='A';
$courseB='B';
?>
<body>
<?php echo $rep; ?>
<form action="<?php echo $blk; ?>" method="POST">
Block : <input type="text" name="url" /></br>
<br>
<input type="submit" value="block" />
<br>
</form>
</body>
</html>
Your $blk isn't formatted in a way proper for <form>:
$blk = "<a href='newblocktryteacher.php?a=$name'>Block this site</a>";
<FORM action = <?php echo $blk; ?> method ="POST";>
Assuming $name is bob in this example:
<FORM action = <a href='newblocktryteacher.php?a=bob'>Block this site</a> method ="POST";>
As you can see, this is not correct. All you need is the URL itself. Also, remove the semicolon after "POST".
Your $blk is not a valid action form, because it's a <a> tag(link). So, $blk should be newblocktrypupil.php?a=$name' instead.

Categories

Resources