update values in the textboxes using javascript - javascript

actualy im just a newbie programmer and i made my own update javascript function for my program but sadly the codes doesnt update..can anyone help me assist in order my codes work? please.
i want to do is if i change the values in the textboxes and click update it will update.
script:
<script type="text/javascript">
$(document).ready(function() {
$('#updates').click(function() {
$id1 = $('#id1').val();
$di2 = $('#id2').val();
$name1 = $('#name1').val();
$name2 = $('#name2').val();
$optA1 = $('#optA1').val();
$optA2 = $('#optA2').val();
$optB1 = $('#optB1').val();
$optB2 = $('#optB2').val();
$other_qual1 = $('#other_qual1').val();
$other_qual2 = $('#other_qual2').val();
$interview1 = $('#interview1').val();
$interview2 = $('#interview2').val();
$total1 = $('#total1').val();
$total2 = $('#total2').val();
$.ajax({
type: "POST",
cache: false,
url: "update.php",
data: "id1_text="+$name1,
data: "id2_text="+$name1,
data: "name1_text="+$name1,
data: "name2_text="+$name2,
data: "optA1_text="+$optA1,
data: "optA2_text="+$optA2,
data: "optB1_text="+$optB1,
data: "optB2_text="+$optB2,
data: "other_qual1_text="+$other_qual1,
data: "other_qual2_text="+$other_qual2,
data: "interview1_text="+$interview1,
data: "interview2_text="+$interview2,
data: "total1_text="+$total1,
data: "total2_text="+$total2,
success: function(data) {
alert('data has been updated!');
}
});
});
});
</script>
update.php code:
<?php
mysql_connect("localhost","root","") or die ("cant connect to database!");
mysql_select_db("test") or die ("cant find database!");
$id1=$_POST['id1_text'];
$id2=$_POST['id2_text'];
$name1=$_POST['name1_text'];
$name2=$_POST['name2_text'];
$optA1=$_POST['optA1_text'];
$optA2=$_POST['optA2_text'];
$optB1=$_POST['optB1_text'];
$optB2=$_POST['optB2_text'];
$other_qual1=$_POST['other_qual1_text'];
$other_qual2=$_POST['other_qual2_text'];
$interview1=$_POST['interview1_text'];
$interview2=$_POST['interview2_text'];
$total1=$_POST['total1_text'];
$total2=$_POST['total2_text'];
$query1=mysql_query("UPDATE score SET name='$name1', score1='$optA1', score2='optB1', other_qual='$other_qual1', interview='$interview1', total='$total1' WHERE id='$id1'");
$resource1 = mysql_query($query1)
or die (mysql_error());
$query2=mysql_query("UPDATE score SET name='$name2', score1='$optA2', score2='optB2', other_qual='$other_qual2', interview='$interview2', total='$total2' WHERE id='$id2'");
$resource2 = mysql_query($query2)
or die (mysql_error());
?>

