1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Edit field value with PHP and Ajax

Discussion in 'PHP' started by Gurbrinder, Apr 11, 2013.

  1. #1
    I'm trying to edit the value of field using Ajax and Php. Code is working fine but i want how can i perform this only with one function and one php file.
    profile.php

    <html>
    <head>
    <title></title>
    <link href="default.css" rel="stylesheet" type="text/css" media="all" />
    <link href="profile.css" rel="stylesheet" type="text/css" />
    <script src="javascript/java.js" type="text/javascript"></script>
    <script type="text/javascript">
    function AjaxName(str)
    {
     
    var xmlhttp=new XMLHttpRequest();
     
    xmlhttp.open("POST","editprofile.php?id="+str,true);
     
    xmlhttp.send();
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 )
        {
        document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
        }
      }
     
    }
    function AjaxFather(str)
    {
     
    var xmlhttp=new XMLHttpRequest();
     
    xmlhttp.open("POST","editfather.php?id="+str,true);
     
    xmlhttp.send();
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 )
        {
        document.getElementById("txtFather").innerHTML=xmlhttp.responseText;
        }
      }
     
    }
     
    function AjaxClass(str)
    {
    xmlhttp=new XMLHttpRequest();
    xmlhttp.open('POST','editclass.php?id='+str,true);
    xmlhttp.send();
     
    xmlhttp.onreadystatechange=function()
    {
    if(xmlhttp.readyState == 4)
        {
        document.getElementById('txtClass').innerHTML=xmlhttp.responseText;
        }
    }
    }
    </script>
    </head>
    <body>
    <?php
    include 'html/header.php';
     
    ?>
    <div id="page">
            <div id="content">
    <?php
            $db_host = "localhost";
        $db_user = "root";
        $db_password = "";
        $db_name = "registration";
     
        $con = mysql_connect($db_host, $db_user , $db_password) or die ('Can\'t connect to mysql server'. mysql_error());
        mysql_select_db($db_name) or die ('Database is not selected');
     
    $email_id = $_SESSION['email'];
    $sql = "select * from student_register where email_id = '$email_id'";
    $result = mysql_query($sql);
    $row = mysql_fetch_array($result);
    echo "<div id='name'>Name : ";
    echo "<a onclick='AjaxName(". $row[0] . ")' href='#'><img src='images/edit.gif' /></a></div>";
    echo $row[1];
    echo "<div id='txtHint'></div><br />";
     
    echo "<div id='name'>Father Name : ";
    echo "<a onclick='AjaxFather(". $row[0] . ")' href='#'><img src='images/edit.gif' /></a></div>";
    echo $row[2];
    echo "<div id='txtFather'></div><br />";
     
    echo "<div id='name'>Class :";
    echo "<a href='#' onclick='AjaxClass(" . $row[0] .")'><img src='images/edit.gif' alt='Edit' title='Edit'/></a></div>";
    echo $row[3];
    echo "<div id='txtClass'></div>";
    ?>
            </div>
     
    <?php
    include 'html/sidebar.php';
    include 'html/footer.php';
    ?>
    </body>
    </html>
    HTML:
    editprofile.php

    <?php
    $student_id = $_GET['id'];         
    $db_host = "localhost";
    $db_user = "root";
    $db_password = "";
    $db_name = "registration";
     
    $con = mysql_connect($db_host, $db_user , $db_password) or die ('Can\'t connect to mysql server'. mysql_error());
    mysql_select_db($db_name) or die ('Database is not selected');
     
        $sql = "select * from student_register where student_id = '$student_id'";
     
        $result = mysql_query($sql);
     
        $row=mysql_fetch_array($result);
     
        echo "<form action='editname.php?edit_id=" . $row[0] . "' method='POST' >";
     
        echo "<input type='text' value='" . $row[1] . "' name='edit_name' />";
     
     
        echo "<input type='submit' value='Save' />";
        echo "</form>";
     
        ?>
    PHP:
    Similar to editprofile.php, I have created editfather.php and editclass.php. Instead of creating 3 AjaxFunction i.e. AjaxName(), AjaxFather(), AjaxClass() and their corresponding 3 php files editprofile.php , editfather.php, editclass.php, I want to create single AjaxFunction() and single editprofile.php that should execute particular code depending upon which edit link/img i have clicked.
     
    Solved! View solution.
    Gurbrinder, Apr 11, 2013 IP
  2. ChiragKalani

    ChiragKalani Active Member

    Messages:
    41
    Likes Received:
    2
    Best Answers:
    3
    Trophy Points:
    73
    #2
    Hi Gurbrinder,
    You can achieve this by passing the name parameter in ajax call.
     
    ChiragKalani, Apr 11, 2013 IP
  3. Gurbrinder

    Gurbrinder Member

    Messages:
    48
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #3
    Thanks Chirag , but i have already passed one parameter in ajax call because i have called the ajax function with only one parameter.

    Can you post the code that what you are saying? i feel like i haven't got your point.
     
    Gurbrinder, Apr 12, 2013 IP
  4. ChiragKalani

    ChiragKalani Active Member

    Messages:
    41
    Likes Received:
    2
    Best Answers:
    3
    Trophy Points:
    73
    #4
    As per my understanding, You code is passing the student id as parameter.
    I am suggesting to also pass the field that you editing. Suppose you are editing name than pass "name" as your parameter. "name" would be your field name in DB. So that, in file which you call through ajax will have two parameter one is student id and second is what you want to edit means name, father, class anything. You also have to pass the value that is enter by User while editing.

    Let me know is any confusion. Or can you can provide me your code, I will sort it our and let you know.

    Thanks,
    Chirag
     
    ChiragKalani, Apr 12, 2013 IP
  5. annaharris

    annaharris Active Member

    Messages:
    119
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    51
    #5
    How this could be beneficial by using one PHP file and one function in AJAX.
     
    annaharris, Apr 12, 2013 IP
  6. #6
    Here is the updated code

    stu_profile.php

    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Student Registration</title>
    <meta name="keywords" content="" />
    <meta name="description" content="" />
    <link href="http://fonts.googleapis.com/css?family=Oswald" rel="stylesheet" type="text/css" />
    <link href="default.css" rel="stylesheet" type="text/css" media="all" />
    <link href="profile.css" rel="stylesheet" type="text/css" />
    <!--[if IE 6]>
    <link href="default_ie6.css" rel="stylesheet" type="text/css" />
    <![endif]-->
    <script src="javascript/java.js" type="text/javascript"></script>
    <script type="text/javascript">
    function AjaxName(str,field)
    {
    var xmlhttp=new XMLHttpRequest();
     
    xmlhttp.open("POST","editprofile.php?id="+str+"&field="+field,true);
     
    xmlhttp.send();
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 )
        {
        document.getElementById(field+"_div").innerHTML=xmlhttp.responseText;
        }
      }
    }
    </script>
    </head>
    <body>
    <?php
    include 'html/header.php';
     
    ?>
    <div id="page">
            <div id="content">
    <?php
    include_once 'conn.php';
    $email_id = "abc@gmail.com";
    $sql = "select * from student_register where email_id = '$email_id'";
    $result = mysql_query($sql);
    print_r($result);
    $row = mysql_fetch_array($result);
    echo "<div id='name'>Name : ";
    echo '<a onclick="AjaxName(\''. $row[0] . '\',\'student_name\')" href="#"><img src="images/edit.gif" /></a></div>';
    echo $row[1];
    echo "<div id='student_name_div'></div><br />";
     
    echo "<div id='name'>Father Name : ";
    echo '<a onclick="AjaxName(\''. $row[0] . '\',\'father_name\')" href="#"><img src="images/edit.gif" /></a></div>';
    echo $row[2];
    echo "<div id='father_name_div'></div><br />";
     
    echo "<div id='name'>Class :";
    echo '<a onclick="AjaxName(\''. $row[0] . '\',\'class\')" href="#"><img src="images/edit.gif" /></a></div>';
    echo $row[3];
    echo "<div id='class_div'></div>";
    ?>
            </div>
       
    <?php
    include 'html/sidebar.php';
    include 'html/footer.php';
    ?>
    </body>
    </html>
    PHP:
    editprofile.php

    This file is for getting ajax form

    <?php
    $student_id = $_GET['id'];
    $field = $_GET['field'];
    include 'conn.php';
     
    $sql = "select * from student_register where student_id = '$student_id'";
    //$count_sql = "select count(student_id) from student_register";
    //$count_query = mysql_query($count_sql);
     
    $result = mysql_query($sql);
     
    //list($count)=mysql_fetch_array($count_query);
     
    $row=mysql_fetch_assoc($result);
    //echo $row[0];
    //echo $name;
    //exit();
     
    echo "<form action='editname.php?edit_id=" . $row['student_id'] . "' method='POST' >";
     
    echo "<input type='text' value='" . $row[$field] . "' name='edit_name' />";
    echo "<input type='hidden' value='" . $field . "' name='field' />";
     
    echo "<input type='submit' value='Save' />";
    echo "</form>";
     
    ?>
    PHP:
    editname.php

    This will update the database.

    <?php
    $edit_name = $_POST['edit_name'];
    $edit_id = $_GET['edit_id'];
    $field = $_POST['field'];
     
     
    include 'conn.php';
     
    $sql = "update student_register set $field = '$edit_name' where student_id = '$edit_id'";
    if(mysql_query($sql))
    {
    header('location:stu_profile.php');
    }
     
    ?>
    PHP:
     
    ChiragKalani, Apr 12, 2013 IP
  7. ChiragKalani

    ChiragKalani Active Member

    Messages:
    41
    Likes Received:
    2
    Best Answers:
    3
    Trophy Points:
    73
    #7

    This can be beneficial when you come to update the same page after 3-4 months. You will find every thing on one page. you do not have to go different pages and change again and again.

    Also making more file increase redundancy of code which is never advisable.

    Hope that clears your confusion... :)
     
    ChiragKalani, Apr 12, 2013 IP
  8. DJO-EL

    DJO-EL Member

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #8
    Hello,
    I wanna know if you guys could please help me with something a little trivial?? I am blushing here, lol. But I don't know much about programming etc... but I am more than willing to learn. Sooo, Im getting ready to launch an online classifieds website, for car sales only. I am thinking about using OSCLASS free PHP scripts for the trial, than update it after 3 months? depending on how it goes. So firstly, could you please guys tell me what you think of those scripts overall? And also, if I want to modify few stuff like the color, layout, would you mind telling me how to do it through the Html please? I mean, once I have the script of course. Guess I will have to copy/paste it here? And Im thinking about going with Blue Host as hosting provider...

    Please do let me know.

    Thnx so much.
    Jay
     
    DJO-EL, Apr 22, 2013 IP