form>table (ajax)>form - html

I want to do different action for each submit button. For example compare will go to "compare.php" and delete will go to "test.php". I know i can't do form inside form but i can't separate to two forms because this is one table. I thought maybe to do
<?php if(isset($_POST["com"])){ javascript - link to compare.php}?>
but how can i send the _POST to this page?
I also thought on send the ind as _GET but the user can select more then one (two deletes or two compares)
of course if you have other solutions..
<html>
<head>
<script>
build ajax tbody...
for (i = 1; i <= numOfRows; i++)
{
oCell = oRow.insertCell(-1);
oCell.innerHTML = "<input type='checkbox' value='"+ ind + "'name='com[]'>";
oCell = oRow.insertCell(-1);
oCell.innerHTML = "<input type='checkbox' value='"+ ind + "'name='del[]' ;>";
.....
</script>
<body>
<form method="post" action="test.php">
<table>
<thead>
<tr>
<th><input type="submit" value="Compare" /></th>
<th><input type="submit" value="Delete" /></th>
</tr>
</thead>
<tbody></tdbody>
</table>
</form>
</body>
</html>
Thank you!

You can simply have:
<input type="submit" value="Compare" onclick="this.form.action = 'someotherpage.php';" />
This will change the form action upon clicking the submit button.

Related

How to Disable a Form element (textbox) if sql query retrieve no row as result

I want to make a textbox read only or disable if sql query retrieves no rows as result. What shall i write to make the textbox "text1" disabled/read only
<%
Set ObjRs=ObjConn.Execute(strSQL)
if not ObjRs.EOF then
CMSPI = ObjRs("CMSPI")
else
< Here i need to disable / make read only the combo box>
end if
%>
<HTML>
<Table>
<form method="POST" action="Dis****.asp" name="Form1">
<Tr>
<Td>
<input type="text" id="idtext1" name="text1" />
</td>
</tr>
</table>
</html>
You could probably make it work if you use something like this
document.getElementById("idtext1").style.display = "none";
<%
Dim isDisabled
Set ObjRs=ObjConn.Execute(strSQL)
if not ObjRs.EOF then
CMSPI = ObjRs("CMSPI")
else
isDisabled = " disabled"
end if
%>
<HTML>
<Table>
<form method="POST" action="Dis****.asp" name="Form1">
<Tr>
<Td>
<input type="text" id="idtext1" name="text1" / <%= isDisabled%>>
</td>
</tr>
</table>
</html>

insert data from html table to database jsp

i have a jsp page . i display books from database. in front of all book i have order button. I want to order book . When i order bookcode and bookname will add
to database border table. but i cant get data from html table. How i can get data from html table in jsp file?
st =conn.prepareStatement("select * from books where book_name like ? ");
st.setString(1, "%"+book+"%");
ResultSet rslt = st.executeQuery(); if(rslt.next()){ %>
<h3 align="center">Result Of Your Search </h3>
<table align="center">
<thead>
<tr>
<td>Book Name</td>
<td>Author</td>
<td>Title</td>
<td>Code</td>
<td>Page</td>
</tr>
</thead>
<tbody>
<% do {
%><tr>
<td><%=rslt.getString("book_name")%></td>
<td><%=rslt.getString("author")%></td>
<td><%=rslt.getString("title")%></td>
<td><%=rslt.getString("code")%></td>
<td><%=rslt.getString("page")%></td>
<td><form method="POST" action=book.jsp>
<input type="submit" value="order" name="order" /></form></td>
<% } while (rslt.next()); %>
</tbody>
</table>
'
i want when i click to order button book name and book code will add to order database table
You can do it with two ways;
Ajax Post Method (recommended and popular way)
You can create a js which calls http post action to book.jsp and save your order without change page. You have to learn how to post with ajax.
Form Post Method (ehhh way)
When you create your table you have to define different forms with hidden input areas:
<% do {
%><tr>
<form method="POST" action=book.jsp>
<td><%=rslt.getString("book_name")%></td>
<td><%=rslt.getString("author")%></td>
<td><%=rslt.getString("title")%></td>
<td><%=rslt.getString("code")%></td>
<td><%=rslt.getString("page")%></td>
<td>
<form method="POST" action=book.jsp>
<input type="hidden" value="<%=rslt.getString("book_name")%>" name="book_name" />
<input type="hidden" value="<%=rslt.getString("author")%>" name="author" />
<input type="hidden" value="<%=rslt.getString("code")%>" name="code" />
.
.
.
<input type="submit" value="order" name="order" /></form></td>
</form>
<% } while (rslt.next()); %>
Then you can use getParameter("book_name");
OR
Just add:
<input type="hidden" value="<%=rslt.getString("id")%>" name="id" />
And you can;
`String id = request.getParameter("id");`
//Insert DB

required field in html form works in chrome but not mobile or ie (done in wordpress)

I am building a form using html in wordpress. The fields that need to be required work in chrome (giving a please fill out this field when the submit button is pressed without information in the field) however it will not work in IE or mobile, it just allows the form to be submitted. Here is a sample of a field
Last name: *
input type="text" name="lastname" value="" maxlength="50" required="required"
I dont know whats going on here and Im pretty new so any help would be appreciated
(edit with answer)
This is what I ended up doing and it works now thanks for the responses
<html>
<head>
<script type="text/javascript">
function validateForm()
{
var x=document.forms["myForm"]["firstname"].value;
if (x==null || x=="")
{
alert("First name must be filled out");
return false;
}
}
</script>
</head>
<body>
<form name="myForm" form action="dlcplateFormTest.php" onsubmit="return validateForm()" method="POST">
<tr>
<td>Last name: *</td>
<td><input type="text" name="lastname" value="" maxlength="50" required="required" /> </td>
</tr>
<tr>
<input type="submit" value="Submit" /><input type="reset" value="Reset" />
</form>
</body>
</html>
you need to write a javascript function that checks if the fields are "" or not if they are "" then cancel request

how to pass value of checkboxes using ahref

I am using following code in my project
<cfoutput query="getOptions">
<tr>
<td align="center"> #optionname#</td>
<td align="center"> #DollarFormat(optionprice)#</td>
<td><input type="checkbox" name="OptionalID" value="#OptionID#" ></td>
</tr>
</cfoutput>
And i am passing the value to other form as follows
<a href="addtocart.cfm?pid=#productId#&OptionalID=#OptionalID#">
what should i do to pass all values of all the checkboxes that are checked.Please help
Thanks in advance
Change your page to use a form instead of passing the values via a link.
Something along the lines of:
<form name="myform" action="addToCart.cfm" method="post">
<cfoutput>
<input type="hidden name="pid" value="#productId#">
<input type="hidden name="OptionalID" value="#OptionalID#">
<cfloop query="getOptions">
<tr>
<td align="center"> #optionname#</td>
<td align="center"> #DollarFormat(optionprice)#</td>
<td><input type="checkbox" name="OptionalID" value="#OptionID#"></td>
</tr>
</cfloop>
<input type="submit" value="Add to cart">
</cfoutput>
</form>
You can pass as many values as you want now, and the checkboxes will come up as a list.
hope that helps
Though you can get tricky with JavaScript, if you use an A to add to cart, you'll only be passing the values in the URL. You want to use a form instead.
<form name="cartForm" action="addtocart.cfm?pid=#productId#&OptionalID=#OptionalID#" method="POST">
. . . .
</form>
Of course, this means you'd ordinarily use a submit button inside your form. If you're set on using a link, you can do
Add to cart
(note that I've omitted various tags and escaping of # for simplicity)
You need JavaScript for that.
First, have this in your page where you have the code you posted:
<script type="text/javascript">
function AddCheckboxValues(oLink, sName) {
var arrCheckboxes = document.getElementsByName(sName);
var values = [];
for (var i = 0; i < arrCheckboxes.length; i++) {
if (arrCheckboxes[i].checked) {
values.push(arrCheckboxes[i].value);
}
}
oLink.href += "&" + sName + "=" + values.join(",");
}
</script>
Second, add onclick to the link tag:
<a href="addtocart.cfm?pid=#productId#" onclick="AddCheckboxValues(this, 'OptionalID');">
That's it, now the form addtocart.cfm will get query string values of the checkboxes ticked by the user.

delete row corresponding to button clicked in jsp

userdetails.jsp
<tr>
<td>
<%
out.println(rs.getString(1));
name=rs.getString(1);
out.print("<input type='hidden' name='user' value='"+name+"'");
%>
</td>
<td>
<%out.println(rs.getString(2));
%>
</td>
<td>
<%out.println(rs.getString(3));
%>
</td>
<td>
<%out.println(rs.getString(4));
%>
</td>
<td>
<input type="Submit" name="delete_user" value="Delete"/>
</td>
</tr>
when i click the delete button only first row is getting deleted and not the row corresponding to which button is clicked
You're putting multiple hidden input values and delete buttons altogether in the same form. When you use request.getParameter() to get the hidden input value, you will indeed only get the first one, regardless of the delete button pressed.
You need to group the hidden input and the delete button in their own forms.
<td>
<form action="delete" method="post">
<input type="submit" name="delete_user" value="Delete" />
<input type="hidden" name="user" value="<%=rs.getString(1)%>" />
</form>
</td>
This way the request will always have the one and the right user name as parameter.
Said that, using scriptlets in JSP is 90's way of writing JSPs and also mingling database logic in a JSP is not really a good practice. I'd suggest to get yourself through this answer.
You can add the user name to the button value upon clicking it:
<input type="Submit" name="delete_user" value="Delete" onclick="this.value += ' <% out.print(name); %>'; this.disabled = true; " />
Then in the server side code, parse the value: get the text after first space, which is the user name to delete and reference only row with that user name.