Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Say I have 3 variables, id, name, number, on a php script, $name, $number, $id

On the PHP, I create a button

    print "<td><form><input type='button' name='edit' id='edit' onclick=ed($name, $number, $id) value='Edit' /></td>";

I want those 3 variables sent to my javascript.

The function call seems to work just fine if I use

onclick=ed(this.id)

and modify the function header, and this passes the string "edit" but that's not quite useful.

function header in javascript is

function ed(name, number, id) {
//stuff here
}

For whatever it's worth using this code gets me an error on the html, something about an unexpected } on line2 of the html document.

Edit:I should clarify I said that code gives me errors so someone didn't just say "Well use this!" when I already expected it not to work.

Using this:

<input type='button' id='$id' onclick=ed(this.id) value='Edit' />

Allows me to send the value in $id to the javascript function because it was saved in the id field. Something along those lines is what I'm hoping for, but I'm unable to find if there's any way to do that. edit: for 3 variables.

Edit again:

Using:

<form><input type='button' name='$name' id='$id' title='$number' onclick='ed(this.name,this.title,this.id)' value='Edit' /></form>

sent the values of all 3 php variables to the javascript function.

It's not pretty, but it works.

share|improve this question
You need to understand the difference between client and server side scripting – Mr. Alien Apr 20 at 6:54
your form tag isn't closed – user2264587 Apr 20 at 6:57
I fully understand the difference between client and server-side scripting, but thanks for the help. I was hoping someone could suggest to me a method of storing the variables for sending via javascript. For instance, setting id='$id' on the button then sending this.id is sending the value of the php variable $id to the javascript function. I was hoping someone had a recommendation that would allow me to send all 3. – Dirgon Apr 20 at 6:58
also you need to quote 'ed($name, $number, $id)' in the javascript – user2264587 Apr 20 at 7:02
and if you want to send variables to your javascript with php, my preference is to have one variable defined in a script tag in the head, that has all of the variables as an object echo 'var serverVars = '.json_encode(array("id"=>$id ... )); – user2264587 Apr 20 at 7:05
show 1 more comment

3 Answers

I think the problem is with your quotes so try this

    print '<td><form><input type="button" name="edit" id="edit" onclick=ed('.$name.','.$number.','.$id.') value="Edit" /></td>'
share|improve this answer
I should've proofread my question more, but it's late. -_-; The problem isn't the code (though that was the problem, it's been fixed.) the problem is you can't literally do it the way I typed it. I meant it more as a psuedo-code. – Dirgon Apr 20 at 7:09

You can only send 3 value like this:-

onclick="javascript:func_name('<?php echo $var1 . "," . $var2 . "," . $var3;?>');

In the js function take only one argument and then split 3 values using "," as a delimiter.

share|improve this answer

For custom attributes define data-attributes, e.g. as shown on Mozilla Dev:

<div id="user" data-id="1234567890" data-user="johndoe" data-date-of-birth>John Doe
</div>

var el = document.querySelector('#user');

// el.id == 'user'
// el.dataset.id === '1234567890'
// el.dataset.user === 'johndoe'
// el.dataset.dateOfBirth === ''

el.dataset.dateOfBirth = '1960-10-03'; // set the DOB.

// 'someDataAttr' in el.dataset === false

el.dataset.someDataAttr = 'mydata';
// 'someDataAttr' in el.dataset === true

In your case, it seems you only need data-number as the other two are standard properties of the DOM-element.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.