1

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.

enter image description here

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());
?>
2
  • 1
    could you tell which portion of code if for what or describe your problem in little bit more detail. Commented Feb 2, 2014 at 21:13
  • @i_m_optional my code for updating wont work? can u help me?please Commented Feb 3, 2014 at 4:29

1 Answer 1

1

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());
}

?>
Sign up to request clarification or add additional context in comments.

3 Comments

kk il try it...sorry i fall asleep i didn't reply immediately.
y is it the alert in javascript says its updated but i look to the database its not updated.
the problem is on the query because in your code you put mysq_query on the $query1 and $query2 variable and put it again in the $resource variable i just need to remove it...and it worked now thanks :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.