Click here to Skip to main content
15,886,055 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a JSP that will show a list of friends and buttons. There's also a search box and I'm using jstl sql:query to select the friends. This worked.
However, I have buttons to add friend in each row. So I'm using sql:update to insert the param values and it didn't work.

Code:

<sql:setDataSource
            var="myDS"
            driver="com.mysql.jdbc.Driver"
            url="jdbc:mysql://localhost:8888/diario"
            user="root" password="narumi"
    />

HTML
<sql:query var="srh" dataSource="${myDS}">
        SELECT * FROM profiles WHERE locate(?, username)>0;
        <sql:param value="${param.search}"/>

    </sql:query>

    <form id="search">
    <input type="search" name="search"  title="search" />
    <button type="submit" name="find"> Search</button>
    </form>

    <c:forEach var="row" items="${srh.rows}">


<form name="lol" method="post" >
                       <input type="hidden" name="id" value="${row.id}" >
                       <input type="hidden" name="fUNAME" value="${row.username}" />
                       <input type="hidden" name="location" value="${row.location}"/>
                       <input type="hidden" name="MF" value="${row.MF}"/>
                       <a type="submit" name="add" class="btn btn-default  btn-xs glyphicon glyphicon-user"> + Add Friend</a>
                   </form>
                   <c:if test="${pageContext.request.getParameter('lol')}">
                       <sql:update var="add" dataSource="${myDS}">
                           INSERT INTO friends (id, fUNAME, Location, MF) VALUES (?, ?, ?, ?);
                           <sql:param value="${param.id}"/>
                           <sql:param value="${param.fUNAME}"/>
                           <sql:param value="${param.location}"/>
                           <sql:param value="${param.MF}"/>
                       </sql:update>
                       <c:if test="${add>=1}">
                           <c:redirect url="friendslist.jsp"/>
                       </c:if>
                   </c:if>

               </div>

           </div>
           </c:forEach>


As you can see, the sql:param values are fetched from hidden input row values (retrieved data).

I've also created servlet and DAO class to add friend. I've tried using in jsp but also didn't work...
HTML
<form name="lol" method="post" action="/addFriend">
                         <input type="hidden" name="id" value="${row.id}" >
                         <input type="hidden" name="fUNAME" value="${row.username}" />
                         <input type="hidden" name="location" value="${row.location}"/>
                         <input type="hidden" name="MF" value="${row.MF}"/>
                         <a type="submit" name="add" class="btn btn-default  btn-xs glyphicon glyphicon-user"> + Add Friend</a>
                     </form>

action="/addFriend" is the servlet's urlPatttern.
Is there any mistakes in this code?
Posted

1 solution

I've found the solution...and it's undetectable.
The answer...
Just change the
HTML
<a></a>
type button and use
HTML
<button></button>
instead.
I've wasted the whole day for this very simple solution >.<</xml>
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900