Updating content to the database onclick event - php

I spent the last 2 days trying to make the following code work.
I have three forms with content in different languages. For each of them there is an Edit button which popup a text editor. What I would like to do is when I click the button "Save and close" the edited content would be updated in the database using javascript (AJAX) and PHP/SQL but in the console it gives me the following error: Uncaught ReferenceError: valueContent is not defined. So here is the code I have so far.
$mvccont .=
"<form action='edit_language/".ID.".raw' method='post' target='pop' class='comparelist' style='clear: both' onsubmit='window.open(\"\", \"pop\", \"width=0,height=0,resizeable,scrollbars\");'>".
"<input type='hidden' name='keyword' value='".$keyword."'>".
"<table style='width:100%; border-collapse: collapse;' >".
"<tr><td width='20%' ><div class='info' style='width:100%; border:none;' >".
substr($keyword, 0, 20)."<a title='".$keyword."'><input type='submit' value='Save' class='BUYN'/></a>".
"<a href='edit_language/".$keyword."/delete'><input type='button' value='Delete' class='NONA'/></a>".
"</div></td>";
foreach($lan as $lang=>$lame) {
$idk = str_replace(" ", "", $keyword).$lang;
$mvccont .=
"<td><div class='number3'".(($red[$keyword][$lang]=='')?" style='background: #fcc;'":"")." style='border:none;' >".
"<input type='hidden' name='touch".$lang.$keyword."'/>".
"<input type='text' id='s".$idk."' name='a".$lang."' value='".htmlentities($lame, ENT_QUOTES, "UTF-8")."' onchange=\"this.style.borderColor='#ff0000'; document.getElementsByName('touch".$lang.$keyword."')[0].value='true'\"/><a onclick='popbox(false, ".$idk.", true); mce();'><input type='button' value='Edit' class='BUYN'/></a>".
"<br />".($red[$keyword][$lang]).
"<script>".
"function falaffel".$idk."(step) {".
"var l".$idk."=document.getElementById('s".$idk."').value;".
"if(step==1) {".
"return '<textarea>'+l".$idk."+'</textarea><input type=\"button\" value=\"Save & close\" class=\"NONA\" onclick=\"'+\"document.getElementById('s".$idk."').value=tinyMCE.activeEditor.getContent(); ".$idk."=falaffel".$idk."(1); popboxremove(); document.getElementById('s".$idk."').style.borderColor='#ff0000'; updateValue(); document.getElementsByName('touch".$lang.$keyword."')[0].value='true' \"+'\">';".
"}".
"return l".$idk.";".
"}".
"var ".$idk." = falaffel".$idk."(1); ".
"</script>".
"</div></td>";
}
$mvccont .=
"</tr></table></form>";
Then here is the AJAX code:
function updateValue(){
var ajaxRequest = getXmlHttpRequestObject();
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var valueContent = document.getElementsByTagName("input");
}
}
ajaxRequest.open("POST", "comp/update_lang.php", true);
ajaxRequest.send(valueContent);
}
And finally the PHP code:
<?php
foreach(array(1=>"en", 2=>"it", 3=>"pl") as $id=>$lang) {
if($_POST["touch".$id.$_POST["keyword"]]=="true") {
$sql = "UPDATE translat SET value='".mysql_real_escape_string($_POST["a".$id])."', date=".NOWTIME." WHERE lang=".$id." AND keyword='".urldecode($_POST["keyword"])."'";
$insert = $db->query($sql);
$insert->execute();
#unlink(TEMP."lang_".$lang.".cache");
}
}
$mvccont .=
"<script>".
"window.close();".
"</script>";
die($mvccont);
?>
Thank you so much for your help.

Related

Passing a variable in $_POST parameters not working in PHP?

