Php inside javascript inside php

After using this script:

<script>
window.onload = function(){
  document.getElementById("jform_contact_name").value = "<?php $user =& JFactory::getUser(); if (!$user->registered) {echo $user->username;} ?>";
} 
</script>

I need to include it into a conditional:

<?php
$uri = $_SERVER['REQUEST_URI'];
if (strpos($uri,'application-form') !== false ) {
echo "<script>window.onload = function(){document.getElementById("jform_contact_name").value = "<?php $user =& JFactory::getUser(); if (!$user->registered) {echo $user->username;} ?>";}</script>";
}
?>

It does not work at all, including after escaping characters or remove the <php tags. I do not know what else to do.

I wouldn’t do it the way you are. I’d make 2 files and check if the URI has application-form. If it does, then include the 2nd file. The 2nd file would have your Javascript code in it.

This helps you maintain it better. Plus, you really don’t want to write spaghetti code. It’ll make your life a living hell when you have to debug what’s going on.

I will not elaborate on it. Just telling that I cannot create two different files. I need the script like on the example.

Again, I don’t want to sound rude, but this will be a nightmare for you to manage. It would be much easier for yourself to create 2 separate files to do the task. People often think that using 1 file for everything is easier to manage, but they end up breaking their own logic due to this. Thus creating a nightmare for themselves to debug.

Why do you want to do something in PHP that you can do in JS as well?

1 Like

Try this:

Your script simplified:

<?php // ADDED PHP TAG
$uri = $_SERVER['REQUEST_URI'];

if (strpos($uri,'application-form') !== false )
{
?>
<script>
	window.onload = function()
	{
		document.getElementById
		(
			"jform_contact_name"
		)
		.value = 
		"<?php 
			$user =& JFactory::getUser(); 
			if (!$user->registered) 
			{
				echo $user->username;
			} 
		?>
		";
	}
</script>
<?php			
}
 // ? > NOT REQUIRED

Revised script:

<?php // ADDED PHP TAG

if (strpos($uri,'application-form') !== false )
{
	$user =& JFactory::getUser(); 
	$tmpUser = 'NO USER -> USERNAME???';
	if (!$user->registered) 
	{
		$tmpUser = $user->username;
	} 

	?>
	<script>
		window.onload = function()
		{
			document.getElementById
			(
				"jform_contact_name"
			)
			.value = "<?php echo $tmpUser; ?>";
		}
	</script>

<?php			
}
// ? > NOT REQUIRED
1 Like

The script must be inside php tags

<?php 
.....
?>

I have added the relevant PHP tags.

Edit:

Using PHP HereDoc Syntax:

http://php.net/manual/en/language.types.string.php

<?php 

$uri = $_SERVER['REQUEST_URI'];

if (strpos($uri,'application-form') !== false )
{
	$user =& JFactory::getUser(); 
	$tmpUser = 'NO USER -> USERNAME???';
	if (!$user->registered) 
	{
		$tmpUser = $user->username;
	} 

    // BEWARE MUST CAN ONLY HAVE A LINEFEED AFTER ____TMP
	$script = <<< ____TMP
		<script>
			window.onload = function()
			{
				document.getElementById
				(
					"jform_contact_name"
				)
				.value = "$tmpUser";
			}
		</script>
____TMP;
// BEWARE: CAN ONLY HAVE ; FOLLOWED BY LINEFEED AFTER ____TMP
  echo $script;

	// < ? php	// TRAILING PHP REMOVED		 
}
// ? > NOT REQUIRED

Edit:
Trailing `<php NOT REQUIRED - NOW EDITED

1 Like

It does not work. It does not add the js.

Drill down on the it does not work and add these breakpoints:

<?php 
declare(strict_types=1); // PHP7 ONLY
error_reporting(-1);
ini_set('display_errors', 'true');

$uri = $_SERVER['REQUEST_URI'];

echo '<br>DEBUG: line: '.__LINE__ .' $uri ==> ' .$uri;

if (strpos($uri,'application-form') !== false )
{
	$user =& JFactory::getUser(); 

	$tmpUser = 'NO USER -> USERNAME???';

	if (!$user->registered) 
	{
		$tmpUser = $user->username;
	} 
	echo '<br>DEBUG: line: '.__LINE__ .' $tmpUser ==> ' .$tmpUser;

    // BEWARE MUST CAN ONLY HAVE A LINEFEED AFTER ____TMP
	$script = <<< ____TMP
		<script>
			window.onload = function()
			{
				document.getElementById
				(
					"jform_contact_name"
				)
				.value = "$tmpUser";
			}
		</script>
____TMP;
// BEWARE: CAN ONLY HAVE ; FOLLOWED BY LINEFEED AFTER ____TMP

echo '<br>DEBUG: line: '.__LINE__ .' $tmpUser ==> ' .$tmpUser;

  echo $script;

} // endif
// ? > NOT REQUIRED

Edit:
Just finished my nightcap and off to get some beauty sleep.

I hope some other user can debug your script.

Yes, it is 1. finished satisfactorily thanks to your help. Happy slumber.

1 Like

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