You had quite a few errors in your code. For example, you had a wrong syntax when you assigned data in the JS-code. It should be in this form:
data: { val:$("#selector").val, val2:$("#selector2").val(), /* etc etc*/ }
Solution
Try this code:
JS
<script type="text/javascript">
$(document).ready(function () {
$('#updates').click(function (e) {
e.preventDefault();
var id1 = $('#id1').val();
var id2 = $('#id2').val();
var name1 = $('#name1').val();
var name2 = $('#name2').val();
var optA1 = $('#optA1').val();
var optA2 = $('#optA2').val();
var optB1 = $('#optB1').val();
var optB2 = $('#optB2').val();
var other_qual1 = $('#other_qual1').val();
var other_qual2 = $('#other_qual2').val();
var interview1 = $('#interview1').val();
var interview2 = $('#interview2').val();
var total1 = $('#total1').val();
var total2 = $('#total2').val();
$.ajax({
type: "POST",
cache: false,
url: "update.php",
data: {
id1_text: name1,
id2_text: name1,
name1_text: name1,
name2_text: name2,
optA1_text: optA1,
optA2_text: optA2,
optB1_text: optB1,
optB2_text: optB2,
other_qual1_text: other_qual1,
other_qual2_text: other_qual2,
interview1_text: interview1,
interview2_text: interview2,
total1_text: total1,
total2_text: total2
},
success: function (data) {
alert('data has been updated!');
}
});
});
});
</script>
PHP
<?php
if (isset($_POST['id1_text'])) {
mysql_connect("localhost", "root", "") or die("cant connect to database!");
mysql_select_db("test") or die("cant find database!");
$id1 = $_POST['id1_text'];
$id2 = $_POST['id2_text'];
$name1 = $_POST['name1_text'];
$name2 = $_POST['name2_text'];
$optA1 = $_POST['optA1_text'];
$optA2 = $_POST['optA2_text'];
$optB1 = $_POST['optB1_text'];
$optB2 = $_POST['optB2_text'];
$other_qual1 = $_POST['other_qual1_text'];
$other_qual2 = $_POST['other_qual2_text'];
$interview1 = $_POST['interview1_text'];
$interview2 = $_POST['interview2_text'];
$total1 = $_POST['total1_text'];
$total2 = $_POST['total2_text'];
$query1 = mysql_query("UPDATE score SET name=$name1, score1=$optA1, score2=$optB1, other_qual=$other_qual1, interview=$interview1, total=$total1 WHERE id=$id1");
$resource1 = mysql_query($query1) or die(mysql_error());
$query2 = mysql_query("UPDATE score SET name=$name2, score1=$optA2, score2=optB2, other_qual=$other_qual2, interview=$interview2, total=$total2 WHERE id=$id2");
$resource2 = mysql_query($query2) or die(mysql_error());
}
?>

Related

cant get concat values and php not working

