Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: How do i process the list and option field form element in CRUD with jQuery and PHP

  1. #1
    Join Date
    Jan 2012
    Posts
    52
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Question How do i process the list and option field form element in CRUD with jQuery and PHP

    Dear All,

    I got a tutorial http://www.codeofaninja.com/2013/05/...hp-jquery.html
    which gives a clear idea about CRUD operation with a simple user form using PHP Data Objects (PDO) extension.

    I added a select field and a option field. Then the
    insertion happens successfully. But the difficulty occurs on update.

    I Tried at many ways to update. But the update form only gets the id. and can't recognize the
    language field and the Read level field.

    How can i overcome this.

    The sample code are as follows-

    Update a Record :

    update_form.php contains
    Code:
     <?php
    session_start();
    try {
    	include 'libs/db_connect.php';
    //	include 'includes/queryfunctions.php';
    	include_once('includes/functions2.php');
    	//prepare query
    	$query = "select 
    				id, employeeid, language, readlevel
    			from 
    				language  
    			where 
    				id = ? 
    			limit 0,1";
    			
    	$stmt = $con->prepare( $query );
    
    	//this is the first question mark
    	$stmt->bindParam(1, $_REQUEST['user_id']);
    
    	//execute our query
    	if($stmt->execute()){
    	
    		//store retrieved row to a variable
    		$row = $stmt->fetch(PDO::FETCH_ASSOC);
    
    		//values to fill up our form
    		$id = $row['id'];
    		$employeeid = $row['employeeid'];
    		$language = $row['language'];
    		$readlevel = $row['readlevel'];
    
    		
    	}else{
    		echo "Unable to read record.";
    	}
    }
    
    //to handle error
    catch(PDOException $exception){
    	echo "Error: " . $exception->getMessage();
    }
    ?>
    <!--we have our html form here where new user information will be entered-->
    <form id='updateUserForm' action='#' method='post' border='0'>
        <table>
            <tr>
                <td>Employee ID</td>
                <td><input name="employeeid" type="text" id="employeeid" value="<?php echo $employeeid; ?>"/></td>
            </tr>
            <tr>
                <td>Language</td>
                <td>	  <select name="language" id="language">
            <option value="">--Select Language--</option>
            <option value="Bangla" <?php if($language->language=='Bangla') echo 'selected'?>>Bangla</option>
            <option value="English" <?php if($language->language=='English') echo 'selected'?>>English</option>
          </select>
    </td>
            </tr>
            <tr>
                <td>Read</td>
                <td>
              <label>
            <input type="radio" name="readlevel" value="Beginner" <?php if($language->readlevel=='Beginner') echo 'checked'?>>
      Beginner</label>
            <label>
            <input type="radio" name="readlevel" value="Intermediate" <?php if($language->readlevel=='Intermediate') echo 'checked'?>>
      Intermediate
            </label>
            <label>
            <input type="radio" name="readlevel" value="Advanced (Fluent)" <?php if($language->readlevel=='Advanced (Fluent)') echo 'checked'?>>
      Advanced (Fluent)
            </label>
                </td>
            </tr>
    
                <td></td>
                <td>
                    <!-- so that we could identify what record is to be updated -->
                    <input type="hidden" name="id" value=<?php echo $language->id; ?>>
                    <input type='submit' value='Update' class='customBtn' />
    				
                </td>
            </tr>
        </table>
    </form>
    
    update.php contains-
    
    <?php
    //include database connection
    include 'libs/db_connect.php';
    
    try{
    
    	//write query
    	//in this case, it seemed like we have so many fields to pass and 
    	//its kinda better if we'll label them and not use question marks
    	//like what we used here
    	$query = "update 
    				language 
    			set 
    				employeeid = :employeeid, 
    				language = :language, 
    				readlevel = :readlevel, 
    			where
    				id = :id";
    
    	//prepare query for excecution
    	$stmt = $con->prepare($query);
    
    	//bind the parameters
    	$stmt->bindParam(':employeeid', $_POST['employeeid']);
    	$stmt->bindParam(':language', $_POST['language']);
    	$stmt->bindParam(':readlevel', $_POST['readlevel']);
    
    	$stmt->bindParam(':id', $_POST['id']);
    
    	// Execute the query
    	if($stmt->execute()){
    		echo "Language was updated.";
    	}else{
    		echo "Unable to update language.";
    	}
    
    }
    
    
    //to handle error
    catch(PDOException $exception){
    	echo "Error: " . $exception->getMessage();
    }
    ?>
    Thanking You
    Megha
    Last edited by Beverleyh; 06-10-2013 at 11:43 AM. Reason: Formatting

  2. #2
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    2,009
    Thanks
    16
    Thanked 315 Times in 313 Posts
    Blog Entries
    13

    Default

    Please enclose code in:

    [CODE]
    code goes here
    [/C
    ODE]

    tags so it looks like so:

    Code:
    code goes here
    You may use the editor's # button (not the #i button) to generate the tags. Or just type them manually into the editor. If typing them, don't forget to use the square brackets and also to use the / in the closing tag. Type exactly as shown in the above example.
    Focus on Function Web Design | Latest News RSS | Facebook | Twitter |
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps |
    The only limit to creativity is imagination: JemCon.org

  3. The Following User Says Thank You to Beverleyh For This Useful Post:

    megha_3000 (06-11-2013)

  4. #3
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 517 Times in 503 Posts
    Blog Entries
    5

    Default

    couple things...

    What is happening? Are any exceptions thrown? Do you get other error messages? What is the output from your script?

    You're using $language as though it was an object. When you assign $language = $row['language'], it will actually be a string (or null).

    You might try dumping the contents of the $_POST array to see if they actually contain the values you expect.
    Last edited by traq; 06-10-2013 at 03:18 PM.

  5. #4
    Join Date
    Jan 2012
    Posts
    52
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default

    No i didn't get any error message.as i stated before it's finding the employeeid at the field of employeeid .
    But can't get the language at the select list field on update form. and didn't make the button checked which value is given.Also don't make any change in database after changing values. seems that values are not passing.
    i did a little bit change to check values at the update_form.php script i.e.

    Code:
    		//values to fill up our form
    		$id = $row['id'];
    		$employeeid = $row['employeeid'];
    		$language = $row['language'];
    		echo $language;
    		$readlevel = $row['readlevel'];
    		echo $readlevel;
    and i found that the values are showing.

  6. #5
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 517 Times in 503 Posts
    Blog Entries
    5

    Default

    Here, you assign $language to the value in $row['language']:
    PHP Code:
    $language $row['language']; 
    If your DB query was successful, that will be a string. If it failed, it will be null (and you will have an "undefined variable" notice).

    However, later on, you treat $language as though it were an object:
    PHP Code:
    $language->language 
    From the code you posted, I would expect that to give a warning about "trying to read member property of a non-object" or something similar.

    Do you have error reporting enabled?

  7. #6
    Join Date
    Jan 2012
    Posts
    52
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default

    Yes i have error reporting enabled.i used error_reporting(E_ALL & ~E_NOTICE); at first of the script. Still it doesn't show any error. but in case of
    Code:
    $language->language
    I used a function which is
    Code:
    function populate_select($table,$fields_id,$fields_value,$selected){
    	$conn=db_connect(HOST,USER,PASS,DB,PORT);
    	$sql="Select $fields_id,$fields_value From $table Order By $fields_value";
    	$results=query($sql,$conn);
    	while ($row = fetch_object($results)){
    		$SelectedField=($row->$fields_id==$selected) ? " selected" : "";		
    		echo "<option value='" . $row->$fields_id ."'". $SelectedField . ">" . $row->$fields_value . "</option>";
    	}
    	free_result($results);
    }
    This function is called at the try block.and this function gets the table name as $language and put their value as language.
    The attached image shows what the update script actually looks. it gets only employee id but doesn't get the value of select field and option field.
    Click image for larger version. 

