Avatar of easyrider439
easyrider439

asked on 

php and Javascript Help

Hey Experts, How can I get that php to work in this JavaScript script that I wrote. To my understanding php is executed during loading and variables are loaded into the browser cant I take that php variable and make it a JavaScript variable or How would That work? Hope that makes sense.

in my code I have a php variable echoed and it doesnt work what can I do?
function addRow(divname) {
    var totalrows = document.getElementById(divname).rows.length-2;
    var newdiv = document.createElement('tr');
    var products = '<?php echo $products; ?>';    
    newdiv.innerHTML = '<td>' + (totalrows + 1) + '</td>';
    newdiv.innerHTML += '<td>' + products + '</td>';
    document.getElementById(divname).appendChild(newdiv);    
}

Open in new window

JavaScriptAJAX

Avatar of undefined
Last Comment
easyrider439
Avatar of jhp333
jhp333
Flag of United States of America image

Your code itself looks fine, but it'll add the same products when you add a new row.
If you want to add different products, you can:
1. use php function json_encode( ) to pass an array to javascript, if all the values are determined when loading.
2. use AJAX calls to get a new value from the server every time.
Avatar of easyrider439
easyrider439

ASKER

the php part doesnt show up, its blank not sure why
Avatar of jhp333
jhp333
Flag of United States of America image

Try something like:
var products = '<?php echo "OK?", $products; ?>';

If you see "OK?", then $products is empty. Check your php code.
Avatar of easyrider439
easyrider439

ASKER

yea I cant see "ok" or the products, I must be missing something my code is below if anybody wants to look through it and see if anything is wrong. Thanks experts



this is index.php

<?php

$title = "Invoicing Home";

$default1 = 'tabbertabdefault';

if (!include '../header2.inc')
	{
	echo 'Stop Error! 0x00000333';
	die();
	}

if (!include '../config/config.php')
	{
	echo 'Stop Error! 0x00000333';
	die();
	}

if (!include '../config/functions.php')
	{
	echo 'Stop Error! 0x00000333';
	die();
	}

if (!include '../config/mysql_connect.php')
	{
	echo 'Stop Error! 0x00000333';
	die();
	}

if (!include 'functions/main.php')
	{
	echo 'Stop Error! 0x00000333';
	die();
	}
if (!include 'functions/products.php')
	{
	echo 'Stop Error! 0x00000333';
	die();
	}

?>

<script type="text/javascript" src="script/add_row.js"></script>
<form action="tableaddrow_nw.html" method="get">

<center>
<table cellpadding="0" cellspacing="0" id="invoice">

<tr>

	<td colspan="4"><center>Client: <?php clients(); ?></center></div></td>

</tr>
<tr>
	<td>1</td>
	<td><?php echo $pro; ?></td>
</tr>
<tr>	
        <td><div id="select"></div></td>
</tr>
</table>
<table cellpadding="0" cellspacing="0" width="1050" border="1">

	<tr>
		<td>
	
			<p>
			<input type="button" value="Add" onclick="addRow('invoice');" />
				<input type="button" value="Remove" onclick="removeRowFromTable()" />
			</p>		


		<td>
			Total:	<input type="text" name="tot" value="0">

		</td>
	</tr>
</table>
</center>

</form>

this is addrow.js

function addRow(divname) {
    var totalrows = document.getElementById(divname).rows.length-2;
    var newdiv = document.createElement('tr');
    var products = '<?php echo `"Ok"`, $pro; ?>';    
    newdiv.innerHTML = '<td>' + (totalrows + 1) + '</td>';
    newdiv.innerHTML += '<td>' + products + '</td>';
    document.getElementById(divname).appendChild(newdiv);    
}

Open in new window

Avatar of jhp333
jhp333
Flag of United States of America image

Why did you surround "Ok" with grave accents(`)? By doing that, you told PHP to run a shell command "Ok", which probably failed, thus printed nothing.
Avatar of easyrider439
easyrider439

ASKER

Ok I took those out and its still not working, theres gotta be something wrong with the javascript casue when I put $pro anywhere else in the index page it works just not in the jaavascript. could it be that there in seperate pages and I included them in the index page?
Avatar of dr_Pitter
dr_Pitter

Hi,

in your code, it looks like your javascript is in a seperate file named "script/add_row.js". You're not writing PHP-code into a js-file, are you? You have to put your javascript into index.php
SOLUTION
Avatar of jhp333
jhp333
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of easyrider439
easyrider439

ASKER

Allright I moved the script  to the header index.php of the page and now it doesnt work saying "addRow" needs to be defined also it says an syntax error  unexpeceted number on line 121. any help with that??
<?php

$title = "Invoicing Home";

$default1 = 'tabbertabdefault';

if (!include '../header2.inc')
	{
	echo 'Stop Error! 0x00000333';
	die();
	}

if (!include '../config/config.php')
	{
	echo 'Stop Error! 0x00000333';
	die();
	}

if (!include '../config/functions.php')
	{
	echo 'Stop Error! 0x00000333';
	die();
	}

if (!include '../config/mysql_connect.php')
	{
	echo 'Stop Error! 0x00000333';
	die();
	}

if (!include 'functions/main.php')
	{
	echo 'Stop Error! 0x00000333';
	die();
	}
if (!include 'functions/products.php')
	{
	echo 'Stop Error! 0x00000333';
	die();
	}

?>
<script type="text/javascript">

function addRow(divname) {
    var totalrows = document.getElementById(divname).rows.length-2;
    var newdiv = document.createElement('tr');
    var products = '<?php echo $pro; ?>';    
    newdiv.innerHTML = '<td>' + (totalrows + 1) + '</td>';
    newdiv.innerHTML += '<td>' + products + '</td>';
    document.getElementById(divname).appendChild(newdiv);    
}

function removeRowFromTable()
{
  var tbl = document.getElementById('invoice');
  var lastRow = tbl.rows.length;
  if (lastRow > 2) tbl.deleteRow(lastRow - 1);
}

</script>


<form action="tableaddrow_nw.html" method="get">

<center>
<table cellpadding="0" cellspacing="0" id="invoice">

<tr>

	<td colspan="4"><center>Client: <?php clients(); ?></center></div></td>

</tr>
<tr>
	<td>1</td>
	<td><?php echo $pro; ?></td>
</tr>
<tr>	
        <td><div id="select"></div></td>
</tr>
</table>
<table cellpadding="0" cellspacing="0" width="1050" border="1">

	<tr>
		<td>
	
			<p>
			<input type="button" value="Add" onclick="addRow('invoice');" />
				<input type="button" value="Remove" onclick="removeRowFromTable()" />
			</p>		


		<td>
			Total:	<input type="text" name="tot" value="0">

		</td>
	</tr>
</table>
</center>

</form>

Open in new window

SOLUTION
Avatar of dr_Pitter
dr_Pitter

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
ASKER CERTIFIED SOLUTION
Avatar of jhp333
jhp333
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of easyrider439
easyrider439

ASKER

yea its something in the php stopping it from displaying
Avatar of easyrider439
easyrider439

ASKER

got it to work I needed to use the str_replace string like jhp333 suggested and I rewrote my php script Thanks Guys I owe ya
JavaScript
JavaScript

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.

127K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo