Pass a php variable to dynamically generated div's onclick event - php

how to pass a php variable to dynamicaly generated div's onclick event?
here is my code
while($rowset = mysql_fetch_array($result))
{
$idTT1++;
$editTT='b'.$idTT1;
$hidTextTT='t'.$idTT1; $hidSubIdTT='s'.$idTT1;
echo "<tr><td>".$rowset[1]." ".$rowset[2]."</td><td> </td>
<td class='table_label'>
<div id='".$idTT1."' onclick='editView($idTT1,$editTT)' >".$rowset[3]."</div>";
echo "<div id='".$editTT."' style='display:none;'>
<input id='".$hidSubIdTT."' name='box1' type='hidden' value='".$row['dDegreeName']."'>
<input id='".$hidTextTT."' name='box2' type='text' value='' />
<input type='submit' value='Update' name='submit'
onclick='updateSubject($hidSubIdTT,$hidTextTT)'/>
<input type='button' value='Cancel' name='Cancel' onclick='setEditView($idTT1,$editTT)'/>
</div></td></tr>";
}
i want to pass 2 variables $idTT1 and $editTT. im getting the value of $editTT1 in javascript but i cant get value of $editTT value in editView() javascript function.

That is probably because you are not quoting the IDs. Try
setEditView(\"$idTT1\",\"$editTT\")
in the line before last.

You need to put it between quotes. Something like this should work:
"<input type='button' value='Cancel' name='Cancel' onclick='setEditView(\"$idTT1\",\"$editTT\")'/>"

Related

insert and delete functionality with ajax

I am working on basic functionality of insert,add,delete and update using ajax.
I have done little functionality write now..actually i want all functionality in a grid .
I put all html code inside a for loop.. this loop will execute till total num rows.. first execution of loop is fine, problem occur after this... next time when i click on delete button ajax call is not working.
Need help on this....
I have 2 files.. form.php and operation.php
code of form.php..
$(document).ready(function(){
$("#delete").click(function(){
var id=$("#uid").val();
$.post("operation.php",{ID:id},function(data){
$("#result").html(data);
});
});
});
for($i=0;$i<2;$i++)
{
echo "<input type='text' value=".mysql_result($all_records, $i, "id")." name='uid' id='uid'>
<input type='text' value=".mysql_result($all_records, $i,"name")." name='name' id='email' placeholder='Email'>
<input type='text' value=".mysql_result($all_records, $i,"email")." name='email' id='email' placeholder='Email'>
<input type='password' value=".mysql_result($all_records, $i,"password")." name='pass' id='pass' placeholder='Password'>
<input type='submit'name='delete' id='delete' value='Delete'>
<input type='submit'name='update' id='update' value='Update'>";
echo "</td></tr>";
}
operation.php
write now i put only one line code for checking purpose...
echo $_POST['ID'];
Well i think you know that ids should have unique names. It is not a good idea to give same id to multiple elements in the same page.
I think the code bellow will solve your purpose.
$(document).ready(function(){
$(".delete").click(function(){
var id=$(this).data('id');
$.post("operation.php",{ID:id},function(data){
$("#result").html(data);
});
});
});
for($i=0; $i<2; $i++)
{
echo "<input type='text' value='".mysql_result($all_records, $i, "id")."' name='uid' class='uid'>
<input type='text' value='".mysql_result($all_records, $i,"name")."' name='name' class='name' placeholder='Email'>
<input type='text' value='".mysql_result($all_records, $i,"email")."' name='email' class='email' placeholder='Email'>
<input type='password' value='".mysql_result($all_records, $i,"password")."' name='pass' class='pass' placeholder='Password'>
<input type='button'name='delete' data-id='".mysql_result($all_records, $i, "id")."' class='delete' value='Delete'>
<input type='submit' name='update' class='update' value='Update'>";
echo "</td></tr>";
}
Try to understand it and compare it with your previous code.
Let me know if you need further assistance.

How to select values out of radio buttons and put them in a PHP Variable

I'm making a Quiz. And with each question I'm showing the possible answers( "True" or "False") with a While loop in PHP:
echo "<form method='post' action='quizCheck.php'>";
while(x=0;x<=10; x++){
echo "<div class='buttons'>
<label>True
<input type='radio' name='answer' value='true' />
</label>
<label>False
<input type='radio' name='answer' value='false' />
</label>
</div>";
}
echo "</form>";
Let's say there are 10 questions and I select "True" on 6 questions.
What code do I have to put in quizCheck.php so it can count the number of "True" answers and store it in a variable?
You will need to do two things, first you need a submit button in the form:
<button type="submit" value="Submit">Submit</button>
Then you will also need the names of the radio inputs to be unique so in the while loop (which you really should just change to a for loop) do:
for(x=0;x<=10; x++){
echo "<div class='buttons'>
<label>True
<input type='radio' name='answer{$x}' value='true' />
</label>
<label>False
<input type='radio' name='answer{$x}' value='false' />
</label>
</div>";
}
When the form is submitted, then in quizCheck.php you just check $_POST[answer0] through $_POST[answer9] to see which are true and increment a counter.
If you want the answers in a single array then do this:
for(x=0;x<=10; x++){
echo "<div class='buttons'>
<label>True
<input type='radio' name='answers[$x]' value='true' />
</label>
<label>False
<input type='radio' name='answers[$x]' value='false' />
</label>
</div>";
}
When this form is submitted, then in quizCheck.php you just get something like $answers = $_POST[answers] and then go through answers[0] to answers[9] for example

PHP Like Button Problems

I am trying to make a like button for posts on my website.
PHP for like query (d_db_update is
function d_db_update($string) {
return mysql_query($string);
}
)
if($_GET['like']) {
$like = d_db_update("UPDATE posts set post_rating = post_rating+1 WHERE post_id = {$_GET['like']}");
}
Button
<form action='{$_SERVER['PHP_SELF']}&like={$posts_row['post_id']}' method='get'>
<p align='left'>{$posts_row['post_rating']}
<input type='submit' name='like' value='Like' /></p>
</form>
What can I do to fix it/make it work?
Use below form with a hidden input it solve your problem.
<form action='{$_SERVER['PHP_SELF']}' method='get'>
<p align='left'>{$posts_row['post_rating']}
<input type='hidden' name='like' value='{$posts_row['post_id']}' />
<input type='submit' value='Like' /></p>
</form>
You are using your form action wrong.. if you are using get method than there is not need to use the form..
try this..
<a href='yourpage.php?like=<?php echo $post_id ?>'>Like</a>
your submit button name and like variable which you have used in action url are the same , and you used get method in method of form.So, you need to change the submit button name.
or
you can do it without using form only on button click try below code
<input type='button' name='like' value='Like' onclick="location.href='yourpage.php?like=<?php echo $post_id ?>'" />
Change your code to this
You can not write PHP variables using {}. You need to echo them out.
<form action='' method='get'>
<p align='left'><?php echo $posts_row['post_rating'] ?>
<input type='hidden' name='like' value='<?php echo $posts_row["post_id"] ?>' />
<input type='submit' value='Like' /></p>
</form>
Edit--
You were not returning the post id correctly, I made the changes, also there is no need to provide any action as it will be self only.

Jquery Radio Button Submit Form

I have multiple forms on one page all are like this: The ID value changes but the options can be similar.
<form class="test"><td>4000</td>
<td align='center'><input type='radio' name='radio' value='car' checked></td>
<td align='center'><input type='radio' name='radio' value='boat' ></td>
<td align='center'><input type='radio' name='radio' value='Plane' ></td>
<input type='hidden' name='id' value='4000'/>
<input type='hidden' name='location' value='local'/>
<div class="form" style="display: none;">
</div></form>
Using JQuery I'm trying to submit this when any of the 3 radio buttons are selected.
<script>
$("input[name='radio']").click(function() {
var CurrectForm=$(this).parents('.test:first');
$.post(
'do.php',
CurrectForm.serialize(),
function( response ) {
CurrectForm.find('#form').html( response );
show( response );
}
);
} );
</script>
I've used similar to the above with checkboxes and dropdown lists, which have worked fine. But this doesn't seem to submit the values to the do.php page. The values that are submitted are show in the div form. Doing a var_dump($_POST); results in an empty array.
Can someone point me in the right direction ?
Thanks
Changing the form ti this seems to work ?
<td><form class="test">
<input type='radio' name='radio' value='car' checked>
<input type='radio' name='radio' value='boat' >
<input type='radio' name='radio' value='Plane' >
<input type='hidden' name='id' value='4000'/>
<input type='hidden' name='location' value='local'/>
<div class="form" style="display: none;">
</div></form></td>
Why does changing the cause it to work ?
Jfiddle with working example :) http://jsfiddle.net/QvpH5/
My guess is that there are multiple forms on your page with the class test. What is happening, is when you use parents(), you don't realize it first finds all of the ancestors, and then searches for .test:first. This will return the very first form's values, regardless of which form is clicked.
Look at this jsFiddle to see the fix (change .parents to .parent): http://jsfiddle.net/SDzzr/

