1

I currently have a script that outputs certain values in a db and it's working as intended. But now, I am trying to add a column with buttons. Very every row that the db spits out, I need a button that would open run a javascript function and then open a window. This works in HTML just fine, but because its in PHP and in #echo its not working. It has to do something with my quotes, but I can't seem to figure it out. Does someone know how to have a button embedded in an echo that can also do javascript? FYI - I would like the buttons to be within the echo so if there are 10 rows, 10 buttons will show up. Thanks!

while($row = odbc_fetch_array($rs)) 
{

echo '
    <tr>
    <td class="td" valign="top">
    <input type="submit" name="SendEmailManually" value="Submit" onclick="window.open("index.php","newWin", "width=400,height=400")" /></td>
    <td class="td" valign="top">' . $Value1 . '</td>
    <td class="td" valign="top">' . $Value2 . '</td>
            <td class="td" valign="top">' . $Value3 . '</td>
            <td class="td" valign="top">' . $Value4 . '</td>
    </tr> 
';
}
9
  • What does the generated HTML look like? Commented Jan 29, 2014 at 22:19
  • 1
    Your onclick attribute can't have double quotes within double quotes. Commented Jan 29, 2014 at 22:19
  • You could avoid this by not using inline javascript Commented Jan 29, 2014 at 22:21
  • @SSHThis - the table currently goes like this - |button|$Value1|$Value2|$Value3|$Value4 - The button shows but does nothing when I click it. Commented Jan 29, 2014 at 22:22
  • @RUJordan - I did have a function but it still did not work. I then tried the inline javascript in hoping that would work. Commented Jan 29, 2014 at 22:23

2 Answers 2

1

Please don't use inline javascript, it's ugly and even worse so when you need to escape quotes to make it work.

function openIndex() {
    window.open("index.php","newWin", "width=400,height=400");
}

throw your window code into a function, and assign the input an id (remove the onclick attribute).

Or, if no id is to be assigned, use querySelector().

var x = document.querySelector("[name='SendEmailManually']");
x.addEventListener("click",openIndex,false);

Add your event listener, and suddenly you have some good looking code that works nicely with delegated events.

Sign up to request clarification or add additional context in comments.

Comments

0

You are using double quotes, which interfere with the double quotes encasing the value of the onclick attribute. Use single quotes instead. "window.open('index.php','newWin', 'width=400,height=400')"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.