Solved

php built html code passing onclick multiple to javascript function

Posted on 2010-11-28
16
488 Views
Last Modified: 2012-05-10
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;
      }
}
0
Comment
Question by:sydneyguy
  • 7
  • 5
  • 4
16 Comments
 
LVL 1

Expert Comment

by:Mulith
ID: 34225003
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
 
LVL 3

Expert Comment

by:roynaufal
ID: 34225032
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
 
LVL 1

Accepted Solution

by:
Mulith earned 250 total points
ID: 34225108
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
 

Author Comment

by:sydneyguy
ID: 34225190
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
 
LVL 3

Assisted Solution

by:roynaufal
roynaufal earned 250 total points
ID: 34225202
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
 
LVL 1

Expert Comment

by:Mulith
ID: 34225203
Please could you send your php code so I can get a better idea of what you want the script to do.
0
 

Author Comment

by:sydneyguy
ID: 34225221
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
 
LVL 3

Expert Comment

by:roynaufal
ID: 34225254
can you show us more of the code please
we can't see where you are using the variable id
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:sydneyguy
ID: 34225259
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
 
LVL 3

Expert Comment

by:roynaufal
ID: 34225266
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
 
LVL 1

Expert Comment

by:Mulith
ID: 34225766
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
 

Author Comment

by:sydneyguy
ID: 34226789
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
 

Author Comment

by:sydneyguy
ID: 34226798
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
 
LVL 3

Expert Comment

by:roynaufal
ID: 34226833
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
 

Author Comment

by:sydneyguy
ID: 34227113
<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
 

Author Closing Comment

by:sydneyguy
ID: 34227561
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

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now