How would one echo this jQuery in PHP

echo "<form class='noPrint' action='demo/saveToExcel.php' method='post' target='_blank'
onsubmit='$('#datatodisplay').val( $('<div>').append( $('#dataTable').eq(0).clone() ).html() )'>
<pre><input id='excel' type='image' src='img/file.png'></pre>
<p id='save'>Save table data to Excel</p>
<pre><input type='hidden' id='datatodisplay' name='datatodisplay' />
</form>
<input class='noPrint' type='button' id='print' value='Print' />";
When i run the page, i dont get a parse error, however ').append( $('#dataTable').eq(0).clone() ).html() )'> actually shows on the page, therefore the jQuery doesn't work!
How can i include it in the echo correctly?
Thanks
Why not skip the echo altogether like this:
//your PHP code before this echo statement
//let us say this is part of a IF statement
if(true)
{
?>
<form class='noPrint' action='demo/saveToExcel.php' method='post' target='_blank'
onsubmit="$('#datatodisplay').val( $('<div>').append( $('#dataTable').eq(0).clone() ).html() )">
<pre><input id='excel' type='image' src='img/file.png'></pre>
<p id='save'>Save table data to Excel</p>
<pre><input type='hidden' id='datatodisplay' name='datatodisplay' />
</form>
<input class='noPrint' type='button' id='print' value='Print' />
<?php
} //if ends here
// continue with your PHP code
EDIT: You also have a improperly nested quote characters in onsubmit. The code given above ALSO fixes that by converting those quotes to double quotes.
You can also use echo and escape those quotes like this:
echo "<form class='noPrint' action='demo/saveToExcel.php' method='post' target='_blank'
onsubmit=\"$('#datatodisplay').val( $('<div>').append( $('#dataTable').eq(0).clone() ).html() )\">
<pre><input id='excel' type='image' src='img/file.png'></pre>
<p id='save'>Save table data to Excel</p>
<pre><input type='hidden' id='datatodisplay' name='datatodisplay' />
</form>
<input class='noPrint' type='button' id='print' value='Print' />";
You have
onsubmit='$('
Single quotes inside attribute values delimited with single quotes must be represented as ' so they get treated as data and not the other end of the attribute value.
Also, and credit to knittl, double quote delimited strings in PHP interpolate variables. So you need to escape the $ signs for PHP.
This would also be better written using:
Unobtrusive JavaScript
Thus keeping the JS in a separate file and not having to worry about nested quotes
A block of HTML instead of an echo statement (conditionals wrapped around it still apply)
Letting you avoid having three levels of quotes (PHP, HTML, JavaScript)
Avoiding having to worry about variable interpolation in PHP
With that kind of variable, the heredoc concept would be pretty useful
$variable = <<<XYZ
........
........
XYZ;
Add # sign in front of string like this
echo #" and now you can have multiple lines
I hope this helps
You shouldnt use echo for this, you can just safely stop PHP for a sec.
The error seems to be in the HTML, as such:
onsubmit='$('#datatodisplay').
HTML thinks the onsubmit is only $(, you should use " instead.
there are several issues …
php substitutes variables in double quoted strings ("$var"), with their value.
you'd either have to use single quotes and escape the other single quotes, or use heredoc:
echo <<<EOT
<form class="noPrint" action="demo/saveToExcel.php" method="post" target="_blank"
onsubmit="$('#datatodisplay').val( $('<div>').append( $('#dataTable').eq(0).clone() ).html() )">
<pre><input id="excel" type="image" src="img/file.png"></pre>
<p id="save">Save table data to Excel</p>
<pre><input type="hidden" id="datatodisplay" name="datatodisplay" />
</form>
<input class="noPrint" type="button" id="print" value="Print" />
EOT;
you can also output your html directly, without php
// suspend php script
?>
<form class="noPrint" action="demo/saveToExcel.php" method="post" target="_blank"
onsubmit="$('#datatodisplay').val( $('<div>').append( $('#dataTable').eq(0).clone() ).html() )">
<pre><input id="excel" type="image" src="img/file.png"></pre>
<p id="save">Save table data to Excel</p>
<pre><input type="hidden" id="datatodisplay" name="datatodisplay" />
</form>
<input class="noPrint" type="button" id="print" value="Print" />
<?php // resume php script
furthermore, javascript event handlers should not be declared inline. use javascript to create and apply them to DOM elements
try this
echo "<form class='noPrint' action='demo/saveToExcel.php' method='post' target='_blank'
onsubmit='$(\"#datatodisplay\").val( $(\"<div>\").append( $(\"#dataTable\").eq(0).clone() ).html() )'>
<pre><input id='excel' type='image' src='img/file.png'></pre>
<p id='save'>Save table data to Excel</p>
<pre><input type='hidden' id='datatodisplay' name='datatodisplay' />
</form>
<input class='noPrint' type='button' id='print' value='Print' />";
you have '$('#datatodisplay'). html parses this as '$(' then takes the first > which is in <div> and print all whats after .
echo "<form class='noPrint' action='demo/saveToExcel.php' method='post' target='_blank'";
echo "onsubmit='\$(\'#datatodisplay\').val(\$(\'<div>\').append(\$(\'#dataTable\').eq(0).clone() ).html() )'>";
echo "<pre><input id='excel' type='image' src='img/file.png'></pre>";
echo "<p id='save'>Save table data to Excel</p>";
echo "<pre><input type='hidden' id='datatodisplay' name='datatodisplay' />";
echo "</form>";
echo "<input class='noPrint' type='button' id='print' value='Print' />";
try this! ;) you have to escape $ with \ and there shouldnt be \n (new line in response) so thats why multiple echos or you can all put to variable and then echo it at end!
Sorry, this is right answer lol
echo "<form class='noPrint' action='demo/saveToExcel.php' method='post' target='_blank'
onsubmit=\"$('#datatodisplay').val( $('<div>').append( $('#dataTable').eq(0).clone() ).html() )\">
<pre><input id='excel' type='image' src='img/file.png'></pre>
<p id='save'>Save table data to Excel</p>
<pre><input type='hidden' id='datatodisplay' name='datatodisplay' />
</form>
<input class='noPrint' type='button' id='print' value='Print' />";
Why are you so angry. hehe :)

Categories