9 Replies - 12024 Views - Last Post: 09 April 2006 - 01:26 AM

#1 gayatri  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 62
  • Joined: 13-February 06

php+mysql+javascript

Posted 06 March 2006 - 11:56 PM

hi guys,
i need some way out for this problem, plz help
i have a form which has one select field and rest text fields, select field has some names available in database,
so i need to design code such that,
if i select some name from select box, rest text fields which contain details of the name say address, contact info etc should get filled up as soon as i select name
and if i don't choose anything from select, i will just fill up text boxes
what i have done is, established a form, in php and connected it to mysql database for select name options
n what's remained is javascript part,
i wanna know how can i write javascript code so that, if i select some name from select box, it would go n search my mysql databse for details of that name?
how to establish connection between mysql and javascript?
or should run query in php and pass on results to javascript

any help would be gr8ly appreciated
i would be much thankful if u provide some web based tutorial for php+mysql+javascript
thanx

This post has been edited by gayatri: 06 March 2006 - 11:59 PM


Is This A Good Question/Topic? 0
  • +

Replies To: php+mysql+javascript

#2 snoj  Icon User is offline

  • Married Life
  • member icon

Reputation: 84
  • View blog
  • Posts: 3,564
  • Joined: 31-March 03

Re: php+mysql+javascript

Posted 07 March 2006 - 12:37 AM

As Javasript is client side it is advisable to not connect to a database with it.
Was This Post Helpful? 0
  • +
  • -

#3 gayatri  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 62
  • Joined: 13-February 06

Re: php+mysql+javascript

Posted 07 March 2006 - 12:55 AM

