1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Number values in database work fine, text don't.

Discussion in 'PHP' started by JoshuaEir, Dec 28, 2020.

  1. #1
    When the button is pressed in the following string, the number variables work fine, but the string ($title1) doesn't. Can't get it.

    foreach ($dbo->query($q1) as $row) {
    
    $title1  = $row['ProductName'];
    
    $string1 =  "
    
    <button id = \"\" onclick = \"SaveProductItems( $title1, $id, $cost,$quantity)\">Submit</button>
       
    ";
    
    
    
    PHP:

    function SaveProductItems(title, id,cost,quantity){
    
    let t = title;
    
    Code (JavaScript):

     
    Solved! View solution.
    JoshuaEir, Dec 28, 2020 IP
  2. JoshuaEir

    JoshuaEir Member

    Messages:
    59
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    28
    #2
    Is there any way to do this:

    $titleID = "titleID" . $counter;
    
    $string1 =  "
    
    div class=\"col\">
          <h4><center><p id =\"\"  >Title</p></center></h4>       
         
        <center>      <p  >      <input id = \"$titleID\"  type=\"text\" name=\"title\" value = \"$title1\"placeholder=\"title1\"></p></center>
        </div>
       
    <button id = \"\" onclick = \"SaveProductItems( $titleID)\">Submit</button>
    
    ";
    
    
    PHP:

    function SaveProductItems(titleID){
    
    var val1 = document.getElementById(titleID).value;
       
    
    Code (JavaScript):
    thanks...
     
    JoshuaEir, Dec 28, 2020 IP
  3. JoshuaEir

    JoshuaEir Member

    Messages:
    59
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    28
    #3
    Well I did this...both, here:

    
    
    foreach ($dbo->query($q1) as $row) {
    
    $counter = $counter + 1;
    
    $titleID = "titleID" . $counter;
    
    $descID = "descID" . $counter;
    
    $costID = "costID" . $counter;
    
    $quantityID = "quantityID" . $counter;
    
    
    string1 =  "
    <div class=\"container\">
      <div class=\"row\" >
    
     
      
     
        <div class=\"col\">
          <h4><center><p id =\"\"  >Title</p></center></h4>     
       
        <center>      <p  >      <input id = \"$titleID\"  type=\"text\" name=\"title\" value = \"$title1\"placeholder=\"$title1\"></p></center>
        </div>
     
     
        <div class=\"col\">
          <h4><center><p id = \"\">Desc</p></center></h4>
       
          <textarea wrap id = \"$descID\" name=\"text\" rows=\"5\" cols=\"34\">$description</textarea>
          </div>
    
    <button id = \"\" onclick = \"SaveProductItems( $titleID, $descID, $costID,$quantityID)\">Submit</button>
     
    
    
    PHP:
    function SaveProductItems(title, desc,cost,quantity){
     
     
     
        var val1 = document.getElementById(title.id).value;
        var val2 = document.getElementById(desc.id).value;
        var val3 = document.getElementById(cost.id).value;
        var val4 = document.getElementById(quantity.id).value;
    Code (JavaScript):
     
    JoshuaEir, Dec 28, 2020 IP
  4. Efetobor Agbontaen

    Efetobor Agbontaen Active Member

    Messages:
    136
    Likes Received:
    41
    Best Answers:
    5
    Trophy Points:
    85
    #4
    If I understand this correctly, you are trying to render those php variables in the HTML button.
    In this case, this


    should be this:
    
    $string1 = "<button id = \"\" onclick = \"SaveProductItems( ".$title1.", $id, $cost,$quantity)\">Submit</button> ";
    
    PHP:
    Notice this (".$title1.").
    If I am right about your intentions. then this should work.
     
    Efetobor Agbontaen, Dec 28, 2020 IP
  5. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #5
    Let's leap forward a decade and do something like this, but I'd probably change counter to something more exact like $row['product_id']. By standardising the names of the input variables you avoid having inputs that change all over the place and you're passing the simplest info to javascript.

    Your onclick event should also be a listener but we'll save that for another day. Onclick is fine in this instance.

    {} curly brackets tell the server the exact name of a variable.
    '' single quotes mean you don't have to escape the string and makes it easier to read. The only place I left doubles in was in the title because it could contain something like "O'Keefes Elixir" that will be buggy with single quotes.

    <?php
    foreach ($dbo->query($q1) as $row) {
    
    $counter = $counter + 1;
    
    // the H4 should be styled with css, shouldn't have a <p> tag inside it
    
    $string1 =  "
    <div class='container'\"'>
        <div class='row' >
            <div class='col'>
                <h4 id=''>Title</h4>  
    
                <p style='text-align: center;' ><input id = '{$titleID}'  type='text' name='title' value = \"{$title1}\" placeholder=\"{$title1}\"></p>
            </div>
    
            <div class='col'>
                <h4 id=''>Desc</h4>
    
                <textarea wrap id='desc{$counter}' name='text' rows='5' cols='34'>{$description}</textarea>
            </div>
    
            <button id='' onclick='SaveProductItems({$counter})'>Submit</button>
        </div>
    </div>";
    
    ?>
    <script>
    function SaveProductItems(counter){
        var val1 = document.getElementById('title'+counter).value;
        var val2 = document.getElementById('desc'+counter).value;
        var val3 = document.getElementById('cost'+counter).value;
        var val4 = document.getElementById('quantity'+counter).value;
        }
    </script>
    Code (markup):
     
    sarahk, Dec 28, 2020 IP
  6. #6
    Use the single quote (') sign around $title1 when passing it via javascript.
    Like this:

    $string1 = "<button id = \"\" onclick = \"SaveProductItems('{$title1}', $id, $cost, $quantity)\">Submit</button>
     
    JEET, Dec 29, 2020 IP
    qwikad.com likes this.
  7. JoshuaEir

    JoshuaEir Member

    Messages:
    59
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    28
    #7
    Thank you everyone. I tested and Jeet got it right. I think it is so neat now that I can pass strings!

    Enjoy your new year,
    Joshua
     
    JoshuaEir, Dec 31, 2020 IP
    qwikad.com and JEET like this.