0

it's the 2nd time i post here. Again i'm not too proficient in logic nor PHP, so please bear with my messy codes :)

I have an array of textboxes with value and there are array of buttons next to it. i've managed to display the value of the text box with the corresponding button into a dialog box (thanks to Mr.Barmer from here)

Then i intend to save the specific value from the textbox that i have captured into the database, here i have no clue what i should do. I try to "echo" the value of the textbox by insert it into

($link=((document.getElementById('link$i').value)))

but i got no result.

My question is: what should i add to my codes to retrieve the value from the textbox, when i clicked the corresponding button and save it to database, or simply capture the value and store it in

(ex:$link=$_POST['link'])

then post it using echo $link?

here's the piece of my PHP codes i'm talking about:

$data = mysql_query("SELECT * from tempimg"); 
while($hasil = mysql_fetch_array($data)){     
    $i++;
    echo "<tr>
        <td align='center'><input type= checkbox name=check[] 
                value=$hasil[idFoto]</td>
        <td align='center'><img src=$hasil[thumbPath]></td>
        <td align='center'>$hasil[imgName]</td>
        <td align='center'>$hasil[thumbPath]</td>
        <td align='center'>$hasil[Path]</td>
        <td align='center'>

        <label class=text_label> $hasil[imgLink]</label>
        <div class=edit></div>
        <input type=text align=center value=$hasil[imgLink] 
                name=link[{$hasil['idFoto']}] id=link$i />
        <div class=clear></div> 

        <td align=center>

        <button type=submit =
            onClick=\"return confirm('you clicked button $i 
                with ID: $hasil[idFoto] '+'value: '
                +(document.getElementById('link$i').value))\">
        <img src=images/sav.png alt=search-btn id=img />
        </button>

        </td>
        <td align=center><img src=images/del.png></img></td>";    
}

and here's the SS of the page that i'm working now http://imgur.com/FtKAs8K

just need to get the "70" and the "test.com" value and "echo" it, the rest of database query i'll try to work out myself. So i humbly request help from the people here, please help me. Thank you for your attention

14
  • You are attempting to display values retrieved via Javascript in a PHP script. This is going to require either some AJAX or submitting the values to an additional PHP page via a URL query. Take a look at this page: coderslexicon.com/… Commented Jul 18, 2013 at 15:21
  • @DevlshOne Um Mr.DevlshOne,is that means i can't directly "echo" it on my page? or is there any other method then using ajax or submit the value to another php page? i appreciate your quick reply :) Commented Jul 18, 2013 at 15:26
  • I took the database part out of the title, because it appears to be a separate issue and not really part of this post. Commented Jul 18, 2013 at 15:29
  • @Smandoli ah yeah, okay, no problem :) Commented Jul 18, 2013 at 15:30
  • That's correct. You cannot "directly" echo a Javascript value in a PHP script. Using AJAX, you could write it to an existing element. Or you would need to treat it as a form variable and POST or GET it on another page. Commented Jul 18, 2013 at 15:32

1 Answer 1

1

First of all, you'll need to change your PHP code (above) to look like this:

<script>
$(function() {
    $(document).on('click','.saveButton',function() {
        var imageNumber = $(this).closest('tr').data('imgNum');
        var imageLink = $(this).closest('tr').data('imgLink');
        $.get('process_img.php',{n:imgNumber,l:imgLink});
    });
});
</script>    
<?php
$data = mysql_query("SELECT * from tempimg"); 
while($hasil = mysql_fetch_array($data)){     
    $i++;
?>
<tr data-imgNum="<?=$i;?>" data-imgLink="<?=$hasil['imgLink'];?>">
    <td align='center'><input type="checkbox" name="check[]" value="<?=$hasil['idFoto'];?>" /></td>
    <td align='center'><img src="<?=$hasil['thumbPath'];?>" /></td>
    <td align='center'><?=$hasil['imgName'];?></td>
    <td align='center'><?=$hasil['thumbPath'];?></td>
    <td align='center'><?=$hasil['Path'];?></td>
    <td align='center'>
        <label class="text_label"><?=$hasil['imgLink'];?></label>
        <div class="edit"></div>
        <input type="text" align="center" value="<?=$hasil['imgLink'];?>" name="link[{<?=$hasil['idFoto'];?>}" id="link<?=$i;?>" />
        <div class="clear"></div> 
    </td>
    <td align="center">
        <input type="button" class="saveButton">
            <img src="images/sav.png" alt="search-btn" id="img" />
        </input>
    </td>
    <td align="center"><img src="images/del.png" /></td>
<?php
}
?>

Then you'll need to write a new PHP script named process_img.php:

<?php
$n = $_REQUEST['n']; // this is the NUMBER (70)
$i = $_REQUEST['i']; // this is the LINK (test.com)
//  save this info to the database
?>

This gives you a terrific head start. Look for tutorials online.......

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

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.