0

I ve been searching through other topics to find a solution for that but nothing . I have form and i want to update the form input values when update button is clicked . The code that i using in Ajax is

$("#updateit").click(function() {

    var surname = $("#search_text").val();
    var name = $("#name").val();
    var company_name = $("#company_name").val();
    var firm = $("#firm").val();
    var address = $("#address").val();
    var town = $("#town").val();
    var tk = $("#tk").val();
    var country = $("#country").val();
    var telephone = $("#telephone").val();
    var fax = $("#fax").val();
    var mobile = $("#mobile").val();
    var web_site = $("#web_site").val();
    var visitors = $("#visitors").val();
    var id = $("#id").val();
       $.ajax({
            url: 'update1.php',
            type: 'POST',
       data: {surname:'surname',name:'name',company_name:'company_name',firm:'firm',address:'address',town:'town',tk:'tk',country:'country',telephone:'telephone',fax:'fax',mobile:'mobile',mail:'mail',web_site:'web_site',visitors:'visitors',id:'id'} , 
dataType:'html',   
            success: function(data)
                        {           
alert(data);                        
                        }
        });
    });

The PHP file for updating values is this :

<?php
require('db.php');
include("auth.php");
date_default_timezone_set('Europe/Athens');
$id=$_POST['id'];
$surname =$_POST['surname'];
$name= $_POST['name'];
$company_name=$_POST['company_name'];
$firm= $_POST['firm'];
$address= $_POST['address'];
$town= $_POST['town'];
$tk= $_POST['tk'];
$country= $_POST['country'];
$telephone= $_POST['telephone'];
$fax= $_POST['fax'];
$mobile= $_POST['mobile'];
$mail= $_POST['mail'];
$web_site= $_POST['web_site'];
$visitors= $_POST['visitors'];

$update="update base set surname='".$surname."', name='".$name."',company_name='".$company_name."',firm='".$firm."',address='".$address."',town='".$town."',tk='".$tk."',country='".$country."',telephone='".$telephone."',fax='".$fax."',mobile='".$mobile."',web_site='".$web_site."',visitors='".$visitors."' where id='".$id."'";
mysql_query($update) or die(mysql_error());
$status = "Record Updated Successfully. </br></br><a href='view.php'>View Updated Record</a>";
echo '<p style="color:#FF0000;">'.$status.'</p>';

?>

Any Help appreciated

Aristain
  • 11
  • 6
  • Tip: use serialize() https://api.jquery.com/serialize/ – Salines May 07 '16 at 13:24
  • What is error in console ? Did you try print_r($_POST); in your php file ? – Pardeep Poria May 07 '16 at 13:25
  • The mysql_ interface functions are deprecated. New development should use either mysqli or PDO. The PHP code appears to be vulnerable to SQL Injection. Potentially unsafe values included into the SQL text must be properly escaped. A better pattern is to use *prepared statements* with *bind placeholders*. – spencer7593 May 07 '16 at 13:36
  • print_r is getting these results [surname] => surname [name] => name [company_name] => company_name [firm] => firm [address] => address [town] => town [tk] => tk [country] => country [telephone] => telephone [fax] => fax [mobile] => mobile [mail] => mail [web_site] => web_site [visitors] => visitors [id] => id – Aristain May 07 '16 at 13:42

2 Answers2

2

the data sent through ajax is not correctly formed. It should be:

var data = {'surname':surname,'name':name,'company_name':company_name .....}

rather than surname: 'surname'

CY_
  • 7,170
  • 1
  • 15
  • 26
  • nope nothing it doesnt even return the alert(data) in ajax when before did – Aristain May 07 '16 at 13:33
  • you may follow these debugging steps: 1. use console.log to print out all variables captured from each dom. 2. add error: function(data){} and done: function(data){} to ajax 3. change the dataType from 'html' to 'text' for debugging 4.in PHP, use echo or var_dump to test all variables. – CY_ May 07 '16 at 13:37
1

The problem is because of the following line in your AJAX request,

data: {surname:'surname',name:'name',company_name:'company_name',firm:'firm',address:'address',town:'town',tk:'tk',country:'country',telephone:'telephone',fax:'fax',mobile:'mobile',mail:'mail',web_site:'web_site',visitors:'visitors',id:'id'}

you're actually sending strings like surname, company_name etc. rather than the value of the variables. So remove those single quotes. It should be,

data: {surname:surname,name:name,company_name:company_name,firm:firm,address:address,town:town,tk:tk,country:country,telephone:telephone,fax:fax,mobile:mobile,mail:mail,web_site:web_site,visitors:visitors,id:id}

Sidenote: Don't use mysql_ database extensions, they were deprecated in PHP 5.5.0 and are removed altogether in PHP 7.0.0. Use mysqli or PDO extensions instead. And this is why you shouldn't use mysql_ functions.

Community
  • 1
  • 1
Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37
  • nope nothing it doesnt even return the alert(data) in ajax when before did – Aristain May 07 '16 at 13:38
  • @Aristain *hmm*, that's because `mail` is not defined. See this line, `data: {..., mail:mail, ...}` in your AJAX request. You should have a variable named `mail`, like this: `var mail = $("#mail").val();`. – Rajdeep Paul May 07 '16 at 13:47