Jump to content

insert javascript result into mysql table?


paulmo

Recommended Posts

I need to INSERT the javascript result, geoip_city and geoip_region_name into 2 fields in mysql db table (I know how to insert the values from php form into db table). The js is embedded in the php code, and it's called within several different "phrases" that are within cases; I've posted one example below.

 

Hope this is possible...thanks for help!

 

$phrase7= 'Although the landscape is dormant, you are experiencing a season of growth! You have found a renewed sense of focus and direction, afforded by
quiet hours of productivity. Let the clear, cold air continue to inspire your progress in    
<script language="JavaScript" src="http://j.maxmind.com/app/geoip.js"></script>
<script language="JavaScript">document.write(geoip_city());</script>, 
<script language="JavaScript">document.write(geoip_region_name());</script>.';
break;

Link to comment
Share on other sites

since the JavaScript isn't parsed/executed until AFTER php is done, and the code is sent to the user's browser, you will need to use AJAX to send the data to your server.

 

the easiest way to do an AJAX call is with jQuery (in my opinion):

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
<script type="text/javascript">
  $(function(){ //This syntax will execute when the page is done loading
    $.post("geo.php",{
      geoip_city: geoip_city(),
      geoip_region_name: geoip_region_name()
    });
  });
</script>

 

the above will do a POST to geo.php with $_POST['geoip_city'] and $_POST['geoip_region_name'] set. the you can use PHP to store the data into mysql

Link to comment
Share on other sites

rhodesa, thanks for getting me started...i've wanted to use jquery.

 

have created geo.php page and put your code into the other php page. getting

Parse error: parse error, unexpected '<'  

here:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>

 

then tried

src=jquery-1.2.6.min.js

and uploaded that file to root. same error message.

 

this is what my geo.php page looks like:

<?php
ini_set("display_errors", 1);
error_reporting(E_ALL); 


mysql_connect("xxx", "xxx", "xxx") or die(mysql_error()); 
mysql_select_db("xxx") or die(mysql_error()); 

$geoip_city = mysql_real_escape_string($_POST['geoip_city']);
$geoip_region_name = mysql_real_escape_string($_POST['geoip_region_name']);

mysql_query("INSERT INTO interactive (geoip_city, geoip_region_name) VALUES ('$geoip_city', '$geoip_region_name') ") or die(mysql_error()); 
?>

 

of course, i've created geoip_city and geoip_region_name fields in db table.

thank you for your help!

Link to comment
Share on other sites

This is working almost perfectly; thanks! I had not closed off php, so I did (not sure how to do with echo):

('$name', '$theme') ") or die(mysql_error()); 
?>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
<script type="text/javascript">
  $(function(){ //This syntax will execute when the page is done loading
    $.post("geo.php",{
      geoip_city: geoip_city(),
      geoip_region_name: geoip_region_name()
    });
  });
</script>
<?php
switch (intval(date('n')))

 

