insert php in jquery script - php

this jquery script not work when im insert php tag into this,.. can i know what is the problem..
<script language="javascript">
$("#openmsg_<?php echo $id; ?>").click(function(){
$("#toggleText_<?php echo $id; ?>").toggle(800);
})
</script>
this is my jquery code to toggle
<form action="my_messages.php" method="POST" name="<?php echo $msg_title; ?>">
<input type="button" name="openmsg" id="openmsg_<?php echo $id; ?>" value="<?php echo $msg_title; ?>" onClick="javascript:toggle<?php echo $id; ?>();" />
<input type="submit" name="setopened_<?php echo $id; ?>" value="I have read this" />
</form>
<div id="toggleText_<?php echo $id; ?>" style="display:none;">
<?php echo $msg_body; ?>
</div>

Update your code like this :
$("#openmsg_"+).click(function(){
$("#toggleTextuserid_"+).toggle(800);
})

Try this:
<script language="javascript">
var id = <?php echo $id?>;
$("#openmsg_" + id + "").click(function(){
$("#toggleTextuserid_" + id + "").toggle(800);
})
</script>

Related

How do I take value with jQuery

standard input, select box and multiple select box can also get the desired value, it works.. but how can i get the value with jquery in this code snippet?
<script>
$(document).ready(function(){
jQuery('#checkboxesmarka').change(function(){
var id=jQuery('#checkboxesmarka').val();
alert(id);
});
});
</script>
<div id="checkboxesmarka">
<?php
while($markacek=$markasor->fetch(PDO::FETCH_ASSOC)) { ?>
<label for="<?php echo "marka".$markacek['marka_id'] ?>">
<input type="checkbox" id="<?php echo "marka".$markacek['marka_id'] ?>" name="marka_id[]" value="<?php echo $markacek['marka_id'] ?>" /><?php echo $markacek['marka_name'] ?> </label>
<? } ?>
</div>
$(function(){
$('#checkboxesmarka').click(function(){
var valSelected = [];
$(':checkbox:checked').each(function(i){
valSelected[i] = $(this).val();
});
console.log(valSelected);
});
});
In console all selected value

How to avoid the jquery "colorbox" closing when we are submitting the data

i'm calling the external php page by using the ajax method in Jquery colorbox in that while i'm submitting the data the colorbox is closed automatically n go to the normal page view without lightbox how to avoid that and reopen the colorbox after submitting....
Please tell some suggestions r solutions abt this problems friends...
//home page
//jquery of colorbox
<script src="jquery/jquery.colorbox-min.js"></script>
<script>
$(function(){
$(".ajax").colorbox({width:"63%", height:"80%"})
});
</script>
<a class="ajax" href="assign_ajax.php">New Task</a>
This is the the page called by ajax in colorbox while submitting it goes to the normal page out of colorbox im using
//////////////////////////////ajax page///////////////////////
if(isset($_POST['update'])){
$sql = UPDATE `notes` SET `note`=$_POST['update_text'] WHERE`id`={$_GET['note_id']};
mysql_query($sql) or die(mysql_error());
}
<form name="update_form" id="update_form" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);>?note_id=<?php echo $row['id']; ?>" method="post">
<textarea name="update_text" id="update_text" rows="5" cols='55'><?php echo $row['note']; ?></textarea>
<br>
<input type="submit" id="update" name="update" value="update">
<input type="button" value="cancel" class ="edit" id="edit-<?php echo $row['id']; ?>">
</form>
<script src="jquery/jquery.colorbox-min.js"></script>
<script>
function showColorBox(event)
{
event.preventDefault();
$.colorbox({href:"assign_ajax.php"});
}
</script>
<a class="ajax" onclick="showColorBox(event)">New Task</a>
//Or
<script src="jquery/jquery.colorbox-min.js"></script>
<script>
$(function(){
$(".ajax").on("click",function(event){
event.preventDefault();
$.colorbox({href:$(this).attr("href")});
});
});
</script>
<a class="ajax" href="assign_ajax.php">New Task</a>
EDIT
<?php
if(isset($_POST['update'])){
$sql = "UPDATE `notes` SET `note`=$_POST['update_text'] WHERE`id`={$_GET['note_id']}";
mysql_query($sql) or die(mysql_error());
}
?>
<form name="update_form" id="update_form" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);>?" note_id="<?php echo $row['id']; ?>" method="post" onsubmit="return FALSE;">
<textarea name="update_text" id="update_text" rows="5" cols='55'><?php echo $row['note']; ?></textarea>
<br>
<input type="button" id="update" name="update" value="update">
<input type="button" value="cancel" class ="edit" id="edit-<?php echo $row['id']; ?>">
</form>
<script>
$(function(){
$("#update").on("click",function(){
$.ajax({url: $("#update_form").attr("action"), data:{note_id: $("#update_form").attr("note_id"),update_text:$("#update_text").val()}, type:"POST", dataType:"json"}).done(function(data){});
});
});
</script>

transfer php-variable to javascript variable

