Simple Syntax Error? PHP; JS; HTML

I press a button which will follow this text block; It submits the page and does all that it needs to do including making the $_SESSION['gadd_record] variable = Yes. I have it echo displayed lower on the page to make sure that the variable is for sure “Yes” However my button does not seem to run the following script, as my heyyou value doesn’t change nor does my window location change. Help please :slight_smile: All of the pieces are located on the same form inside the same TR even.

//////////////////////BUTTON

<.input name="change_item_btn" id="change_item_btn" type="image" src="images/save.gif" alt="Submit"  width="122" height="54" onMouseOut="MM_nbGroup('out');" onMouseOver="MM_nbGroup('over','change_item_btn','images/save_f2.gif','images/save_f4.gif',1);" onClick="MM_nbGroup('down','navbar1','change_item_btn','images/save_f3.gif',1); GetOnWithIt()"> 

//////////////////////FUNCTION

<.script>
function GetOnWithIt() {
if ($_SESSION['gadd_record'] == "Yes") {
document.getElementById('heyyou').value = "yes I did";
window.location.href = "sell_items_qm.php?session_id=<?= $session_id ?>";
}
else {
//do nothing
}
}
<./script>

///////////////////TEXT FIELD FOR HEYYOU

<.td> <.input id="heyyou" type="text" value="nope">

I don’t know what kind of HTML that is but the JavaScript has obvious issues.

eg. output this to the current window, then immediately reload the window content.

So you’re not going to see the change. But what about the location?

if ($_SESSION['gadd_record'] == "Yes") {

That looks an awful lot like PHP inserted directly into JavaScript. That won’t work and TBH I’d be surprised if you’re not getting error messages you forgot to tell us about.

Compare with

window.location.href = "sell_items_qm.php?session_id=<?= $session_id ?>"; 

I’m not sure I understand this part at all; The URL in question points to a different page and that part works on another page I’m not super worried about the …

section however the entire if doesn’t run. I don’t have any debugging other than my own brain and eyeballs so error messages are few and far. However, using the inspect function of the browser and some timely print screens I did get an error that proves you right I believe.

"Uncaught ReferenceError : $_SESSION is not defined
at GetOnWithIt (add…
at HTMLInputElement.onclicke (add…

So my new question I guess is how I am to convert the PHP session variable into a variable that will handle the function. The session variable is set in a “require” include. I think I should just be able to set anything instead of that, if you have any ideas on how to do so. I am rank 1 bad at all of this so the simple confounds me.

Well, don’t feel alone. Mixing server-client php-html-javascript can easily get out of control.

In this case, the text file (as opposed to binary, not ext) goes from what is HTML to PHP without what the engine needs.

Anyway, I think a good first step would be to make a rough list of what needs to happen and where. eg.

  1. a request for this file
  2. do PHP stuff
  3. typically send this back to the requester [client]

Personal preferences vary, but though 2 and 3 above are grossly inadequate the more easily you can tell the two apart the less likely you’ll get mixed up. Where you go from one to the other and how often most often depends on circumstance and choice.

But I get ahead of things. Please post a more detailed 2 and 3 (I usually start with “initial request” stuff first)

When you want the PHP $_SESSION[‘gadd_record’] to be a certain value, it’s not possible to use JavaScript to set that value.

PHP and JavaScript cannot see each others variables.

They can’t see or share variables, but they can pass values around. eg. (pseudocode)

<?php 
$php_username_var = $User->name; 
/* PHP comment */ 
?> 
<!-- HTML comment --> 
<script> 
const js_username_var = "<?php echo $php_username_var; ?>"; 
<input name="username" .. > 
<?php 
$php_username_var = $_POST['username']; 
?> 

OOf ok that all went over my head. I have this now. I am trying to separate JS and PHP tell me where I am wrong please :slight_smile:

 <!-- <input id="record_added" type="text" value="<?php echo $_SESSION['gadd_record'] ?>" > -->
<input id="record_added" type="text" value="Yes" >

<script>

function GetOnWithIt() {
if (document.getElementById('record_added') == "Yes") {
	document.getElementById('heyyou').value = "yes I did";
	window.location.href = "sell_items_qm.php?session_id=<?= $session_id ?>";
}
else {
	//do nothing
}
}
</script>

I have this code directly following this button :

<input name="change_item_btn" id="change_item_btn" type="image" src="images/save.gif" alt="Submit"  width="122" height="54"
					onMouseOut="MM_nbGroup('out');" onMouseOver="MM_nbGroup('over','change_item_btn','images/save_f2.gif','images/save_f4.gif',1);" onClick="MM_nbGroup('down','navbar1','change_item_btn','images/save_f3.gif',1); GetOnWithIt()">

Yet the function does not run, based on the lack of changes to my “heyyou” field

<td> <input id="heyyou" type="text" value="nope"> </td>

Got it; This works. I appreciate the direction from everybody :slight_smile:

<?php

if($_SESSION['gadd_record'] == "Yes")

{
  echo "<script> window.location.href = 'sell_items_qm.php?session_id=$session_id';</script>";
  $_SESSION['gadd_record'] = NULL;

}
?>

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.