php built html code passing onclick multiple to javascript function

hi i have some php html created code and i need to pass multiple varables
this woudl be the php html created code on the fly
**************************8
$bsp = "";
$all = "dfd";
$ret.='<tr>
            
<td ><input type="button" value="edit" class="formbutton" onclick="editrec('.$bsp.', '.$all.')"; /></td>

*********************************************************
if i do one function varable to send to the function it works but more than 1 and it falls over.
have tried different configurations in the ('.$bsp.', '.$all.')";  but still will not accept the sysntax around the varables
**************** javascript function call **************
function editrec(id,bbc,vvv){
      var tbox = document.getElementById('GenDisClassComb');
      if (tbox)
      {
                  
            tbox.value = txt;
      }
}
sydneyguyAsked:
Who is Participating?
 
MulithConnect With a Mentor Commented:
Sorry my mistake, I thought the PHP and HTML were seperate.

Looking at it again the problem seems to be that your variables are not passed in apostrophes and the ; was in the wrong place. Change the code as follows from:
<td ><input type="button" value="edit" class="formbutton" onclick="editrec('.$bsp.', '.$all.')"; /></td> 

Open in new window



to

<td><input type="button" value="edit" class="formbutton" onclick="editrec(\''.$bsp.'\', \''.$all.'\');"/></td>

Open in new window


You'll see that  \ is used for character escaping so that it is not interperated by your php parser.  
0
 
MulithCommented:
I'm assuming those variable at the top are from your PHP.

I think the PHP variables need to be echoed in like this:
<td ><input type="button" value="edit" class="formbutton" onclick="editrec('<? echo $bsp; ?>', '<? echo $all;?>')"; /></td>

Open in new window



Not sure if that solves the problem completely though.
0
 
roynaufalCommented:
sorry, but I did not fully understand what you're trying to do, is it possible you rephprase?
I don't see how your using the parameters of your javascript function inside the code
maybe if you can explain a bit more what the variables are supposed to be, it would help
like show an actual example
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
sydneyguyAuthor Commented:
what i have is a php page loaded up with about 6-8  select boxes some with multiple select some with out, and there are a couple of cascades also on the page.
so on press of a submit button i use this to ajax access the data base and bring back the values for the select boxes, from here the result come back with all the recs for that clint, from here they can edit or delete buttons,
so when the button is selectes the varables that are retunted need to reload the select boxes on the page,
this is were the html is built for the page that holds the onclick function that takes the varables off to the javascipt to repopulate the page.
this is the problem that i have is when i create the html code that gets placed on the record page,
so i build up the page ......class="formbutton" onclick="editrec('.$id.')"; />..... this works but i cannot pass the other varables across to the javascript.
0
 
roynaufalConnect With a Mentor Commented:
did u try using json
in php u can use json_encode on an array, which makes it an object that u can pass as string in php in 1 variable , for example u can put all ur variable in 1 array in php, example:
$myArray['name'] = 'some value';
$myArray['test'] = 'some other value';

then $jsonArray= json_encode($myArray)

when you build your html,
you can then do this:
javascriptFunction($jsonArray)

and in the javascript function u can access the json like this:
javascriptFunction(json) {
var nameFromPhp = json.name
var testFromPhp = json.test
}


am not sure I totally answered your question, but this is one helpful approach you can potentially use here, or in other situations
0
 
MulithCommented:
Please could you send your php code so I can get a better idea of what you want the script to do.
0
 
sydneyguyAuthor Commented:
his one thing that i just found out, if the varable is $id and it is an integer it works but if it is a text value eg $sys and its text the  system throws uo and error???
0
 
roynaufalCommented:
can you show us more of the code please
we can't see where you are using the variable id
0
 
sydneyguyAuthor Commented:
bit hard to send the code as it goes from a php file which creates a html add in for each record which is then added to a html file gets a bit convoluted apart from the code that i posed above
0
 
roynaufalCommented:
hmm, can you show us more of the bit where the problem is, I kinda lost track of where the code & variables are moving around
0
 
MulithCommented:
It's unlikely that we'll be able to help you without seeing the code,at least the PHP side that creates the HTML. The problem could range from a mistake in your syntax -  to the order in which different elements are called. Without looking at the nuts and bolts it's only guess work.

I have tested the code above. It is correct and working so the problem is most likely to be in:
a)the other parts of your PHP script or
b)your javascript logic/syntax or
c)your AJAX logic/syntax

without the benefit of code we cannot determine where to start looking.
0
 
sydneyguyAuthor Commented:
below is the simple code that needs to work

$sp = " - ";
$bsp = "";
$all = "dfd";
$id = '1234'
$name = '$jones';
/* ********************************** at the end of this code is were the probem is at $id,$name *****/            
$ret.='<tr><td ><input type="button" value="edit" class="formbutton" onclick="getSBMsg()"; /></td>
<td ><input type="button" value="edit" class="formbutton" onclick="editrec('.$id.',.'$name')"; /></td>

/* ********************* the java reciever *************** */
function editrec(id,name)\
{
alert (id);
alert(name);
 }      
0
 
sydneyguyAuthor Commented:
it wont be the ajax as the data is returned ok its just either the syntax for the creation of the html or the java reciver i beleive
0
 
roynaufalCommented:
what is the exact error do you get when you click the button ?
because as code,it looks right
is $jones supposed to be the actual value in $name, or is it also another variable?

and is the above the actual code or something you wrote up quickly ?
it doesn't look right, should be:
$ret.='<tr><td ><input type="button" value="edit" class="formbutton" onclick="getSBMsg()"; /></td><td ><input type="button" value="edit" class="formbutton" onclick="editrec('.$id.',.' .$name .')"; /></td>';

(u had a prob in the dots/quote around name
0
 
sydneyguyAuthor Commented:
<td ><input type="button" value="edit" class="formbutton" onclick="editrec('.$bsp.', '.$all.')"; /></td>
this is the correct code from above sorry about that
i still have the issue with the system not allowing text to be sent as well uustrebuilding the system to make sure thats its not something else silly back in a while with an oerating syetem
0
 
sydneyguyAuthor Commented:
bloody ripper..... huston we have lift off
you are both right, i wrote some code to drop out the html code to a server doc to actually see what is happening and your were right we were all right, it was a syntax problem.
end of the day the code that was being generated was
<td ><input type="button" value="edit" class="formbutton" onclick="editrec(1039,term)"; /></td> ......
which was my problem using the code
<td ><input type="button" value="edit" class="formbutton" onclick="editrec(''.$id.'',''.$sys.'')"; /></td> .....
using the solution and the code
<td ><input type="button" value="edit" class="formbutton" onclick="editrec(\''.$id.'\',\''.$sys.'\')"; /></td>  with the back space we now get
<input type="button" value="edit" class="formbutton" onclick="editrec('1039','smith')"; />
I tried this earlyer but due to the lack of sleep and the system using smarty i must have forgotten to recompile the system so the data was incorrect but with fresh eyes and your great ideas and help we are once again are on the track.
which is what we need. as for the array this will be used in the next part of a problem.
so once again you guys are absolute stars
and thanks for all your input
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.