I am displaying images from a database, each image has a comment box that contains a form and a submit button; the submit button has the name of the image that is displayed. Ex. if the image name is flowers.jpg, the name for the submit button is set to flowers.jpg. (I did string replace though, so in my code it would be set to flowersjpg) The names are added to the submit button with no problem at all. Because I have several images, I wanted to pass $row['image'] (image name) into $_POST[ ] parameters for an isset() function but it is not working. - All the code is in one document
$result = mysqli_query($conn, "SELECT * FROM uploads ORDER BY timestamp DESC");
while($row = mysqli_fetch_array($result))
{
// Prints all the photos with a caption
echo "<div class='wrapper' name=".$row['image'].">";
echo "<div class='caption'>";
echo "<h3>".$row['title']."</h3>";
echo "<p class='description'>".$row['description']."</p>";
echo "</div>";
echo "<img src='images/uploads/".$row['image']."'/>";
// name for submit button, the $row['image'] contains the entire image name so certain characters have to be removed
**$name = str_replace(array(".", "-", ":", "'", "/", " "), "", $row['image']);**
// comment box - just a form with a submit
echo "<div class='commentBox'>
<form action='test.php' method='POST'>
<textarea placeholder='Say something nice!' name='comment'></textarea>
// CREATES NAME FOR BUTTON. FOR EACH IMAGE, THE BUTTON HAS THE IMG NAME
**<button type='submit' name='".$name."' value='submit'> post </button>**
</form>
</div>";
echo "</div>";
}
if(isset($_POST["'".$name."'"]))
{
echo '<script type="text/javascript"> alert("isset"); </script>';
}
When I put the name as a plain string in the $_POST parameters, it works fine but when I put the variable in, it does not work. The $name variable is a string.
This works but I'm sure any string used will work as long as the button name and string in $_POST are the same.
echo "<button type='submit' name='flowerspng' value='submit'> post </button>";
if(isset($_POST[flowerspng]))
{
echo '<script type="text/javascript"> alert("isset"); </script>';
}
This code on the other hand, does not. I know variables can be passed inside $_POST parameters, so I don't understand what's wrong with my code. Is my syntax incorrect? Clearly the variable is not being set otherwise the alert box would pop up.
echo "<button type='submit' name='".$name."' value='submit'> post </button>";
if(isset($_POST["'".$name."'"]))
{
echo '<script type="text/javascript"> alert("isset"); </script>';
}
I have tried this too but I don't think it worked because there are no quotes around $name.
echo "<button type='submit' name=$name value='submit'> post </button>"
if(isset($_POST[$name]))
{
echo '<script type="text/javascript"> alert("isset"); </script>';
}
This code works:
echo "<button type='submit' name='$name' value='submit'> post </button>"
if(isset($_POST[$name]))
{
echo '<script type="text/javascript"> alert("isset"); </script>';
}
I moved the if statement for the isset() function into the end of the while loop, it works for all images now.
while($row = mysqli_fetch_array($result))
{
echo "<div class='wrapper' name=".$row['image'].">";
echo "<div class='caption'>";
echo "<h3>".$row['title']."</h3>";
echo "<p class='description'>".$row['description']."</p>";
echo "</div>";
echo "<img src='images/uploads/".$row['image']."'/>";
$name = str_replace(array(".", "-", ":", "'", "/", " "), "", $row['image']);
echo "<div class='commentBox'>
<form action='test.php' style='text-align: right; background-color: rgb(34,34,34); width: 100%; height: 120%; margin: 0;' method='POST'>
<textarea style='height: 90%; width: 79%; resize: none; border-radius: 3px; margin: 0; margin-top: 5px;' placeholder='Say something nice!' name='comment'></textarea>
<button type='submit' name='$name' value='submit' style='height: 100%; width: 20%; float: right;'> post </button>
</form>
<div class='seeMore'> see more </div>
</div>";
echo "</br><div class='comments' style='display: none;'> comment display </div>";
echo "</div>";
**
// testing the if statement for each image - works perfectly now
if(isset($_POST[$name]))
{
echo '<script type="text/javascript"> alert("'.$name.'"); </script>';
mysqli_query($conn, "UPDATE uploads SET title='test' WHERE image='".$row['image']."'");
header("Refresh:0");
}
**
}

Unable to assign POST data from multiple check boxes

I am a newbie to PHP and MySQL, so please ignore my bad practice of coding :)
Ok, here's the scenario:
I have a page, checkin.php which has two forms;
Form 1: Takes some input and displays data from database along with multiple checkboxes.
Form 2: It is dynamically generated if any data is found above.
Here's the code:
//Echoing out the query results
echo "<form method='POST' action='checkin.php'>";
echo "<tbody>";
foreach ($checkin_status as $row) {
echo "$rowStart";
echo "$row[book_id]";
echo "$insertColumn";
echo "$row[branch_id]";
echo "$insertColumn";
echo "$row[card_no]";
echo "$insertColumn";
echo "$row[fname]" . " " . "$row[lname]";
echo "$insertColumn";
echo "$row[date_out]";
echo "$insertColumn";
echo "$row[due_date]";
echo "$insertColumn";
//Checkbox creation
$checkin_recall_value = "$row[book_id]" . " " . "$row[branch_id]" . " " . "$row[card_no]";
echo "<div style='text-align: center;'><input type='checkbox' name='checkin' value='$checkin_recall_value'></div>";
echo "$rowEnd";
}
echo "</tbody>";
//Checkin button in foot
echo "<tfoot>";
echo "<td colspan='7' style='text-align: center; padding-right: 20px;'><input id='checkin_button' type='submit' value='Check In' title='Click to check in!'></td>";
echo "</tfoot>";
echo "</form>";
The form is generated with checkboxes along side the returned tuples.
Now when I am selecting some checkboxes and trying to display, it WONT...
Here is the code:
if(!empty($_POST['checkin'])) {
//Retrieving the recall variables as an array from POST and assigning to $main_array
$main_array = array();
//echo "I am here";
if(is_array($_POST['checkin'])) {
foreach($_POST['checkin'] as $value)
{
$sub_array = explode(" ", $value);
array_push($main_array, $sub_array);
}
}
print_r($main_array);
}
Its returning an empty array...
Please help me fix this.
IF you want to have multiple html elements with the same name, you must define it as an array, adding []:
<input type='checkbox' name='checkin[]' value='$checkin_recall_value'>