so is there any other way out to solve this?
i am trying to get data from mysql with the help of php and i hope i can transfer this to javascipt,
just giving a try! :( :(

This post has been edited by gayatri: 07 March 2006 - 12:58 AM

Was This Post Helpful? 0
  • +
  • -

#4 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1875
  • View blog
  • Posts: 20,282
  • Joined: 17-March 01

Re: php+mysql+javascript

Posted 07 March 2006 - 01:05 AM

You'll want to use AJAX and PHP. Just google "AJAX PHP Forms" and you should find a wealth of information. I've not personally played to much with AJAX but I've seen it's power. Post back here if you need help.
Was This Post Helpful? 0
  • +
  • -

#5 gayatri  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 62
  • Joined: 13-February 06

Re: php+mysql+javascript

Posted 07 March 2006 - 01:10 AM

thanx a lot for ur help guys
will revert back if i find some useful info which would help others regarding this
and even then if i come across any problem
thanx again
will start search now :P
Was This Post Helpful? 0
  • +
  • -

#6 kaushiks  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 07-March 06

Re: php+mysql+javascript

Posted 07 March 2006 - 06:01 AM

gayatri, on 7 Mar, 2006 - 12:53 AM, said:

hi guys,
i need some way out for this problem, plz help
i have a form which has one select field and rest text fields, select field has some names available in database,
so i need to design code such that,
if i select some name from select box, rest text fields which contain details of the name say address, contact info etc should get filled up as soon as i select name
and if i don't choose anything from select, i will just fill up text boxes
what i have done is, established a form, in php and connected it to mysql database for select name options
n what's remained is javascript part,
i wanna know how can i write javascript code so that, if i select some name from select box, it would go n search my mysql databse for details of that name?
how to establish connection between mysql and javascript?
or should run query in php and pass on results to javascript

any help would be gr8ly appreciated
i would be much thankful if u provide some web based tutorial for php+mysql+javascript
thanx

hi,


for getting the existing information to fill up the text boxes u just have to write a Javascript as written below


function load(form)





{



var val=form.subcat.options[form.subcat.options.selectedIndex].value;



self.location='form.php?subcat=' + val ;



}


[PHP Code]<for the select option text box>
<?PHP

if(isset($subcat) and strlen($subcat) > 0){

$query4=mysql_query("SELECT DISTINCT * FROM databse table where id=$subcat ");

$row1=mysql_fetch_assoc($query4);

}else{$query4=mysql_query("SELECT DISTINCT * FROM databsetable");

}

?>


<?
echo "<select name='subcat' onchange=\"load(this.form)\">";
if($row1['boat']!=" ")
{
print "<option value=$row1[id]>$row1[boat]</option>";
}
echo "<option>Select one</option>";
while($rownew=mysql_fetch_array($quer11res))
{
print $rownew['boat'];
print "<br>";
print "<option value=$rownew[id]>$rownew[boatr]</option>";
}
/*while($noticia2 = mysql_fetch_array($query6)) {
if($noticia2['id']==@$subcat)
{echo "<option selected value='$noticia2[id]'>$noticia2[boat]</option>"."<BR>";}
else{echo "<option value='$noticia2[id]'>$noticia2[boat]</option>";}
}
echo "</select>";*/
?>

after that for filling the text boxes with the existing information :

<input name="boatstatus" type="text" id="boatstatus"
value="<?PHP echo($row1['boatstatus']); ?>" size="20"/>



this is the whole coding need to be done for getting the existing info from the database and filling it in the textboxes, for the entry selected from the select option.


please change in the names because they are not generic.hope it works for you



regards,
kaushik
Was This Post Helpful? 0
  • +
  • -

#7 gayatri  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 62
  • Joined: 13-February 06

Re: php+mysql+javascript

Posted 07 March 2006 - 06:42 AM

thanx buddy
will try that out and post again my progress
thanx again 4 help
Was This Post Helpful? 0
  • +
  • -

#8 gayatri  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 62
  • Joined: 13-February 06

Re: php+mysql+javascript

Posted 08 March 2006 - 12:27 AM

hi kaushik
what i haven't understood from the code is

while($rownew=mysql_fetch_array($quer11res))
{
print $rownew['boat'];
print "<br>";
print "<option value=$rownew[id]>$rownew[boatr]</option>";
}

and in my form option value from select box is not getting set
i am trying that

This post has been edited by gayatri: 08 March 2006 - 12:28 AM

Was This Post Helpful? 0
  • +
  • -

#9 gayatri  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 62
  • Joined: 13-February 06

Re: php+mysql+javascript

Posted 09 March 2006 - 03:48 AM

skyhawk133, on 7 Mar, 2006 - 02:02 AM, said:

You'll want to use AJAX and PHP. Just google "AJAX PHP Forms" and you should find a wealth of information. I've not personally played to much with AJAX but I've seen it's power. Post back here if you need help.

thanx as i found ur post useful, and i have given a try for ajax
got started with it,
but i got solution to my problem and it's with
php+mysql and js

i have posted it here
http://forums.dreami...t=0#entry146922

i thought i would post some useful url's here for beginners in ajax
but will keep them n post if someone asks for that

thanx again

GaYaTrI
Was This Post Helpful? 0
  • +
  • -

#10 wzeller  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 78
  • Joined: 09-April 06

Re: php+mysql+javascript

Posted 09 April 2006 - 01:26 AM

gayatri, on 9 Mar, 2006 - 02:40 AM, said:

thanx as i found ur post useful, and i have given a try for ajax
got started with it, but i got solution to my problem and it's with
php+mysql and js

A couple months ago I had exactly the same challenge, and it took a lot of searching (and posting to help forums) to get the answers I needed.

To help the next person, here's step-by-step of how I met the challenge. (I figure this is a good thread to put it in, since this thread came up when I did an Ajax search and already talks about exactly what this solution pertains to.)

In my case, I was creating a form that would submit shipping requests to our warehouse. Sometimes they would be orders to send products to an individual, in which case the address and contact information would be unique and therefore entered manually. Other times, they would be orders to send to our retail outlets, who information was stored in the database. So I wanted it to populate the form if a store was picked from a dropdown list, but still allow manual entry and editing of the data shown.

This is the web page, with all my data validation scripting and other irrelevant parts, stripped out:

<html>
<head>

<script language="Javascript">

var xmlHttp;

function createXMLHttpRequest() {
        if (window.ActiveXObject) {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        else if (window.XMLHttpRequest) {
                xmlHttp = new XMLHttpRequest();
        }
}

function startRequest(method, URL) {
        createXMLHttpRequest();
        xmlHttp.onreadystatechange = handleStateChange;
        xmlHttp.open(method, URL, true);
        xmlHttp.send(null);
}

function handleStateChange() {
        if (xmlHttp.readyState == 4) {
                if (xmlHttp.status == 200) {
                        processResponse(xmlHttp.responseText);
                }
        }
}

function processResponse(response) {
        // set your html form from the results here
        // If the response isn't null, set all the field values,
        // Otherwise, blank them out for manual entry.

        // These are all the settable fields in my form. Obviously, change 
        // them to the names of the fields in your form. (And, unless the 
        // name of your form happens to be "fulfill", change that too.

        if (response != "null") {
              var myObject = eval('(' + response + ')');
              document.forms.fulfill.phone.value=myObject.phone;
              document.forms.fulfill.zipcode.value=myObject.zipcode;
              document.forms.fulfill.state.value=myObject.state;
              document.forms.fulfill.city.value=myObject.city;
              document.forms.fulfill.address.value=myObject.address;
              document.forms.fulfill.country.value=myObject.country;
              document.forms.fulfill.tofirstname.value=myObject.firsttname;
              document.forms.fulfill.tolastname.value=myObject.lastname;
         } else {
              document.forms.fulfill.phone.value="";
              document.forms.fulfill.zipcode.value="";
              document.forms.fulfill.state.value="";
              document.forms.fulfill.city.value="";
              document.forms.fulfill.address.value="";
              document.forms.fulfill.country.value="";
              document.forms.fulfill.tofirstname.value="";
              document.forms.fulfill.tolastname.value="";
         }
}
</script>

</head>

<body>
<form method="post" action="formprocesser.php" name="fulfill">

<!--  My fields go here. I'll spare you the details of all the fields. 
      However, one is very important: It is the pull down of store names.
      That's the one that triggers the AJAX script, and it looks like this:  -->

      Store:
       <select name="storeid" onchange="startRequest('GET', 'http://www.myserver.com/serverfile.php?storeid=' + this.value);">
            <option value="" selected>Not Listed/Not a Store</option>
            <option value="1">Store Name</option>
            <option value="2">Another Store Name</option>
            <option value="3">Yet Another Store Name</option>

<!--   The options are actually all generated by a PHP script that pulls the
       store names and ID numbers from the database.   -->

       </select>

<!--   NOTE: That onchange command specifies the name of the server-based
       script that is being called. In this case, "serverfile.php". 
       Be sure to point this action to whatever URL your script resides at.
       In my case, it calls the script with the storeid from the pulldown values.   -->

        <input type="submit" name="Submit" value="Submit">
</form>

</body>
</html>



That is, by far, the hardest part. Now you just need a script that returns the values you need, and it's much, much easier than you expect.

Here's my entire serverfile.php. All it does is query the database and output an javascript-readable array with the database query results.

<?php
   include_once("json.php");
   include_once("ezsql.php");
   $store = $db->get_row("select * from  storelist where id=$storeid");
   $json = new json;
   $jsonencoded = $json->encode($store);
   echo $jsonencoded;
?>



Json.php is a very easy to use (and free) class which I found here: Click Me. As it turns out, there's a better alternative, also mentioned and pointed to from that URL, but the one there works fine for my purposes.

Ezsql.php is a (free) class for using mySQL/Oracle within php scripts, and (as you can see) it makes it very, very easy. You don't have to use Ezsql, of course, but it's my favorite wrapper and I highly recommend it. You can get it from Click Me.

That should be everything you need to make a form work as described!

I help somebody finds it helpful.

Wayne

This post has been edited by wzeller: 09 April 2006 - 07:41 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1