I thought js could be embedded in php? It works that way for the geoip js. And why go to http jquery instead of use jquery file? (I'm a newbie on this.)

 

Only problem now is—perhaps because table fields (name/theme & geoip_city/geoip_region_name) are being processed by different pages—the db table rows are not matching up; the name/theme fields are in same row but geoip info is appearing unpredicatably: sometimes above, then below all the other rows. Of course, I need to see name/theme/geo city and name all in same row.

 

Advice on this? thanks

 

Link to comment
Share on other sites

i think you misunderstand the integration of PHP/HTML/JavaScript...PHP is a server-side language and HTML/JavaScript are client-side languages. So, when you are looking at your script, all the PHP that is in it is run first by the server, to generate the HTML/JavaScript that is then sent to the client's browser. Only AFTER all this is generated, the client's browser runs the HTML/JavaScript.

 

can you post the entire code for the PHP page? i can explain it better with using your code as an example

Link to comment
Share on other sites

I understand the server side/browser side concept. Just seemed odd that js does work embedded in php in one instance but not in the present example.

 

I appreciate your help. As I can't post the entire page, I'll try to re-state my question: how to get values from different pages inserted into same row in db table?

 

thanks

Link to comment
Share on other sites

ok.....i think i see what you mean...on the main page, do the INSERT and get the ID for the row created using mysql_insert_id(). Then, pass that ID with the AJAX request, and have geo.php run an UPDATE on the row with that ID. does that make sense?

Link to comment
Share on other sites

how close is this for getting all in same db row? from main page:

mysql_query("INSERT INTO interactive (name, theme) VALUES ('$name', '$theme') ") or die(mysql_error()); 
mysql_insert_id();

from geo.php page:

mysql_query("INSERT INTO interactive (geoip_city, geoip_region_name) VALUES ('$geoip_city', '$geoip_region_name') ") or die(mysql_error()); 
mysql_insert_id(UPDATE);

Link to comment
Share on other sites

assuming your Auto-incrementing Primary Key on the table is called 'id', like this:

<?php
mysql_query("INSERT INTO interactive (name, theme) VALUES ('$name', '$theme') ") or die(mysql_error());
$row_id = mysql_insert_id(); //Put it in a variable for later
?>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
<script type="text/javascript">
  $(function(){ //This syntax will execute when the page is done loading
    $.post("geo.php",{
      geoip_city: geoip_city(),
      geoip_region_name: geoip_region_name(),
      row_id: '<?php echo $row_id; />' //Pass it with the JavaScript
    });
  });
</script>

 

<?php
ini_set("display_errors", 1);
error_reporting(E_ALL); 


mysql_connect("xxx", "xxx", "xxx") or die(mysql_error()); 
mysql_select_db("xxx") or die(mysql_error()); 

$geoip_city = mysql_real_escape_string($_POST['geoip_city']);
$geoip_region_name = mysql_real_escape_string($_POST['geoip_region_name']);
$row_id = mysql_real_escape_string($_POST['row_id']);

mysql_query("UPDATE interactive SET geoip_city = '$geoip_city', geoip_region_name = '$geoip_region_name' WHERE id = '$row_id'") or die(mysql_error()); 
?>

Link to comment
Share on other sites

rhodesa, have done your edits, thanks. did not have primary key, so created "id" field (medint), value 0, and made it primary. the geoip city and region are not posting at all now in the db ("name/theme" did post), and after the 2nd form submit, get error message:

#1062 - Duplicate entry '0' for key 1 

which was same as previous error message in mysql (phpadmin) before deleting all previous db field entries.

 

curiously, "id" row is "0" upon 1st form/field entry ("name/theme"). 

Link to comment
Share on other sites

i've ALTER TABLE AUTO INCREMENT "id" field in mysql query, and selected field as primary, and now "id" field is auto incrementing with numbers, so that looks good (i'm guessing sole purpose is to define row_id). but the geoip city and region are still not posting to the db.

 

i did replace a closing "/>" with "?>" from here

row_id: '<?php echo $row_id; /> //Pass it with the JavaScript

because reported error on that line. the code all seems to work now just this problem above.

 

Thanks for help!!

Link to comment
Share on other sites

ok...time for some debugging...let's add a function that alerts the ajax response:

<?php
mysql_query("INSERT INTO interactive (name, theme) VALUES ('$name', '$theme') ") or die(mysql_error());
$row_id = mysql_insert_id(); //Put it in a variable for later
?>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
<script type="text/javascript">
  $(function(){ //This syntax will execute when the page is done loading
    $.post("geo.php",{
      geoip_city: geoip_city(),
      geoip_region_name: geoip_region_name(),
      row_id: '<?php echo $row_id; /> //Pass it with the JavaScript
    },function(message){
      alert(message);
    });
  });
</script>

 

and print some stuff for it to alert:

<?php
ini_set("display_errors", 1);
error_reporting(E_ALL); 


mysql_connect("xxx", "xxx", "xxx") or die(mysql_error()); 
mysql_select_db("xxx") or die(mysql_error()); 

$geoip_city = mysql_real_escape_string($_POST['geoip_city']);
$geoip_region_name = mysql_real_escape_string($_POST['geoip_region_name']);
$row_id = mysql_real_escape_string($_POST['row_id']);

$sql = "UPDATE interactive SET geoip_city = '$geoip_city', geoip_region_name = '$geoip_region_name' WHERE id = '$row_id'";
print $sql;
mysql_query($sql) or die(mysql_error());
?>

Link to comment
Share on other sites

i'm getting no alert box, on web page after form's processed, or anywhere in mysql (guessing alert would be on page though).

 

the city/region is working on form, just not posting to db table; id/name/theme/date are all working, and on same line.

 

AJAX is a pain in the butt to debug. What I usually do, with post data anyways, is create a form that is just straight html to post to that ajax page, so when you submit it you see the feedback.

 

Chances are you have an error in your code and the script is throwing the error and ajax does not know how to handle that well so it just does nothing.

Link to comment
Share on other sites

success! was missing the comma here:

geoip_region_name: geoip_region_name(),

 

now to get rid of ie alert box?:

UPDATE interactive SET geoip_city = '$geoip_city', geoip_region_name = '$geoip_region_name' WHERE id = '20'

 

thanks!

 

JS Syntax error, should have known. That is what a good JS Debug Console is for (Firefox has one built in) and I always check that first if my AJAX does not work =)

 

Good luck.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.