How can I pass ids to colorbox?

Colorbox is not recognizing ids of random elements when clicked on , it alerts ids in ascending order when you click on elements randomly .. How can I make it so the right id is displayed when clicking on whatever element ?
JavaScript Code
jQuery(document).ready(function() {
$("a.madscore").colorbox({
inline:true,
width:"350px",
href: "#madcomment_menu"
});
$("div#scoring_scale a").click(function(e) {
e.preventDefault();
ID = $(this).attr('id');
alert(ID);
point = $(this).text();
username = $('#username'+ID).val(); alert(username);
name = $('#name'+ID).val();
image = $('#image'+ID).val();
//var message = $('textarea#text'+ID).val(); alert(message);
var result ='Just gave #'+username+' a score of'+point+'via MadFlock';
$('textarea#text'+ID).attr("value", result);
tweet = $('textarea#text'+ID).val();
});
});
PHP Code
<?php
$select = "SELECT * FROM COMMENTS INNER JOIN Twitter_Data ON Twitter_Data.screen_name=Comments.Twitter WHERE Category ='Comments'";
$result = mysql_query($select);
$result_count = mysql_num_rows($result);
echo " <table border =\"0\">";
echo "<tr>";
$user_array = array();
$counter = 0;
if($result_count > 0) {
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "<div id ='scoring_scale' class='madscore".$row['ID']."' style='display:none;'>";
echo "<div id='madcomment_menu' style='padding:10px; background:#fff;'>";
echo "<a id='".$row['ID']."' class='green_circle' href='#'> +3 </a>";
echo "<a id='".$row['ID']."' class='orange_circle' href='#'> +1 </a>";
echo "<a id='".$row['ID']."' class='red_circle' href='#'> -1 </a>";
echo "<a id='".$row['ID']."' class='brown_circle' href='#'> -3 </a><br />";
echo"<form>";
echo "<textarea id='text".$row['ID']."'rows='5' cols='33'>";
echo "-";
echo "</textarea>";
echo"<button id='button".$row['ID']."'class='button_madscore'> MadScore </button>";
echo "</form>";
echo "</div>";
echo "</div>";
}
}
// Here is the link that will generate the COLORBOX pop-up
echo "<a id='".$row['ID']."'class=' madscore' href='#madcomment_menu'><img src='images/madcomment.png' /> </a>";
?>

Send checked table row value in array