Name:	edit_view.png 
Views:	93 
Size:	6.3 KB 
ID:	5091
    Last edited by megha_3000; 06-12-2013 at 05:02 AM.

  8. #7
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 517 Times in 503 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by megha_3000 View Post
    Yes i have error reporting enabled.i used error_reporting(E_ALL & ~E_NOTICE); at first of the script. Still it doesn't show any error.
    you should be using E_ALL | E_STRICT instead. ~ is bitwise not - it means "do NOT show Notices" - and, in this case, Notices are the errors you need to see most. Alternatively, you can do error_reporting( -1 ), which will show every possible error.

    Quote Originally Posted by megha_3000 View Post
    in case of

    $language->language

    I used a function which is
    PHP Code:
    function populate_select($table,$fields_id,$fields_value,$selected){
        
    $conn=db_connect(HOST,USER,PASS,DB,PORT);
        
    $sql="Select $fields_id,$fields_value From $table Order By $fields_value";
        
    $results=query($sql,$conn);
        while (
    $row fetch_object($results)){
            
    $SelectedField=($row->$fields_id==$selected) ? " selected" "";        
            echo 
    "<option value='" $row->$fields_id ."'"$SelectedField ">" $row->$fields_value "</option>";
        }
        
    free_result($results);

    This function is called at the try block.and this function gets the table name as $language and put their value as language.
    No; first, this function does nothing with your $langauge variable (it does not even appear to use a value like the one $language holds - it expects a table name and three column names). It does not "get a language" - it selects arbitrary fields from a table in the DB and prints their values in an <option> element. (Perhaps you're using it to print a select list of available languages from the DB...? but this has nothing to do with the variable we're discussing.)

    Second, it does not return any value, so it cannot be used to assign a new value to $langauge. All the data the function retrieves is lost after the function completes.

    Third, it is not used anywhere in the code in question anyway.

    Look at this section of the code you posted. It starts by assigning $language to the value of a field retrieved from the DB. As I said earlier, this value is almost certainly a string. Unless re-assigned, it will _never_ be an object.

    From this point, you start writing an HTML form. You do nothing else with $language until you start writing the <select>, at which point, you're treating it like it is an object (trying to access $language->language). Now, at this point, $language might hold the value "Bangla", but $language->language does not even exist.
    PHP Code:
    <?php
    /* snip */
            
    $language $row['language'];
            
    $readlevel $row['readlevel'];
        }else{
            echo 
    "Unable to read record.";
        }
    }
    //to handle error
    catch(PDOException $exception){
        echo 
    "Error: " $exception->getMessage();
    }
    ?>
    <!--we have our html form here where new user information will be entered-->
    <form id='updateUserForm' action='#' method='post' border='0'>
        <table>
            <tr>
                <td>Employee ID</td>
                <td><input name="employeeid" type="text" id="employeeid" value="<?php echo $employeeid?>"/></td>
            </tr>
            <tr>
                <td>Language</td>
                <td>      <select name="language" id="language">
            <option value="">--Select Language--</option>
            <option value="Bangla" <?php if($language->language=='Bangla')
    /* snip */
    Is the code you posted the actual code you are using? Have you removed any parts you thought were not relevant?
    Last edited by traq; 06-12-2013 at 05:56 AM.

  9. The Following User Says Thank You to traq For This Useful Post:

    megha_3000 (06-12-2013)

  10. #8
    Join Date
    Jan 2012
    Posts
    52
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default

    Jazak ALLAHU Khair (May ALLAH give you best reward in feedback). Thank You. Alhamdulillah (All Praise goes to ALLAH).
    I did changes as you recommended. After that the edit form works fine Click image for larger version. 

