0

I am trying to create a javascript function where the first argument is a number and the second arg is a string. My code is below, I have added a comments here to make it easier to read.

while ($dbsearch = mysqli_fetch_assoc($run_query)) {

    //define 3 vables. Example $dbu = 'Bill.Gates'
    //Example $id = 123
    // example $func using the values above should say add(123,'Bill.Gates')

    $dbu = $dbsearch['Username'];
    $id = $dbsearch['PlayerID'];
    $func = "add(" . $id . ",'" . $dbu . "')";

    //this is a string to output the results of an SQL search. It is working fine. The 
    // $func is inserted toward the end in the submit button.

    echo "<tr><td>" . $id . "</td><td>" . $dbu 
    . "</td><td><input type='submit' id='PlayerAdded" . $id 
    . "' value='Add' onclick='" . $func . "'></input></td></tr>";
}

The purpose of the submit button is to add a players name as a registered player in an event.

I have tried changing the $func variable to just output the $id for both arguments and that worked, but I can't get it to output a string (name of person).

How can I fix this please?

Added: Sorry if my question was not clear. I am wanting to know how to get the add function to accept a string as an argument. For example

add(123,'Bill Gates');
7
  • What is your problem? $dbsearch['Username'] is empty? Commented Aug 28, 2015 at 8:39
  • 2
    I'm finding it hard to understand what it is you're asking Commented Aug 28, 2015 at 8:39
  • 4
    I think the problem is the quotes inside the add function are breaking your HTML code, isn't it? Commented Aug 28, 2015 at 8:40
  • 1
    Good find Peter! If you want to open a sting with double quotes and wish to use them within a string you should back slash them \" I would use double quotes for element arrtibutes and use single quotes to place your javascript arguments. Example echo "<tr><td>".$id ."</td><td>".$dbu."</td><td><input type=\"submit\" id=\"PlayerAdded".$id."\" value=\"Add\" onclick=\"add('".$id."','".$dbu."');\"></input></td></tr>"; Commented Aug 28, 2015 at 8:45
  • 1
    @Tunna182 When you use php to output html elements it will replace single quotes with double quotes so your onclick attribute wasn't valid. I'm not sure why single quotes are replaced with double quotes but I found that out the hard way. If you use php to echo the input you can inspect element and you will notice the replacement of quotes. I also learn in my spare time too. It's good that you want to know the reason(s) why things don't always work rather than just copy/paste the answer/solution. Commented Aug 28, 2015 at 15:28

2 Answers 2

1

Try commenting this line:

// $func = "add(" . $id . ",'" . $dbu . "')";

And then in your echo:

echo "
<tr>
   <td>" . $id . "</td>
   <td>" . $dbu . "</td>
   <td>
       <input type='submit' id='PlayerAdded" . $id . "' value='Add' onclick='add(" . $id . ", \'" . $dbu ."\');' />
   </td>
</tr>";
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks heaps for the suggestion, but this didn't fix the issue. This stopped the add button from doing anything :(
@Tunna182 but the code isn't broken if your check it with a browser debugger?
To be totally honest, I spent so long yesterday trying different changes I don't remember what was broken and what wasn't. At the moment the code in there works and that's the main thing I'm worried about. I have copy, pasted the code from @newtoJS and that did the trick for me :) Now I have to learn about escaping quotes. I am experiencing a similar difficulty now, but will keep going with it. If I get really stuck I'll post another question.. (trying to create a function to delete a child element using instructions from w3 schools).
0

You are not escape quotes.

$dbsearch = array(array('Username' => 'First User', 'PlayerID' => 1),
                  array('Username' => 'Second User', 'PlayerID' => 2));

echo '<table>';
foreach ( $dbsearch as $db ) {

  $func = 'add(' . $db['PlayerID'] . ', \'' . $db['Username'] . '\')';
  echo '<tr>
          <td>' . $db['PlayerID'] . '</td>
          <td>' . $db['Username'] . '</td>
          <td><input type="submit" id="PlayerAdded' . $db['PlayerID'] . '" value="Add" onclick="' . $func . '"></td>
        </tr>';
}
echo '</table>';

?>

<script type="text/javascript">
  function add( id, name ) {
    document.write( 'Id: ' + id + ', Name: ' + name );
  }
</script>

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.