I have a table like this:
CheckBox Course Semester Paper Level
[] ABC 1 1 •Expert •normal
[] ABC 1 2 •Expert •normal
[] ABC 1 3 •Expert •normal
[Save]
In the above table checkbox value is paper value. The trouble i am having is that , when i click on save, it runs a parameterised javascript function addpapers($course,$semester) as you can see they are same, but i am not able to send the checkbox value and level value together. a person can choose either expert or normal level for a given paper. I am not able to send them in javascript.
function flagaddpapers(course, semester) {
displayBox = document.getElementById("studentBox");
elements = document.getElementsByName('pids[]');
levelid = document.getElementsByName('radios[]');
data = [];
for (i = 0; i < elements.length; i++) {
if (elements[i].checked) {
data.push('pids[]=' + encodeURIComponent(elements[i].value));
}
}
params = "teacher=" + encodeURIComponent(teacher) + "&course=" + encodeURIComponent(course) + "&semester=" + encodeURIComponent(semester) + "&flag=" + encodeURIComponent(1) + "&" + data.join('&');
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.body.removeChild(document.getElementById('loadMsg'));
result = xmlhttp.responseText;
alert(result);
displayBox.className = "dimBox";
setTimeout(function () {
ajaxstp();
document.body.removeChild(document.getElementById('adBackground'));
document.body.removeChild(document.getElementById('adBox'));
}, 200);
}
}
xmlhttp.open("POST", "tpaper.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send(params);
}
This is my html code:
echo "<tr>
<th style='font-size:12px;'><input type='checkbox' id='master_check'
onclick='masterSelect();' style='display:inline-block;cursor:pointer;'></th>
<th style='font-size:12px;'>S.No</th>
<th style='font-size:12px;'>Course</th>
<th style='font-size:12px;'>Semester</th>
<th style='font-size:12px;'>Level</th>
<th style='font-size:12px;'>Paper</th></tr>";
$counter17 = 1;
while($row4 = mysql_fetch_array($q4))
{
$pids = $row4['pid'];
echo "<tr>";
echo "<td><input type='checkbox' name='pids[]' class='persistentChecks'
style='display:inline-block;cursor:pointer;' value=".$pids."></td>";
echo "<td style='text-align:center;font-size:12px;'>".$counter17."</td>";
echo "<td style='text-align:center;font-size:12px;'>".$row4['cname']."</td>";
echo "<td style='text-align:center;font-size:12px;'>".$semester."</td>";
echo "<td style='font-size:12px;'><input type='radio' name='radio[]' id='radio1'
value='1'>Expert</input><input id='radio2' type='radio' name='radio[]'
value='2'>Normal</input></td>";
echo "<td style='text-align:center;font-size:12px;'>".$row4['pname']."</td></tr>";
$counter17 = $counter17 + 1;
}
echo "</table>";
echo "<div style='text-align:left;'><input type='button' class='t_s' value='Add Paper'
onclick='flagaddpapers(".$course.",".$semester."); return false'
id='addpaperBtn'><input type='button' class='t_s' value='Cancel' onclick='closedialog();
return false'></div>";
Try this..
here pval is the paperid and lval is the level value
<script type="text/javascript">
function merge_value(lval,pval)
{
document.getElementById(pval).value=pval + '-' + lval;
}
</script>
<?php
echo "<tr>
<th style='font-size:12px;'><input type='checkbox' id='master_check'
onclick='masterSelect();' style='display:inline-block;cursor:pointer;'></th>
<th style='font-size:12px;'>S.No</th>
<th style='font-size:12px;'>Course</th>
<th style='font-size:12px;'>Semester</th>
<th style='font-size:12px;'>Level</th>
<th style='font-size:12px;'>Paper</th></tr>";
$counter17 = 1;
while($row4 = mysql_fetch_array($q4))
{
$pids = $row4['pid'];
echo "<tr>";
echo "<td><input type='checkbox' name='pids[]' id='".$pids."' class='persistentChecks'
style='display:inline-block;cursor:pointer;' value=".$pids."></td>";
echo "<td style='text-align:center;font-size:12px;'>".$counter17."</td>";
echo "<td style='text-align:center;font-size:12px;'>".$row4['cname']."</td>";
echo "<td style='text-align:center;font-size:12px;'>".$semester."</td>";
echo "<td style='font-size:12px;'><input type='radio' name='radio[]' id='radio1'
value='1' onClick='javascript:merge_value(this.value,".$pids.")'>Expert</input><input id='radio2' type='radio' name='radio[]'
value='2' onClick='javascript:merge_value(this.value,".$pids.")'>Normal</input></td>";
echo "<td style='text-align:center;font-size:12px;'>".$row4['pname']."</td></tr>";
$counter17 = $counter17 + 1;
}
echo "</table>";
echo "<div style='text-align:left;'><input type='button' class='t_s' value='Add Paper'
onclick='flagaddpapers(".$course.",".$semester."); return false'
id='addpaperBtn'><input type='button' class='t_s' value='Cancel' onclick='closedialog();
return false'></div>";
?>

HTML onClick functionality on working

I am trying to get images from my server via php and I want to have onClick functionality but it is not working following is my code sample:
<?php
$conn = ftp_connect("myserver") or die("Could not connect");
ftp_login($conn,"username","password");
$images = ftp_nlist($conn,"folder");
$r = count($images);
for($i=0;$i<$r;$i++)
{
//echo " $images[$i] ";
echo"<img id= '$i' class = '' border='1' src='mysource' width='300' height='250'>";
echo "<button onClick= 'hide()'> Print </button>";
echo "<button> Email </button>";
echo "<button> Text Me </button>";
echo "</br>";
}
ftp_close($conn);
?>
and following is my javascript code
function hide()
{
var t = document.getElementById(x);
t.setAttribute(class, print);
}
when I click my print button it is not even calling that function by the way this all is in .php file. Thanks for ay help.
→ Try This:
echo "<script type='text/javascript'>function hide_it(){alert('Entering Function hide_it()?'); /*var t = document.getElementById(x); t.setAttribute(class, print);*/}</script>";
for($i=0;$i<$r;$i++)
{
//echo " $images[$i] ";
echo"<img id= '$i' class = '' border='1' src='mysource' width='300' height='250'>";
echo "<button type='button' onclick='javascript:hide_it()'> Print </button>";
echo "<button> Email </button>";
echo "<button> Text Me </button>";
echo "</br>";
}

Categories