I have incorporated this simple datetimepicker : http://trentrichardson.com/examples/timepicker/#rest_examples
So it looks
<?php $tabname = "brightness"; ?>
<script type="text/javascript">
$('#datumstartbrightness').timepicker();
</script>
...
<input type="text" name="datumstart<?php echo $tabname; ?>"
id="datumstart<?php echo $tabname; ?>"/>
...
With this code it works and datetimepicker appears !
But with this ...
<?php $tabname = "brightness"; ?>
<script type="text/javascript">
$('#datumstart<?php echo $tabname; ?>').timepicker();
</script>
...
<input type="text" name="datumstart<?php echo $tabname; ?>" id="datumstart<?php echo $tabname; ?>"/>
...
DateTimePicker doesn't appear. What can be wrong with this line
$('#datumstart').timepicker();
You need to do the ready-state first before you can call jQuery-Functions: So try this:
<script type="text/javascript">
$(function() {
$('#datumstart<?php echo $tabname; ?>').timepicker();
});
</script>

how to store html and php code inside a php variable to be echoed to a html page

I'm trying to echo a whole html string with some php code in it to a html page, the problem is, i can't store the string to a php variable;
prependapp.php
<?php
include("db.php");
include("tolink.php");
$result=mysql_query("select * from messages order by msg_id desc");
$row=mysql_fetch_array($result);
$id=$row['msg_id'];
$msg=$row['message'];
$date=$row['date_sent'];
$msg= nl2br($msg);
$msg="<br>{$msg}<br>{$date}";
$app = <<<EOD
<li class="bar<?php echo $id; ?>">
<div align="left" class="post_box">
<span style="padding:10px"><?php echo $msg; ?> </span>
<span class="delete_button">X</span>
<span class='feed_link'>comment</span>
</div>
<div id='expand_box'>
<div id='expand_url'></div>
</div>
<div id="fullbox" class="fullbox<?php echo $id; ?>">
<div id="commentload<?php echo $id; ?>" >
</div>
<div class="comment_box" id="c<?php echo $id; ?>">
<form method="post" action="" name="<?php echo $id; ?>">
<textarea class="text_area" name="comment_value" id="textarea<?php echo $id; ?>">
</textarea><br />
<input type="submit" value=" Comment " class="comment_submit" id="<?php echo $id; ?>"/>
</form>
</div>
</div>
</li>
EOD;
echo $app;
?>
javascript inside the html page
<script>
$(document).ready(function() {
$("#formID").validationEngine({
ajaxSubmit: true,
ajaxSubmitFile: "update_ajax.php",
success : function() {
alert("success exe!");
$("ol#update").load("prependapp.php");
$("ol#update li:first").slideDown("slow");
document.getElementById('content').value='';
$('#content').value='';
$('#content').focus();
alert("success exe");
},
failure : function() { alert("success fail"); }
})
});
</script>
the "test" on my prepend always shows the problem is the $app doesn't, is there any way for me to prepend large html code w/ some php code inside it to my html page?
You can't use PHP code in javascript part. Please note PHP would be rendered in server side and javascript would be executed in client browser. So, browser does not have access to any PHP variables and objects.
However, you could use AJAX. For example:
$.ajax({
url : "/server/prepend.php",
success : function(response)
{
$("ol#update").prepend(response);
},
});
Would execute php code in server side and prepends the result in ol#update tag.

how to get current value of textbox in jquery

I'm trying to compute a value from 2 given values quantity and price. I used the keyup event in jquery to compute the total from the current value of the qty and price.
How will I be able to reference the current textbox which initiates the keypress event. And put the corresponding total to the assigned box.
Do you know of any jquery plugins that will make it easier for me to achieve the result.
<script type="text/javascript">
$(function(){
$('input[name^=qty]').keyup(function(){
var curprice=$('input[name^=price]').val();
var curqty=$('input[name^=qty]').val();
var curtotal=curprice * curqty;
$('input[name^=comp]').val(curtotal);
});
});
</script>
</head>
<body>
<?php
for($a=0;$a<=5;$a++){
?>
Price:<input type="text" name="<?php echo 'price'.$a; ?>" value="<?php echo $a; ?>"/>
Quantity:<input type="text" name="<?php echo 'qty'.$a; ?>" value="<?php echo $a; ?>"/>
Computed:<input type="text" id="<?php echo 'a'.$a; ?>" name="<?php echo 'comp'.$a ?>" value="" />
<?php
echo "<br/>";
}
?>
</body>
</html>
Heres a screenshot.
If you need more details, feel free to ask, thanks.
<script type="text/javascript">
$(function(){
$('input[name^=qty]').keyup(function(){
var parentDiv = $(this).closest('div');
var curprice=parentDiv.find('input[name^=price]').val();
var curqty= this.value;
var curtotal=curprice * curqty;
parentDiv.find('input[name^=comp]').val(curtotal);
});
});
</script>
</head>
<body>
<?php
for($a=0;$a<=5;$a++){
?>
<div>
Price:<input type="text" name="<?php echo 'price'.$a; ?>" value="<?php echo $a; ?>"/>
Quantity:<input type="text" name="<?php echo 'qty'.$a; ?>" value="<?php echo $a; ?>"/>
Computed:<input type="text" id="<?php echo 'a'.$a; ?>" name="<?php echo 'comp'.$a ?>" value="" />
</div>
<?php
echo "<br/>";
}
?>
</body>
</html>
notice all the changes against your current code.
Here's something else. It uses regex to get the number part of the element's name, and assumes that something named priceX and compX are the related inputs.
$("input[name^=qty]").keyup(function() {
var i = /^qty(\d+)$/.exec($(this).attr('name'))[1],
price = parseFloat($("input[name=price" + i + "]").val()),
qty = parseInt($("input[name=qty" + i + "]").val(), 10);
$("input[name=comp" + i + "]").val(price * qty);
});
Example: http://jsfiddle.net/AHUHu/

Categories