So my problem is that why jquery concat is not working and also it is not posting and can't inserted into the database
I tried changing the code and read references still cant get enough
This is my jquery
var uid = $('#lname').val() + $('fname').val() + $('#datecreated').val(moment().format('YYYY'));
var datecreated = $('#datecreated').val(moment().format('YYYY'));
var fname = $('#fname').val();
var lname = $('#lname').val();
var email = $('#email').val();
var password = $('#pass').val();
var passcheck = false;
This is my ajax
if (uid && fname && lname && email && password && datecreated)
{
var form = $(this);
var formData = new FormData(this);
$(".formcontent").hide();
$.ajax({
url : form.attr('action'),
type: form.attr('method'),
data: form.serialize(),
data: formData,
dataType: 'json',
cache: false,
contentType: false,
processData: false,
success:function(response)
{
this is my full code php, I dunno if the problem is with xampp or not. Im tackling this problem for 3 day straight now and I dunno where the problem is
valid['success'] = array('success' => true, 'messages' => array());
$uid = $_POST ['uid'];
$pass = $_POST['pass'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$datecreated = $_POST['datecreated'];
if ($_POST)
{
if(true)
{
$sqlmail = "SELECT * FROM acc WHERE (email = '$email') AND acc_stat < 3";
$resmail = $connect->query($sqlmail);
if($resmail->num_rows > 0)
{
while($row = $resmail->fetch_array())
{
if($email === $row['email'])
{
$valid['messages'] = "Email address is already taken";
}
}
$valid['success'] = false;
$connect->close();
echo json_encode($valid);
}
else
{
$sql = "INSERT INTO 'acc' ('uid', 'password', 'lname', 'fname', 'email', 'acc_type', 'acc_stat','date_create') VALUES ('$uid', '$pass', '$fname', '$lname', '$email', '3', '1','$datecreated')";
if($connect->query($sql) === TRUE)
{
$valid['success'] = true;
$valid['messages'] = "Account registration successful.";
$connect->close();
echo json_encode($valid);
}
else
{
$valid['success'] = false;
$valid['messages'] = "Network connection not stable. Please try again later.";
$connect->close();
echo json_encode($valid);
}
}
}
else
{
$valid['success'] = false;
$valid['messages'] = "No internet connection.";
$connect->close();
echo json_encode($valid);
}
}
There is a small mistake in your code, please fix it and it should work fine. the error is at line
var uid = $('#lname').val() + $('fname').val() + $('#datecreated').val(moment().format('YYYY'));
chage it to var uid = $('#lname').val() + $('#fname').val() + $('#datecreated').val(moment().format('YYYY'));.
silly mistake of missing just #. one more thing, you will not receive the uid paramter in php side, just because you are not sending it with form. append it to FormData as give,
var uid = $('#lname').val() + $('#fname').val() + $('#datecreated').val(moment().format('YYYY'));.
var formData = new FormData(this);
formData.append('uid' , uid);
Now you will be able to recieve that uid parameter.
Try to use this:
var uid = $('#lname').val() + $('#fname').val() + $('#datecreated').val(moment().format('YYYY'));
var datecreated = $('#datecreated').val(moment().format('YYYY'));
var fname = $('#fname').val();
var lname = $('#lname').val();
var email = $('#email').val();
var password = $('#pass').val();
var passcheck = false;

why my file upload not copied on directory? (javascript, php)

please help
This my constructor in class
$this->covername = $image['cover']['name'];
$this->coverdir = $image['cover']['tmp_name'];
$this->covertype = pathinfo($this->covername, PATHINFO_EXTENSION);
This json script
prosesSave = function(saveas) {
document.getElementById("saveas").value = saveas;
var message = $('#message');
var fields = $("#form1,#form2,#form3");
var formdata = new FormData(fields[0]);
$.ajax({
type: "POST",
url: "project/proses-new-page.php",
data: formdata,
cache: false,
contentType : false,
processData : false,
dataType: 'json',
success: function(response) {
$(message).empty();
if(response.saveas){
if (saveas == 'Draft') {
var idmsg = $("<div></div>", {
"class":"callout callout-info"}).appendTo(message);
}else{
var idmsg = $("<div></div>", {
"class":"callout callout-success"}).appendTo(message);
}
}else{
var idmsg = $("<div></div>", {
"class":"callout callout-danger"}).appendTo(message);
}
$(idmsg).empty().html(response.html);
document.getElementById("status").value = "EditPage";
document.getElementById("idpage").value = response.idpage;
}
});
return false;}
This my process copy
if ($this->covername) {
$this->filename = $id."_".$this->date.".".$this->covertype;
$this->move = $this->dirupload."cover/".$this->filename;
if(!move_uploaded_file($this->coverdir, $this->move)){
$this->response_html = '<p>Faild</p>'.$this->covername."<br>".$this->coverdir."<br>".$this->move;
}else{
$this->response_html = '<p>Succes</p>'.$this->covername;
}
chmod($this->move, 0777);
mysqli_query($this->conn, "UPDATE tb_pages SET COVER_PAGE = '".$this->filename."' WHERE ID_PAGE = '".$id."'");
}
i have problem with my code. in this code name of image, type and so on can insert into database. but on the image can't copied in directory($this->coverdir).
sory for my bad english.

commands in chatroom & defining words after command

Okay basically I'm trying to have a action happen of alert('hi $message'); when a user enters the command /command lewis into the chatroom; In the alert I have stated the variable $message and this is the word followed by the command; for example /command $message. I have posted my script below; so basically what I'm trying to achieve is recognise when a user types /command followed by a $message into the textarea then perform an action.
Chatroom Javascript
name ='<? echo $chat_room_username; ?>';
$("#name-area").html("You are: <span>" + name + "</span>");
var chat = new Chat();
$(function() {
chat.getState();
// watch textarea for key presses
$("#sendie").keydown(function(event) {
var key = event.which;
//all keys including return.
if (key >= 33) {
var maxLength = $(this).attr("maxlength");
var length = this.value.length;
// don't allow new content if length is maxed out
if (length >= maxLength) {
event.preventDefault();
}
}
});
// watch textarea for release of key press
$('#sendie').keyup(function(e) {
if (e.keyCode == 13) {
var text = $(this).val();
var maxLength = $(this).attr("maxlength");
var length = text.length;
// send
if (length <= maxLength + 1) {
chat.send(text, name);
$(this).val("");
} else {
$(this).val(text.substring(0, maxLength));
}
}
});
});
var instanse = false;
var state;
var mes;
var file;
function Chat () {
this.update = updateChat;
this.send = sendChat;
this.getState = getStateOfChat;
}
//gets the state of the chat
function getStateOfChat(){
if(!instanse){
instanse = true;
$.ajax({
type: "POST",
url: "/rooms/process.php?room=<? echo $room; ?>",
data: {
'function': 'getState',
'file': file
},
dataType: "json",
success: function(data){
state = data.state;
instanse = false;
},
});
}
}
//Updates the chat
function updateChat(){
if(!instanse){
instanse = true;
$.ajax({
type: "POST",
url: "/rooms/process.php?room=<? echo $room; ?>",
data: {
'function': 'update',
'state': state,
'file': file
},
dataType: "json",
success: function(data){
if(data.text){
for (var i = 0; i < data.text.length; i++) {
var newdata = data.text[i].replace(/:brand/g,"<img src=\"/_img/logo1.png\"></img>");
newdata = newdata.replace(/:tipsound/g,"<audio autoplay><source src=\"/tip.wav\" type=\"audio/mpeg\"></audio>");
<?
$select_gifs = mysql_query("SELECT * FROM `submited_chatroom_gifs` WHERE `staff` = '1'");
while($gif = mysql_fetch_array($select_gifs)){
?>
newdata = newdata.replace(/:<? echo $gif['name']; ?>/g,"<img data-toggle=\"tooltip\" height=\"<? echo $gif['height']; ?>\" width=\"<? echo $gif['width']; ?>\"title=\":<? echo $gif['name']; ?>\" src=\"/_img/gifs/<? echo $gif['img']; ?>\"></img>");
<? } ?>
$('#chat-area').append($("<p>"+ newdata +"</p>"));
}
}
document.getElementById('chat-area').scrollTop = document.getElementById('chat-area').scrollHeight;
instanse = false;
state = data.state;
},
});
}
else {
setTimeout(updateChat, 1500);
}
}
//send the message
function sendChat(message, nickname)
{
updateChat();
$.ajax({
type: "POST",
url: "/rooms/process.php?room=<? echo $room; ?>",
data: {
'function': 'send',
'message': message,
'nickname': nickname,
'file': file
},
dataType: "json",
success: function(data){
updateChat();
},
});
}
process.php
<?php
$function = $_POST['function'];
$room = $_GET['room'];
$log = array();
switch($function) {
case('getState'):
if(file_exists($room . '.txt')){
$lines = file($room . '.txt');
}
$log['state'] = count($lines);
break;
case('update'):
$state = $_POST['state'];
if(file_exists($room . '.txt')){
$lines = file($room . '.txt');
}
$count = count($lines);
if($state == $count){
$log['state'] = $state;
$log['text'] = false;
}
else{
$text= array();
$log['state'] = $state + count($lines) - $state;
foreach ($lines as $line_num => $line)
{
if($line_num >= $state){
$text[] = $line = str_replace("\n", "", $line);
}
}
$log['text'] = $text;
}
break;
case('send'):
$nickname = $_POST['nickname'];
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
$message = htmlentities(strip_tags($_POST['message']));
if(($message) != "\n"){
if(preg_match($reg_exUrl, $message, $url)) {
$message = preg_replace($reg_exUrl, ''.$url[0].'', $message);
}
fwrite(fopen($room . '.txt', 'a'), "<p><font size=\"2px\">". $nickname . ": " . $message = str_replace("\n", " ", $message) . "</font></p>\n");
}
break;
}
echo json_encode($log);
}
?>
the alert is only for the person who wrote the command in
Thankyou for any help, and I apologise for the lengthy question.
[edit] Sorry just re-read my question and I will just try and explain what I'm trying to achieve in abit more detail. So basically when a user inputs /command lewis the script would then perform an alert('Hi Lewis');. But then if a user was to enter /command john the alert would be alert('Hi John');.
The alert would be instead of posting the message to the chatroom.

preventing Ajax from sending multiple times

i am using this script:
https://jsfiddle.net/b7wjj39p/9/
it is running 12 wheels randomly, then the results are bring sent via AJAX to php, who prints them out.
the problem is that the AJAX is being sent 12 times instead of only once.
this is the relevant code who sends the data:
function sendwinnertophp(){
var winner = $("#winner").html();
var winner_two = $("#winner222").html();
var winner_three = $("#winner333").html();
var winner_four = $("#winner444").html();
var winner_five = $("#winner555").html();
var winner_six = $("#winner666").html();
var winner_seven = $("#winner777").html();
var winner_ehight = $("#winner888").html();
var winner_nine = $("#winner999").html();
var winner_ten = $("#winner10").html();
var winner_eleven = $("#winner11").html();
var winner_twelve = $("#winner12").html();
$.ajax({
type: "POST",
url: "getwinner.php",
data: {
"winner": winner,
"winner_two" : winner_two,
"winner_three" : winner_three,
"winner_four" : winner_four,
"winner_five" : winner_five,
"winner_six" : winner_six,
"winner_seven" : winner_seven,
"winner_ehight" : winner_ehight,
"winner_nine" : winner_nine,
"winner_ten" : winner_ten,
"winner_eleven" : winner_eleven,
"winner_twelve" : winner_twelve,
},
cache: false,
success: function(data){
// alert(data);
},
error: function(err){
alert(err);
}
});
}
this is the php code that print the results to an html file:
$file = fopen("log.html", "a");
$ip=$_SERVER['REMOTE_ADDR'];
$winner = $_POST['winner'];
$winner_two = $_POST['winner_two'];
$winner_three = $_POST['winner_three'];
$winner_four = $_POST['winner_four'];
$winner_five = $_POST['winner_five'];
$winner_six = $_POST['winner_six'];
$winner_seven = $_POST['winner_seven'];
$winner_ehight = $_POST['winner_ehight'];
$winner_nine = $_POST['winner_nine'];
$winner_ten = $_POST['winner_ten'];
$winner_eleven = $_POST['winner_eleven'];
$winner_twelve = $_POST['winner_twelve'];
//write the data
$time = date("H:i dS F");
fwrite($file, "<b>Time:</b> $time<br/><b>ip:</b> $ip<br/><b>first:</b> $winner<br/><b>second:</b> $winner_two<br/><b>third:</b> $winner_three<br/><b>forth:</b> $winner_four<br/><b>fifth:</b> $winner_five<br/>
<b>sixth:</b> $winner_six<br/><b>seventh:</b> $winner_seven<br/><b>ehihth:</b> $winner_ehight<br/><b>ninth:</b> $winner_nine<br/><b>tenth:</b> $winner_ten<br/><b>eleventh:</b> $winner_eleven<br/><b>twelvth:</b> $winner_twelve<br/><hr/>" );
fclose( $file )

How to refresh a page section using ajax/json?

i am building a chat website, now i working on ONLINE USER.
I use AJAX to refresh but i meet some problems with Append.();.
When refreshing section the same data appear again and again...That is the PHP file.
<?php
public function online(){
$database = new DB();
$db = $database->database();
$array = array();
$ref = $_SESSION['oc_users_ref'];
$time_out = time()-5;
$time = time();
$query = $db->query("SELECT * FROM oc_users WHERE users_online = 0");
$updateOnline = $db->query("UPDATE oc_users SET users_online = 1 WHERE users_lastcome < '$time_out'");
$q = $db->query("SELECT * FROM oc_users WHERE users_ref <> '$ref' AND users_online = 0 ORDER BY users_lastcome DESC");
while($data = $q->fetch()) {
if($data['users_online'] == 1){
$class = "opacity30";
}else{
$class = '';
}
$array[] = $data;
}
print json_encode($array);
} ?>
Now the Javascript
function getOnline(){
$.ajax({
url: "conf/users.php?act=online",
dataType: "text",
success: function(data) {
var json = $.parseJSON(data);
for (var i=0;i<json.length;++i)
{
$('#printOnline').append('<li><a href="javascript:void(0);" onclick=javascript:chatWith("'+json[i].users_nickname+'")><i></i>'+json[i].users_nickname+'</a></li>');
}
}
});
}
setInterval('getOnline()',10000);
And the result*
User001
User001
User001
User001
User001
User001
User001
User001
Please help me...Great Thanks
Reset the user list before updating:
function getOnline(){
$.ajax({
url: "conf/users.php?act=online",
dataType: "text",
success: function(data) {
$('#printOnline').html("");
var json = $.parseJSON(data);
for (var i=0;i<json.length;++i)
{
$('#printOnline').append('<li><a href="javascript:void(0);" onclick=javascript:chatWith("'+json[i].users_nickname+'")><i></i>'+json[i].users_nickname+'</a></li>');
}
}
});
}
setInterval('getOnline()',10000);

Categories

Resources