Name:	new_edit_view.png 
Views:	119 
Size:	6.5 KB 
ID:	5092.

    But update is not happening!
    Last edited by megha_3000; 06-12-2013 at 12:08 PM.

  11. #9
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 517 Times in 503 Posts
    Blog Entries
    5

    Default

    I didn't suggest any changes; I was just trying to understand your problem. But I'm happy to hear it works now!

    I will look at your update code later today (I'm out of the office right now).

  12. #10
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 517 Times in 503 Posts
    Blog Entries
    5

    Default

    Alright: are you talking about this code now?
    PHP Code:
    <?php
    //include database connection
    include 'libs/db_connect.php';

    try{

        
    //write query
        //in this case, it seemed like we have so many fields to pass and 
        //its kinda better if we'll label them and not use question marks
        //like what we used here
        
    $query "update 
                    language 
                set 
                    employeeid = :employeeid, 
                    language = :language, 
                    readlevel = :readlevel, 
                where
                    id = :id"
    ;

        
    //prepare query for excecution
        
    $stmt $con->prepare($query);

        
    //bind the parameters
        
    $stmt->bindParam(':employeeid'$_POST['employeeid']);
        
    $stmt->bindParam(':language'$_POST['language']);
        
    $stmt->bindParam(':readlevel'$_POST['readlevel']);

        
    $stmt->bindParam(':id'$_POST['id']);

        
    // Execute the query
        
    if($stmt->execute()){
            echo 
    "Language was updated.";
        }else{
            echo 
    "Unable to update language.";
        }

    }


    //to handle error
    catch(PDOException $exception){
        echo 
    "Error: " $exception->getMessage();
    }
    ?>
    First thing I'd ask is if you're sure you even get to this page. Your form's action is set as "#", meaning the form will submit to the current page (which you identify as update_form.php), not the page which contains this code (which you identify as update.php).

  13. The Following User Says Thank You to traq For This Useful Post:

    megha_3000 (06-13-2013)

Similar Threads

  1. making a field read only after running process
    By megha_3000 in forum JavaScript
    Replies: 1
    Last Post: 07-30-2012, 10:30 AM
  2. Replies: 1
    Last Post: 08-09-2011, 06:29 AM
  3. Is it possible to process my form in two ways?
    By theemorgandorfer in forum ASP
    Replies: 3
    Last Post: 06-10-2010, 07:27 PM
  4. Date and time stamp in form field box on a form field page
    By Embryo in forum Looking for such a script or service
    Replies: 1
    Last Post: 05-10-2008, 04:37 PM
  5. Replies: 1
    Last Post: 01-11-2008, 04:09 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •