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"/>
10
  • 1
    kindly fix the code, some block statements are not close properly Commented Aug 13, 2015 at 1:14
  • 1
    This is the full php script? You never return/echo $msg and you haven't closed your if statements } Commented Aug 13, 2015 at 1:25
  • 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. Commented Aug 13, 2015 at 1: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. Commented Aug 13, 2015 at 1:49
  • 1
    Your HTML attributes should be quoted, especially when putting dynamic values into them Commented Aug 13, 2015 at 2:05

2 Answers 2

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());
    });
});
Sign up to request clarification or add additional context in comments.

23 Comments

In his anonymous function?
@JamieRoads You should be able to use my code copy/paste.
@JamieRoads and now we're back to the original argument in the comments above; you haven't provided all the relevant information :(
@JamieRoads Your original question doesn't mention anything about AJAX search results.
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.
|
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;
    

7 Comments

When I put this in my html file nothing shows up it's a blank page
@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
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.
The exit; in your first if statement is causing the page to be unfinished, you have no </body> or </html> closing tags in the rendered output if there's no txt query variable
I don't see an exit; could it be something to do with UTF-8 encoding?
|

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.