PHP - Newbee Needs Help With Javascript/php/mysql
Ok, I consider myself a dabbler in programming so bear with me. What I need to do sounds fairly simple, but I cannot figure it out. I have an html page. Inside is some basic html and some Javascript. The javascript basically generates a random value consisting of numbers and characters in: var = randomToken. The length of the random value generated is 11 characters on chrome and 19 on FF? I have no idea why the discrepancy between browsers, but that's not a problem. I'll look into that later. What I'm having trouble doing is right now, I need to take that random value that was generated by a javascript function and save it out to a database, so on the other end, when someone browses to another page, I need to retrieve the saved value from the database. I've looked at many examples but they always include a bunch of stuff I don't need, like tables, multiple stuff in the database, etc. All I need to do is be able to store a variable and later read it out of a database. The database just needs a field to store the random value. Nothing else is needed in the database (for now anyway). I know I need php, but i am super green at using it. I have a mysql database/table built with field: randomVar So in a nutshell: The starting html page needs to: Generate random value in Javascript. Save it in a variable. Done. Pass it to php Not Done Update the database randomVar with the random value Not Done Zero out the randomVar field in the database at some point. (when originator hits stop button or closes the page.) Not DoneAnother html page needs to: PHP Read the randomVar from the database; if > 0 use it Not Done If = 0, echo back some text to the html page....like "Try Later... Not Done Pass it to javascript Not DoneI am not looking for the exact code unless you want to supply it? :) Thanks, Ray
Similar TutorialsHi I need to set up a web page that will allow users to find a location. I have set up a web form where a user can search by country, state or postal code. Now I need to find a php code that will let me display the information into a web page. I am also going to need to create a database that will house the information that I want to display. Like street map, street address and phone number. Please tell me there is a generic form out there that I can use and easily editable. Thank You! hello all merry christmas
new to all this allways needsome help
I am implementing a site that will be pulling data from a MySQL server every 2 seconds and updating the page accordingly. I want to redraw charts using Google Chart API as this is happening. Overall I do not want to have the page refresh for the user. How should I implement this (generally of course)? I don't think there is a way to do it all in javascript...since I need to access a remote database and all. Please let me know what advice you have. thanks! Hi, hope I have posted this in the right place, I have the following code which contains PHP and javascript, where both work on there own but when I try putting them together I keep getting errors: Code: [Select] <?php include 'config.php'; include 'opendb.php'; $var = 'Smith'; $query = "SELECT * FROM search_directory WHERE merchant LIKE \"%$var%\""; $result = mysql_query($query); $id = $row{"id"}; while($row = mysql_fetch_assoc($result)) { echo "Name: {$row['name']} <br>"; echo "Subject: {$row['title']} <br><br>"; } include 'closedb.php'; ?> <a href="javascript:;" onclick="document.getElementById('.....').style.display='block';window.open('http://www.google.co.uk');"><img src="../../graphics/Click here.png" border="0" /></a> <div id="....." style="display:none;"> ...your text here </div> What I have been trying to do is get $id variable from the php code to replace the '.....' in the javascript and div id. I have tried putting it into an echo statement which seemed to work with the div id but the brought up errors when i tried with the javascript. If anyone knows how to solve this it would be very much appreciated. Thanks in advance. Hello Everyone, I think this might be a bit of a challenge but its always worth getting some input for solutions. I'm using jqplot to create graphs from data held in a database. And im having trouble thinking of a way to lay the data out in an acceptable way. To plot a line the javascript takes a statement in this form: line1 = [['x-axis1'],y-axis1],['x-axis2',y-axis2] I'm running this query to get the data: <?php $query2 = "SELECT * FROM tracker WHERE username = '" . mysql_real_escape_string($usera) . "'"; $result = mysql_query($query2) or die('Error, query failed : ' . mysql_error()); while($row = mysql_fetch_assoc($result)) { $data[] = $row; } ?> This returns things like this: $data[0]['date'], $data[0]['reps'] $data[1]['date'], $data[1]['reps'] etc... Values are likely to be added or removed so I need to construct a loop of some kind to format it like this, line1 = [['{$data[0]['date']}',{$data[0]['reps']}],['{$data[1]['date']}',{$data[1]['reps']}],['{$data[2]['date']}'],{$data[2]['reps']}]]; Can someone point me in the right direction? Thanks in Advanced This is my website http://www.rahulkadukar.info/ As you can see I have the blue navigation bar on top and on mouse hover you can see a drop down menu. The contents of the drop down menu can change and I wanted to write a script that would populate the contents of the drop down menus. Which of these is a better option 1. Use JavaScript along with a text file and use JavaScript to populate the drop down 2. Use a SELECT in PHP to populate the drop down menu I am expecting that the 10 main columns would have at the most 20 entries each. This topic has been moved to Ajax Help. http://www.phpfreaks.com/forums/index.php?topic=347585.0 create table mimi (mimiId int(11) not null, mimiBody varchar(255) ); <?php //connecting to database include_once ('conn.php'); $sql ="SELECT mimiId, mimiBody FROM mimi"; $result = mysqli_query($conn, $sql ); $mimi = mysqli_fetch_assoc($result); $mimiId ='<span>No: '.$mimi['mimiId'].'</span>'; $mimiBody ='<p class="leading text-justify">'.$mimi['mimiBody'].'</p>'; ?> //what is next? i want to download pdf or text document after clicking button or link how to do that Hello everyone, Sorry if this has been answered but if it has I can't find it anywhere. So, from the begining then. Lets say I had a member table and in it I wanted to store what their top 3 interests are. Their$ row has all the usual things to identify them userID and password etc.. and I had a further 3 columns which were labled top3_1 top3_2 & top3_3 to put each of their interests in from a post form. If instead I wanted to store this data as a PHP Array instead (using 1 column instead of 3) is there a way to store it as readable data when you open the PHPmyadmin? At the moment all it says is array and when I call it back to the browser (say on a page where they could review and update their interests) it displays 'a' as top3_01 'r' as top3_02 and 'r' as top3_03 (in each putting what would be 'array' as it appears in the table if there were 5 results. Does anyone know what I mean? For example - If we had a form which collected the top 3 interests to put in a table called users, Code: [Select] <form action="back_to_same_page_for_processing.php" method="post" enctype="multipart/form-data"> <input name="top3_01" type="text" value="enter interest number 1 here" /> <input name="top3_02" type="text" value="enter interest number 2 here" /> <input name="top3_03" type="text" value="enter interest number 3 here" /> <input type="submit" name="update_button" value=" Save and Update! " /> </form> // If my quick code example for this form is not correct dont worry its not the point im getting at :) And they put 'bowling' in top3_01, 'running' in top3_02 and 'diving' in top3_03 and we catch that on the same page with some PHP at the top --> Code: [Select] if (isset($_POST)['update_button']) { $top3_01 = $_POST['top3_01']; // i.e, 'bowling' changing POST vars to local vars $top3_02 = $_POST['top3_02']; // i.e, 'running' $top3_03 = $_POST['top3_03']; // i.e, 'diving' With me so far? If I had a table which had 3 columns (1 for each interest) I could put something like - Code: [Select] include('connect_msql.php'); mysql_query("Select * FROM users WHERE id='$id' AND blah blah blah"); mysql_query("UPDATE users SET top3_01='$top3_01', top3_02='$top3_02', top3_03='$top3_03' WHERE id='$id'"); And hopefully if ive got it right, it will put them each in their own little column. Easy enough huh? But heres the thing, I want to put all these into an array to be stored in the 1 column (say called 'top3') and whats more have them clearly readable in PHPmyadmin and editable from there yet still be able to be called back an rendered on page when requested. Continuing the example then, assuming ive changed the table for the 'top3' column instead of individual colums, I could put something like this - Code: [Select] if (isset($_POST)['update_button']) { $top3_01 = $_POST['top3_01']; // i.e, 'bowling' changing POST vars to local vars $top3_02 = $_POST['top3_02']; // i.e, 'running' $top3_03 = $_POST['top3_03']; // i.e, 'diving' $top3_array = array($top3_01,$top3_02,$top3_03); include('connect_msql.php'); mysql_query("UPDATE members SET top3='$top3_array' WHERE id='$id' AND blah blah blah"); But it will appear in the column as 'Array' and when its called for using a query it will render the literal string. a r r in each field instead. Now I know you can use the 'serialize()' & 'unserialize()' funtcions but it makes the entry in the database practically unreadable. Is there a way to make it readable and editable without having to create a content management system? If so please let me know and I'll be your friend forever, lol, ok maybe not but I'd really appreciate the help anyways. The other thing is, If you can do this or something like it, how am I to add entries to that array to go back into the data base? I hope ive explained myself enough here, but if not say so and I'll have another go. Thanks very much people, L-PLate (P.s if I sort this out on my own ill post it all here) I have following piece of code below, and I would like to be able to express it pure SQL, something that could go into a .sql file :
$request_string='SELECT topic_forum_id FROM topic_table WHERE topic_id= 2014'; $query=database->prepare($request_string); $query->execute(); $data=$query->fetch(); $query->closeCursor(); $forum=$data['topic_forum_id']; $request_string='INSERT INTO post_table (post_topic,post_forum) VALUES (2014,:forum)'; $query=database->prepare($request_string); $query->bindValue(':forum',$forum,PDO::PARAM_INT); $query->execute(); $data=$query->fetch(); $query->closeCursor();Is it possible ? So i have this php as shown below. It should make a list of comments with comment replies below their comment respectively. The problem is that it only goes through and shows 1 comment and all the comment replies for that one comment. It should be showing all comments i have in the db for that article. If i remove the second while then it shows all the comments correctly but no comment replies then... How do i get this script to loop through the db for every comment but also loop through every comment reply for that $row[id]? If anyone has a better / more efficient way of what I am trying to do, please explain or show example (i am open to anything)... Code: [Select] // what article are we showing? $article_to_show_id = $_GET['article_id']; $active_is_set_text = "1"; // Active Column text that makes it okay to show // Finding the article $search_for_article = mysql_query("SELECT * FROM articles WHERE id = '$article_to_show_id' AND active = '$active_is_set_text'"); while($row = mysql_fetch_array($search_for_article)) { // format the last updated date right $update_date_edit = $row[update_date]; $update_date_edit = date('F j, Y \a\t h:ia', $update_date_edit); $row[update_date] = $update_date_edit; // format the submit updat date right $submit_date_edit = $row[submit_date]; $submit_date_edit = date('F j, Y \a\t h:ia', $submit_date_edit); $row[submit_date] = $submit_date_edit; echo ' <div> ', $row[title] ,' </div> <div> by: ', $row[author] ,' on ', $row[submit_date] ,' </div> <div> ', $row[content] ,' </div> <div> Last Updated: ', $row[update_date] ,' </div> <form action="article_reply.php" method="post"> <input type="hidden" name="article_id" value="', $row[id] ,'" /> <button name="article_reply" type="submit" value="submit">Reply</button> </form> '; } $comment_count = 0; $comment_reply_count = 0; // Finding all of the comments $search_for_article = mysql_query("SELECT * FROM article_comments WHERE article_id = '$article_to_show_id' AND reply_id = '0'"); while($row_comment = mysql_fetch_array($search_for_article)) { // format the submit updat date right $comment_date_edit = $row_comment[comment_date]; $comment_date_edit = date('F j, Y \a\t h:ia', $comment_date_edit); $row_comment[comment_date] = $comment_date_edit; echo ' <br> <br> COMMENT:<br> <div> By: ', $row_comment[username] ,' on ', $row_comment[comment_date] ,' </div> <div> ', $row_comment[comment] ,' </div> '; $comment_count++; // Finding all of the comment replies if any $search_for_article = mysql_query("SELECT * FROM article_comments WHERE article_id = '$article_to_show_id' AND reply_id = '$row_comment[id]'"); while($row_two = mysql_fetch_array($search_for_article)) { // format the submit updat date right $comment_date_edit = $row_two[comment_date]; $comment_date_edit = date('F j, Y \a\t h:ia', $comment_date_edit); $row_two[comment_date] = $comment_date_edit; echo ' <br> <br> COMMENT REPLY:<br> <div> By: ', $row_two[username] ,' on ', $row_two[comment_date] ,' </div> <div> ', $row_two[comment] ,' </div> '; $comment_reply_count++; } } Need some help I have 2 tables in a database and I need to search the first table and use the results from that search, to search another table, can this be done? and if it can how would you recommend that I go about it? Thanks For Your Help Guys! I have a table that has a field called rating which can be from 0-100. I want to create a mysql query that will count the number of ratings between 0-20 then 21-40 then 41-60 etc. However I'm not really sure how to do this so any help would be great. Evening everybody! What I am trying to do is display infomation from a database, each item has a catid and a subid. I can display the correct information when I am looking at a particular subid by using: $catid = $_GET['catid']; if(isset($_GET['subid'])){ $subid = $_GET['subid']; } else $subid = '';?> //other code $query = mysql_query("SELECT * FROM merchants WHERE catid= '$catid' AND subid= '$subid'"); But I cant figure a way to change this so if the AND is not met it will still display the info based on $catid? Basically I want to display all the records with a particular catid, regardles of subid, but only if subid does not exits?! Does this make sense? Basically I think I am after and AND/OR I hope you can see what I am on about! Cheers Hi all! I have racked my brain trying to figure this one out. I'm typically a smart cookie, but since I am kinda new to php and mysql, I'm stumped. I'm just trying to program a Joomla search plugin that queries my HDFLV player database, so videos are searchable, but I keep getting an invalid "foreach(" command. It's supposed to be there to interpret the database query, right? I'll post my code here, can someone give me some insight? Code: [Select] <?php /** * @version $Id: contacts.php 10381 2008-06-01 03:35:53Z pasamio $ * @package Joomla * @copyright Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved. * @license GNU/GPL, see LICENSE.php * Joomla! is free software. This version may have been modified pursuant * to the GNU General Public License, and as distributed it includes or * is derivative of works licensed under the GNU General Public License or * other free or open source software licenses. * See COPYRIGHT.php for copyright notices and details. */ // no direct access defined( '_JEXEC' ) or die( 'Restricted access' ); $mainframe->registerEvent( 'onSearch', 'plgSearchhdflvplayer' ); $mainframe->registerEvent( 'onSearchAreas', 'plgSearchhdflvplayerAreas' ); JPlugin::loadLanguage( 'plg_search_hdflvplayer' ); /** * @return array An array of search areas */ function &plgSearchhdflvplayerAreas() { static $areas = array( 'hdflvplayer' => 'Videos' ); return $areas; } /** * Contacts Search method * * The sql must return the following fields that are used in a common display * routine: href, title, section, created, text, browsernav * @param string Target search string * @param string mathcing option, exact|any|all * @param string ordering option, newest|oldest|popular|alpha|category */ function plgSearchhdflvplayer( $text, $phrase='', $ordering='', $areas=null ) { $db =& JFactory::getDBO(); $user =& JFactory::getUser(); if (is_array( $areas )) { if (!array_intersect( $areas, array_keys( plgSearchhdflvplayerAreas() ) )) { return array(); } } // load plugin params info $plugin =& JPluginHelper::getPlugin('search', 'hdflvplayer'); $pluginParams = new JParameter( $plugin->params ); $limit = $pluginParams->def( 'search_limit', 50 ); $text = trim( $text ); if ($text == '') { return array(); } $section = JText::_( 'hdflvplayer' ); switch ( $ordering ) { case 'oldest': $order = 'a.id ASC'; break; case 'popular': $order = 'a.times_viewed DESC'; break; case 'alpha': $order = 'a.title ASC'; break; case 'category': $order = 'b.title ASC, a.title ASC'; break; case 'newest': default: $order = 'a.id DESC'; } $text = $db->Quote( '%'.$db->getEscaped( $text, true ).'%', false ); $query = 'SELECT a.title AS title, "" AS created,' . ' CASE WHEN CHAR_LENGTH(a.alias) THEN CONCAT_WS(\':\', a.id, a.alias) ELSE a.id END as slug, ' . ' CASE WHEN CHAR_LENGTH(b.alias) THEN CONCAT_WS(\':\', b.id, b.alias) ELSE b.id END AS catslug, ' . ' CONCAT_WS( ", ", a.title, a.description ) AS text,' . ' CONCAT_WS( " / ", '.$db->Quote($section).', b.title ) AS section,' . ' "2" AS browsernav' . ' FROM #__hdflvplayerupload AS a' . ' INNER JOIN #__hdflvplayername AS b ON b.id = a.catid' . ' WHERE ( a.title LIKE '.$text . ' OR a.description LIKE '.$text.' )' . ' AND a.published = 1' . ' AND b.published = 1' . ' AND a.access <= '.(int) $user->get( 'aid' ) . ' AND b.access <= '.(int) $user->get( 'aid' ) . ' GROUP BY a.id' . ' ORDER BY '. $order ; $db->setQuery( $query, 0, $limit ); $rows = $db->loadObjectList(); foreach($rows as $key => $row) { $rows[$key]->href = '?pid='.$row->slug; } return $rows; } ANY help is more than what I got from HDFLV Player support. Thank you so much in advance! hey Guys I have a table in a db that stores attack details for my game below Code: [Select] id attacker win defender time deleted 1 17 1 84 1288576720 0 2 22 1 84 1288576989 0 i want to give a stat like, total attacks made : 20 (times there id is attacker) percent successful = 20% (if win =1 its a win =0 its a loss) and the same with times attacked. I was thinking along the lines of getting the info using Code: [Select] $stat = mysql_fetch_array(mysql_query("select * from attacklog where attacker='$userid' AND win='1'")); would i just do that for every option in the database? Code: [Select] <?php $un = $_POST['Username']; // connect to the db $mysql_host = "localhost"; $mysql_database = "****"; $mysql_user = "*****"; $mysql_password = "*****"; $conn = mysql_connect($mysql_host,$mysql_user,$mysql_password); mysql_select_db($mysql_database,$conn); $query = "SELECT * FROM student WHERE Username='$un'"; $result = mysql_query($query); //Store all the course code into an array $course_code = mysql_fetch_array($result); //verification if(mysql_num_rows($result) == 1) { echo 1; // for correct login response $format = ".txt"; $output = $un.$format; $fh = fopen($output,'w') or die ("Can't open file"); for($num=1;$num<=5;$num++) { $code = $course_code['Course'.$num]; fwrite($fh,$code."\r\n"); } chmod($output,0777); //chmod the file to 0777 fclose($fh); } else { echo 0; // for incorrect login response } ?> im doing the Learning Management System application. this FirstTimeUser.php will connect to the database and list out the coursecode in .txt take a look at my code, the output is always 0. I already stuck on how to get 1. The connection for the php to mysql is correct. the username also exist in the database. is there any suggestion on how to debug it. i dont know, its like a simple coding, but its not working... please leave some suggestion... hello i am making a online cook for people to register and make and online cookbook on there account i want them to be able to add recipes and be able to see them on there file, i know how it would connect to the database and how to send the information but what tables would i use and what fields? this is my basic site now www.lachlanmcgrath.net, if you can help please reply here or even e-mail me at lmcgr44@me.com thank-you for your help I have this code I want to check to see if the id from table1 is in table2. Can anyone help me with that if($checkProfile) { $sql_chk_tbl_username = "SELECT id FROM members_profile WHERE id = '"$_SESSION['face']"'"; $rs_user_list = mysql_query($sql_chk_tbl_username); if(mysql_num_rows($rs_user_list) <= 0) } ?> |