Note that there are some explanatory texts on larger screens.

plurals
  1. POmysql select js variable
    primarykey
    data
    text
    <p>Hey I wanna know if something like this is possible?</p> <pre><code>$result = mysql_query("SELECT * FROM posted_events WHERE Month_ = ".echo"monthS";." ); </code></pre> <p>Obviously the syntax is wrong (because it doesn't work), but basically I want to select some data from my database where equal to the variable which happen to be a JS variable in my JS file. I know you can't assign a js variable to a PHP, unless you use AJAX, but when I use ajax, it returns an empty string first, runs the php and then it gives me the value of the variable, but by then my php script already ran. So was wondering if I could echo the variable.</p> <p>I'm not using any submit or hidden post method because it isn't necessary in what I'm trying to create. So I need to find a way for PHP to recognize the variable and make the query. PLEASE help me. Thank a lot. </p> <p>my JS:</p> <pre><code> var date = new Date(); var month = date.getMonth(); var day = date.getDay(); var monthDate = date.getDate(); var monthNow= date.getMonth(); var monthNow2; var current = 1; var first_date = new Date(date.getFullYear(), date.getMonth(), 1); var start_day = first_date.getDay(); var start_day2; var first_date2; var weekday; var day_selected; var day_selectedTxt; var monthNowTxt; var dayNum; var out; var monthS; var calenderMonths = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']; var calenderDays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday' ]; var daysInMonths = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; function currentDate(){ var first_day = first_date.getDay() +1; document.getElementById("monthElement").innerHTML = calenderMonths[month]; for( var days = 1; days &lt;= daysInMonths[month]; days++) { document.getElementById("day"+ first_day).innerHTML = days; first_day++; } //current date if( month === monthNow) { monthDateNow = start_day +monthDate; var today = document.getElementById("day"+ monthDateNow); today.setAttribute('class', today.getAttribute('class') + ' current'); } start_day2 = start_day; monthNow2 = monthNow; monthS = calenderMonths[monthNow]; $.get('load2.php', {month:monthS} ); //where i made the ajax get } </code></pre> <p>my PHP:</p> <pre><code>&lt;?php Header("content-type: application/x-javascript"); error_reporting(E_ERROR | E_WARNING | E_PARSE); $monthS = $_GET['month']; // Connect to MySQL if ( !( $database = mysql_connect( "localhost", "root", "" ) ) ) die( "Could not connect to database &lt;/body&gt;&lt;/html&gt;" ); // open Events database if ( !mysql_select_db( "Events", $database ) ) die( "Could not open Events database &lt;/body&gt;&lt;/html&gt;" ); $result = mysql_query("SELECT * FROM posted_events WHERE Month_ = .'monthS'.") or die ('Error updating database because: '.mysql_error());; $daysArray = Array(); while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $daysArray[] = $row['DayNum']; } $length = count($daysArray); echo "function test() { "; for($i=0; $i &lt; $length; $i++) { echo "var active = document.getElementById('day'+".$daysArray[$i]."); active.setAttribute('class', active.getAttribute('class') + ' event'); "; } echo "}"; ?&gt; </code></pre> <p>And in my HTML i have <code>&lt;body onload="currentDate(); test(); " class ="home"&gt;</code> and other code ofc, I just posted that cause the rest wasn't necessary.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload