Jump to content

So any ideas on why this code not working


Notretsam

Recommended Posts

am a novice with javascript but really don't see why this code isn't working.

this is the trigger, person clicks on the area and page changes over to a new page

<div class='contframe' onClick='gotoshow($showid,show,$showhistid)'>

below is the function

<script type="text/javascript">
function gotoshow($sids, $booktype, $histid) {
var x=$sids;

var bocorp = document.getElementById('bocorp').value;
var fshowhistid = $histid;
var fbooktype = $booktype;
window.location.href = "showsetup.php?showid=" + x + "&corpid=" + bocorp + "&histid=" + fshowhistid + "&btype=" + fbooktype;
}

</script>

I can always get the onclick event to work with just passing one var across, but never been able to get more than one var passed across.

Link to comment
Share on other sites

Where are these variables being set? $showid,show,$showhistid

 

$showid and $showhistid are set via a database query before the output you see.

 

the "show" isn't a variable (I wonder if that causing the problem, but doubt it)

Edited by Notretsam
Link to comment
Share on other sites

Without any context, in the code you have shown those variables are undefined so the code would not work. You'll need to show the exact code you have and where those variables are being set if you want any more help.

Link to comment
Share on other sites

Don't know how seeing the full coding going help , as its just a normal prepare/bind sql query to get the information in variables.

<?php
	/* create a prepared statement */
	$stmtcommedit = $conn->prepare("SELECT eventid, eventName, eventImage, eventSize, eventStatus, eventCurrhistid, eventDesc, GMtype, eventGMmain, eventGMupper, eventGMmidcard, eventGMlower, eventGMopener, eventdivimain, eventdiviupper, eventdivimidcard, eventdivilower, eventdiviopener FROM companyEvents WHERE corpid=?");
    /* bind parameters for markers */
    $stmtcommedit->bind_param("s", $corpid);
    /* execute query */
    $stmtcommedit->execute();
	$stmtcommedit->store_result();
	$eventcount= $stmtcommedit->num_rows;
	    /* bind result variables */
    $stmtcommedit->bind_result($eventid, $eventName, $eventImage, $eventSize, $eventStatus, $eventCurrhistid, $eventDesc, $GMtype, $eventGMmain, $eventGMupper, $eventGMmidcard, $eventGMlower, $eventGMopener, $eventdivimain, $eventdiviupper, $eventdivimidcard, $eventdivilower, $eventdiviopener);
	while ($row = $stmtcommedit->fetch()) {
    
	/* checking if person viewing the page is listed as GM for event */
	if ( $visitID == "$eventGMmain" ) { $dispeve = "yes"; }
	elseif ( $visitID == "$eventGMupper" ) { $dispeve = "yes"; }
	elseif ( $visitID == "$eventGMmidcard" ) { $dispeve = "yes"; }
	elseif ( $visitID == "$eventGMlower" ) { $dispeve = "yes"; }
	elseif ( $visitID == "$eventGMopener" ) { $dispeve = "yes"; }
	/* end off checking if person viewing the page is listed as GM for event */
	
	if ( $dispeve == "yes" ) {
	$eventDesc = stripslashes("$eventDesc");
   	if ( $eventImage == "" ) { $eventImage = "No Image Set"; }
    else { $eventImage = "<img src='$eventImage' alt='$eventName'>"; }
	echo "
	<div class='contframe' onClick='gotoshow($eventid,event,$eventCurrhistid)'>
	<h2>$eventName</h2><br>
<table class='container'>
<tr>
<td id='showleftcol'>
<p class='centertext'>$eventImage</p>
   <p class='lefttext'>$eventDesc</p>
</td>
<td id='showrightcol'>
<p class='centertext'><h2>Event Size</h2><br>$eventSize</p>
</td>
</tr>
</table>
	</div><br>";
	}

	}
	
	?>
<script type="text/javascript">
function gotoshow($sids, $booktype, $histid) {
var x=$sids;

var bocorp = document.getElementById('bocorp').value;
var fshowhistid = $histid;
var fbooktype = $booktype;
window.location.href = "showsetup.php?showid=" + x + "&corpid=" + bocorp + "&histid=" + fshowhistid + "&btype=" + fbooktype;
}

</script>
Edited by Notretsam
Link to comment
Share on other sites

It's important because those are the values your function is working with. If the values are wrong then the output will be wrong.

 

This is still not enough information because I don't know what's in those PHP variables, if it's anything other than a number it's going to be passing undefined values into the function. You also seemed to have changed the variable show for event. event is a variable that will work because it's set by the browser.

 

 

Try printing out the values to your browser console.

