Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Updating mysql Database with an input tag using php,ajax, and javascript
Old 03-31-2009, 05:56 AM Updating mysql Database with an input tag using php,ajax, and javascript
Junior Talker

Posts: 4
Name: Mayan Sneh
Trades: 0
Hi all,

I tired searching the entire forum and was unable to find the answer i was looking for so i decided to post a new thread, if a thread explaining this already exists than i'm sorry for not finding it and posting there.
any how my problem is that i don't manage the update part of this i have attached all the files i'm using to try get this to work. what happens is that i'm able to get all the variables that i want to use into the URL address using the ajax and javascript, but i don't seem to be able to update even though after checking the php file is access just not updated.

Main Page:

PHP Code:
<?php
    
function ConnectDB()
    {
        
$con=mysql_connect("localhost","xxxx","xxxx");
        if (!
$con)
        {
            die(
'Could not connect: ' mysql_error());
        }

        
mysql_select_db("test"$con);    
    }
?>

<?php
    
function CloseDB()
    {
        
$con=mysql_connect("localhost","xxxx","xxxx");
        
mysql_close($con);
    }
?>

<html>
<head>
<script src="selectuser.js"></script>
</head>
<body>

<form> 
Select a User:
<select name="users" onChange="showUser(this.value)">
<option value="1">Peter Griffin</option>
<option value="2">Lois Griffin</option>
<option value="3">Glenn Quagmire</option>
<option value="4">Joseph Swanson</option>
</select>
</form>

<table width="800" height="auto" border="1">
<?php
    ConnectDB
();
    
$result mysql_query("SELECT * FROM user ORDER BY id ASC");
    while(
$row mysql_fetch_array($result))
    {
    
?>
    <tr>
           <td>
            <a href="Commands/deleteAdminNavigation.php?cmd=delete&id=<?php echo $row['id']; ?>">
            Delete
            </a>
        </td>
        <td><input type="text" value="<?php echo $row['id']; ?>" /></td>
        <td><input type="text" value="<?php echo $row['FirstName']; ?>" onFocus="Old=this.value;" onChange="showUser(Old,this.value)" /></td>
        <td><input type="text" value="<?php echo $row['LastName']; ?>" /></td>
        <td><input type="text" value="<?php echo $row['Age']; ?>" /></td>
        <td><input type="text" value="<?php echo $row['Hometown']; ?>" /></td>
        <td><input type="text" value="<?php echo $row['Job']; ?>" /></td>
    </tr>
        <?php
        
}
        
CloseDB();
?>
</table>

<p>
<div id="txtHint"><b>User info will be listed here.</b></div>
</p>

</body>
</html>
Javascript Code:
Code:
var xmlHttp;

function showUser(str,str2)
{ 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request");
 return;
 }
 alert(str);
var url="getuser.php";
url=url+"?q="+str;
url=url+"&a="+str2;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function stateChanged() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
 document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
 } 
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 //Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}
PHP command page Updater page:
PHP Code:
<?php
$q
=$_GET['q'];
$new=$_GET['a'];

$con mysql_connect('localhost''xxxx''xxxx');
if (!
$con)
 {
 die(
'Could not connect: ' mysql_error());
 }

mysql_select_db("user"$con);

mysql_query("UPDATE user SET FirstName = $new
WHERE FirstName = 'test'"
);

echo 
"changed";
echo 
"<br />";
echo 
$q;
echo 
"<br />";
echo 
$new;

mysql_close($con);
?>
All help is highly appreciated thank you in advance
P.S. - Right now the code is set to update without new information that has been given by the user, this works fine to update but when i start involving variables its stops working. Really grew some new white hairs because of this bug, i have been busy on it for a couple of days now and must find a solution fast.
__________________
Mayan

Last edited by mayan.sneh; 03-31-2009 at 06:01 AM.. Reason: add some more breif to the message
mayan.sneh is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-31-2009, 08:50 AM Re: Updating mysql Database with an input tag using php,ajax, and javascript
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
The first thing that struck my eye is
PHP Code:
mysql_query("UPDATE user SET FirstName = $new
WHERE FirstName = 'test'"
); 
You are missing single quotes there.
The query should be
PHP Code:
mysql_query("UPDATE user SET FirstName = '$new'
WHERE FirstName = 'test'"
); 
How does it goes with this modification ?
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 03-31-2009, 11:20 PM Re: Updating mysql Database with an input tag using php,ajax, and javascript
Junior Talker

Posts: 4
Name: Mayan Sneh
Trades: 0
it changes it but what i really want to get at is for it to receive all the information for the update including table name and field name.
So that i would have something like this:
PHP Code:
mysql_query("UPDATE $Table SET $Field = '$new'
WHERE 
$Field = 'test'"); 
can this be done in any way cause as it is now and in many other ways that i tried it doesn't work. there is a way to by pass this but it is smarter to do like this rather than use a cmd stop with different cmd's in a switch case.

If someone can help it will be most appreciated.
thanks
__________________
Mayan

Last edited by mayan.sneh; 03-31-2009 at 11:21 PM.. Reason: change code
mayan.sneh is offline
Reply With Quote
View Public Profile
 
Old 03-31-2009, 11:57 PM Re: Updating mysql Database with an input tag using php,ajax, and javascript
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Well, you're calling the function showUser() with only one argument, but in the function you use both arguments, passing str2 (which is empty) to the $new variable. So $new will always be empty.
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 04-07-2009, 08:42 AM Problem Solved!
Junior Talker

Posts: 4
Name: Mayan Sneh
Trades: 0
Thanks all,

I have solved the problem and created an amazing option that shows which cell is selected and updates the admin when a cell was changed. if any one are looking for the same code let me know I'll send it you.
__________________
Mayan

Last edited by mayan.sneh; 04-07-2009 at 08:43 AM.. Reason: bad writing
mayan.sneh is offline
Reply With Quote
View Public Profile
 
Old 04-13-2009, 02:29 AM Re: Updating mysql Database with an input tag using php,ajax, and javascript
Junior Talker

Posts: 1
Name: PriyankaP
Trades: 0
Can you mail me the code @ priyankap@qualsoftservices.com?

I want to save the value of the 'select tag' in the database...

Can you please help me?

Thanks is Advance...
priyankap is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Updating mysql Database with an input tag using php,ajax, and javascript
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.41968 seconds with 12 queries