0

Hello I am wanting to echo a PHP Variable to a buttons value then send the buttons value to an input text. I was able to echo the button with the variable but when I click the button it does nothing. I'm not sure why, because when I do this without the PHP just the script, and inputs it works perfectly. I am just missing something I know it, I can't find much info on how to pass php to a button then pass the button value to an input text.

Here's the script that passes the button value to the input text:

$( document ).ready(function() {
var $theButtons = $(".button");
var $theinput = $("#theinput");
$theButtons.click(function() {
    $theinput.val(this.value);
});
});

Here's the PHP that echos the variable as a button:

        require "config.php";  // database connection



    $in=$_GET['txt']; 
    if(!ctype_alnum($in)){  // 
    echo "Search By Name, or Entry ID";
    exit;
    }

    $msg="";
    $msg="";
    if(strlen($in)>0 and strlen($in) <20 ){ 
    $sql="select name, entry, displayid from item_template where name like '%$in%' LIMIT 10"; // the query
    foreach ($dbo->query($sql) as $nt) {
    //$msg.=$nt[name]."->$nt[id]<br>";
    $msg .="<table style='table-layout:fixed;'> // Just the start of my table
    <tr>
    <td>Name</td>
    <td>Entry ID</td>
    <td>Display ID</td>
    </tr>
    <tr>
    <td align=center><a href=http://wowhead.com/item=$nt[entry]>
    $nt[name]</a>
    </td>
    <td>$nt[entry]</td>
    <td>
    <input type=button class=button value=$nt[displayid]> // The Value I need echoed out in a button is $nt[displayid]
    </td>
    </tr>
    </table>"; // end of my table}
}
$msg .='';
echo $msg;

Not that it matters but here is the input text

<input type="text" id="theinput"/>
Jamie Roads
  • 23
  • 1
  • 7
  • 1
    kindly fix the code, some block statements are not close properly – Oli Soproni B. Aug 13 '15 at 01:14
  • 1
    This is the full php script? You never return/echo `$msg` and you haven't closed your if statements `}` – NewToJS Aug 13 '15 at 01:25
  • The PHP Script is fine, it works, no errors – Jamie Roads Aug 13 '15 at 01:33
  • @JamieRoads I can see multiple errors. check for error reports on your webhost. It will normally display as a file named `error_log` – NewToJS Aug 13 '15 at 01:35
  • There is no errors. As I said already. – Jamie Roads Aug 13 '15 at 01:36
  • @JamieRoads I see you have updated the source code. Your original post was incorrect as you didn't have any output for `$msg` so *"as I said already"* it had errors. – NewToJS Aug 13 '15 at 01:39
  • It didn't have any errors, I just didn't post relevant code – Jamie Roads Aug 13 '15 at 01:43
  • 1
    @JamieRoads I can only go by the source code you display. From what you were showing it was incorrect meaning it wouldn't do anything as you didn't attempt to output anything. Since you want to play smart to those willing to help you I wish you the very best of luck as I'm not going to bother. I believe many who read this will decide to help someone worth the time and your attitude doesn't seem to fit. – NewToJS Aug 13 '15 at 01:47
  • 1
    @JamieRoads I did quote that as I was pointing out something for the second time, just thought I would use your own word and quote them. Good luck getting help! I think you'll need it when people read the comments. Next time post the relevant source code and people will see you have no errors rather than getting this sort of attitude from someone asking for help. – NewToJS Aug 13 '15 at 01:49
  • 1
    Your HTML attributes should be quoted, especially when putting dynamic values into them – Phil Aug 13 '15 at 02:05

2 Answers2

2

Make it easy on yourself and try to make your code easy to read. I personally prefer to write my html cleanly and outside of echo statements like so:

Html

if (strlen($in) > 0 and strlen($in) < 20) {
    $sql = "select name, entry, displayid from item_template where name like '%{$in}%' LIMIT 10"; // the query
    foreach ($dbo->query($sql) as $nt) {
        //$msg.=$nt[name]."->$nt[id]<br>";
        ?>
        <table style="table-layout:fixed;">
            <tr>
                <td>Name</td>
                <td>Entry ID</td>
                <td>Display ID</td>
            </tr>
            <tr>
                <td align="center">
                    <a href="http://wowhead.com/item=<?=$nt['entry'];?>"><?=$nt['name'];?></a>
                </td>
                <td><?=$nt['entry'];?></td>
                <td>
                    <input type="button" class="button" value="<?=$nt['displayid'];?>">
                </td>
            </tr>
        </table>
        <?php
    }
}

Javascript

$( document ).ready(function() {
    var $theButtons = $(".button");
    var $theinput = $("#theinput");
    $theButtons.click(function() {
        // $theinput is out of scope here unless you make it a global (remove 'var')
        // Okay, not out of scope, but I feel it is confusing unless you're using this specific selector more than once or twice.
        $("#theinput").val(jQuery(this).val());
    });
});
Blake
  • 2,294
  • 1
  • 16
  • 24
  • In his anonymous function? – Blake Aug 13 '15 at 02:06
  • Yes. `$theinput` is most certainly in scope within the `click` handler – Phil Aug 13 '15 at 02:06
  • Explain. It's a local variable. – Blake Aug 13 '15 at 02:07
  • I don't know what you guys are even talking about LOL but I can remove the var is it needs to be removed. – Jamie Roads Aug 13 '15 at 02:08
  • @Blake https://jsfiddle.net/v8ynr3e2/. There was nothing wrong with OP's JS. I suggest you have a good read of [What is the scope of variables in JavaScript?](http://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript) – Phil Aug 13 '15 at 02:11
  • @JamieRoads no, don't change your JS at all. The rest of this answer is right on the money though; `echo`-ing out great chunks of HTML is a bad idea – Phil Aug 13 '15 at 02:13
  • @Phil I updated my js to correct it. I don't mind being wrong, especially if it doesn't break anything. – Blake Aug 13 '15 at 02:14
  • Alright so, how do I get my the php variables that I echoed out in the button, to where when the button is click it puts it in the input text – Jamie Roads Aug 13 '15 at 02:15
  • @Blake OP's JS code was more efficient. In your answer, you're looking up the `#theinput` element every time a button is clicked. – Phil Aug 13 '15 at 02:15
  • @JamieRoads Just remember that your selector `$(".button")` is looking for a **class** of button, not a **type** of button. That would be a different selector. – Blake Aug 13 '15 at 02:15
  • @Phil Pre-mature optimization. That is a unique selector (or should be, since it's an id). That one cycle will surely destroy his browser's DOM. – Blake Aug 13 '15 at 02:17
  • Okay so make sure my button is ? – Jamie Roads Aug 13 '15 at 02:18
  • 1
    @JamieRoads You should be able to use my code copy/paste. – Blake Aug 13 '15 at 02:19
  • Question, the php is in an external file, I forgot to mention, that was totally my bad. See I am using AJAX live search, and the way it's setup is my script calls the php file. So how will echoing out what you've made for the php and html work? – Jamie Roads Aug 13 '15 at 02:22
  • To be extra safe (and because I have no idea about the kind of data in each *displayid* record), you should use `value="= htmlspecialchars($nt['displayid']) ?>"`. – Phil Aug 13 '15 at 02:22
  • 2
    @JamieRoads and now we're back to the original argument in the comments above; you haven't provided all the relevant information :( – Phil Aug 13 '15 at 02:23
  • The data in the displayid records are of 6-8 numbers long. It's all numbers – Jamie Roads Aug 13 '15 at 02:24
  • @Phil no that argument was because I didn't show an echo, which is not being used for anything. This time I will admit I forgot that my PHP was in an external file. – Jamie Roads Aug 13 '15 at 02:25
  • If I could provide what I'm looking to do, an example if you will. Go here http://wow-v.com/create.php?tool=mob in the Displayid Finder Tool, Search by name, and enter, "Thrall", and you will see that all the results show a button to select that Displayid and send it back to an input text. This is what I want to achieve, I want everything to be to where the page doesn't refresh and loose all the user input data as it would be pretty annoying to the user. – Jamie Roads Aug 13 '15 at 02:28
  • 1
    @JamieRoads Your original question doesn't mention anything about AJAX search results. – Blake Aug 13 '15 at 02:29
  • So? it doesn't matter about that. I am just saying since I am using Ajax Live Search, the PHP file is external. It's the way it was set up. The Ajax search has nothing to do with anything. The PHP file being external does. – Jamie Roads Aug 13 '15 at 02:30
  • Why at the end of the HTML titled code is there, ` – Jamie Roads Aug 13 '15 at 02:32
  • 1
    It is unknown if you have more php code under your snippet and I had to close your `if` and `foreach` loop for functional PHP code. – Blake Aug 13 '15 at 02:33
0

Ok, here goes...

  1. Use event delegation in your JavaScript to handle the button clicks. This will work for all present and future buttons

    jQuery(function($) {
        var $theInput = $('#theinput');
        $(document).on('click', '.button', function() {
            $theInput.val(this.value);
        });
    });
    
  2. Less important but I have no idea why you're producing a complete table for each record. I'd structure it like this...

    // snip
    
    if (strlen($in)>0 and strlen($in) <20 ) :
    // you really should be using a prepared statement
    $sql="select name, entry, displayid from item_template where name like '%$in%' LIMIT 10";
    ?>
    <table style="table-layout:fixed;">
    <thead>
    <tr>
        <th>Name</th>
        <th>Entry ID</th>
        <th>Display ID</th>
    </tr>
    </thead>
    <tbody>
    <?php foreach ($dbo->query($sql) as $nt) : ?>
    <tr>
        <td align="center">
            <a href="http://wowhead.com/?item=<?= htmlspecialchars($nt['entry']) ?>"><?= htmlspecialchars($nt['name']) ?></a>
        </td>
        <td><?= htmlspecialchars($nt['entry']) ?></td>
        <td>
            <button type="button" class="button" value="<?= htmlspecialchars($nt['displayid']) ?>"><?= htmlspecialchars($nt['displayid']) ?></button>
        </td>
    </tr>
    <?php endforeach ?>
    </tbody>
    </table>
    
    <?php endif;
    
Phil
  • 157,677
  • 23
  • 242
  • 245
  • When I put this in my html file nothing shows up it's a blank page – Jamie Roads Aug 13 '15 at 02:48
  • @JamieRoads You may have missed a closing brace or something. I've update my answer to better show you where the code goes; just replace everything from the `if(strlen(...` line down – Phil Aug 13 '15 at 02:54
  • Hm just did and still the same. I really appreciate your time and help guys. Just want ya'll to know, oh and your patience of my lack of coding knowledge. – Jamie Roads Aug 13 '15 at 02:59
  • The `exit;` in your first `if` statement is causing the page to be unfinished, you have no `

    ` or `

    – Phil Aug 13 '15 at 03:06
  • I don't see an `exit;` could it be something to do with UTF-8 encoding? – Jamie Roads Aug 13 '15 at 03:10
  • In your original code, not mine. You have `if (!ctype_alnum($in)) { ...; exit; }`. If I go to http://trinity-creator.com/css/?txt=home I see results but I don't see the JS I provided anywhere – Phil Aug 13 '15 at 03:11
  • @JamieRoads you can only use jQuery **after** it's included in the page. Also, you have no element with `id="theinput"`. – Phil Aug 13 '15 at 03:18