function gotoshow($sids, $booktype, $histid) {
  console.log($sids, $booktype, $histid);
  var x=$sids;

  var bocorp = document.getElementById('bocorp').value;
  var fshowhistid = $histid;
  var fbooktype = $booktype;
  window.location.href = "showsetup.php?showid=" + x + "&corpid=" + bocorp + "&histid=" + fshowhistid + "&btype=" + fbooktype;
}

I suspect that these values are nothing like what you're expecting.

 

In most browsers you can see the Javascript console by pressing F12. You might even find an error message there.

Link to comment
Share on other sites

I don't understand how you can't see the variables are filled with information from a mysql database table row, so its random.

/* this is taken from view source */
<div class='contframe' onClick='gotoshow(14680020425539,event,)'>

/* this is the actual code */
<div class='contframe' onClick='gotoshow($eventid,event,$eventCurrhistid)'>

I will need to check if $eventCurrhistid is blank and set something in that variable it appears.

 

 

As I stated in original post , below coding has always worked for me.

<div class='contframe' onClick='gotoshow($eventid)'>



<script type="text/javascript">
function gotoshow($sids) {
var x=$sids;

var bocorp = document.getElementById('bocorp').value;
window.location.href = "showsetup.php?showid=" + x + "&corpid=" + bocorp;
}

</script>

Just never been able to get it working when passing more than one variable across to the function, always been able to work around that until now.

Link to comment
Share on other sites

OK checking F12 , I see this when I go to "Console"

4content.js:191 NO OEMBED
setupshow.php?corpid=1234698567:74 Uncaught ReferenceError: show is not defined
setupshow.php?corpid=1234698567:97 Uncaught ReferenceError: show is not defined
setupshow.php?corpid=1234698567:120 Uncaught ReferenceError: blank is not defined
setupshow.php?corpid=1234698567:134 Uncaught ReferenceError: blank is not defined
setupshow.php?corpid=1234698567:120 Uncaught ReferenceError: blank is not defined
setupshow.php?corpid=1234698567:134 Uncaught ReferenceError: blank is not defined

the "show" and "blank" are potential values in the last two variables listed in the onclick event

	$btype = "event";
	if ( $eventCurrhistid == "" ) { $eventCurrhistid = "blank"; }
	echo "
	<div class='contframe' onClick='gotoshow($eventid,$btype,$eventCurrhistid)'>

I hope that makes sense to you, if not I'll start again and explain what am doing.

Link to comment
Share on other sites

thank you Ingolme, got it working

 

Instead of setting $btype as "show" or "event" , I setting it as "1" or "2"

 

also instead of setting $eventCurrhistid as "blank" , am setting it as "1"

 

$eventid is always a 14 digit number, so why that always worked.

 

Didn't realize it had to be numbers instead of letters/words being passed, seems a bit weird to me.

 

thanks for the help.

Link to comment
Share on other sites

The thing is that they're not words, they're variable names. When you put show there, what you're doing is telling it to use the value of a variable named show. Since there's no part of the code that says var show = "something"; it has no value and is undefined.

 

If you want to pass a word into a function you have to wrap it in quotation marks: "show"

Link to comment
Share on other sites

ah I see, could I maybe do something like below, which I highly doubt

<script type="text/javascript">
var btype = "<?php echo $btype ?>";
</script>

as I said, doubt that work but just curious

 

I got it working fine with just using number sequences and that sufficient for what am doing.

Edited by Notretsam
Link to comment
Share on other sites

That will work. You should know that Javascript and PHP work very differently from eachother and run in their own environments. Javascript is not aware that PHP exists, PHP is not aware that Javascript exists.

 

PHP is executed first, whatever is printed out by PHP will be read by the browser, the browser has no idea that PHP generated the content that is being viewed. View the source code of your page after it has loaded (Control+U in Firefox on Windows) to see what code the browser is seeing.

Link to comment
Share on other sites

I am aware that they different , I always find PHP much easier to work with but first coding language I ever worked with, was mIRC scripting which is similar to PHP.

 

Also aware off view source, don't like that javascript code showing up on it, but love that PHP doesn't.

 

Can do some cool stuff with javascript, but I only ever use it after I go searching on how to do something and find a javascript answer before PHP. Even then, I try and find a way to get the javascript over to PHP, then do what I want to do.

Edited by Notretsam
Link to comment
Share on other sites

Use php to bring content from database, validate/sanitize forms data, and features that on its own JavaScript cannot provide. Use JavaScript to provide a more user friendly experience without reloading of page experience (ajax) with JavaScript form input validation for example, which has backup of php validate/sanitize, IF JavaScript is disabled.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...