close ad
Databridge V2 with MySQLi support IS Now Available!
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

javaScript to prevent form submit with 0 value

Thread began 1/18/2013 3:38 pm by Roxana | Last modified 1/20/2013 8:00 pm by Roxana | 6137 views | 9 replies |

Roxana

javaScript to prevent form submit with 0 value

Hi there,

I noticed, that on my shop site it is possible to put items into the cart, even if their value is at $ 0.
To explain, some of the products require version and/or size selection, and the price of the selection is instantly displayed. So, if no selection is made, the price is at $ 0.00.

Now I am trying to prevent the page from submitting (putting items into the cart), if a necessary selection was not made and the displayed price is still at $ 0.

Having no javaScript training whatsoever, I concocted this from the internet:

<script type="text/javascript">
function checkForm() {
//.....
if (WA_eCart_DisplayMoney($shop, $row_rspricetable['price']); == "0") {
alert("Make a selection");
return false;
}
}
</script>



and

<form action="<?php echo $_SERVER["PHP_SELF"]; ?><?php echo (isset($_SERVER["QUERY_STRING"]) && $_SERVER["QUERY_STRING"] != "")?"?".$_SERVER["QUERY_STRING"]:""; ?>" onclick="checkForm();" method="post" name="shop_1_ATC_<?php echo $row_DetailRS1["product_id"]; ?>" id="segufix_1_ATC_<?php echo $row_DetailRS1["product_id"]; ?>" >



... where the "onclick" is the only thing I added, everything else was already there.

Could someone help me out?

:)

Sign in to reply to this post

Jason ByrnesWebAssist

a form tag does not have an onclick event.

the onclick event needs to be applied to the submit button.

Sign in to reply to this post

Roxana

Hi Jason,

Thanks for your reply

I deleted the code from the form tag and put it in the submit button instead. Looks like this:

<input type="submit" value="<?php echo $row_rscontent['description4']; ?>" name="shop_1_ATC" onclick="return checkForm();"/>



It's still not working though.

Sign in to reply to this post

Jason ByrnesWebAssist

send a link please

Sign in to reply to this post

Roxana

detailTest1.php?recordID=13

Sign in to reply to this post

Jason ByrnesWebAssist

you can refer to a php variable in a javascript function.

you would need to use php code to look at the value of the $row_rspricetable['price'] variable:

<?php
if($row_rspricetable['price'] == "0") {
echo("make a selection")
}
?>

Sign in to reply to this post

Roxana

Did you mean, I cannot refer to a php variable in a javascript function?

I needed to add ";" to the code, so it didn't show a syntax error. Then I put it in the head section of the page, but it still allows the item to be put into the cart. I also tried it right after the code for the version and size selection, but that also didn't work. I don't know if adding the ";" messed it all up.

The code looks like this now:

<?php
if ($row_rspricetable['price'] == "0")
{
echo("make a selection");
}
?>

On page load, when the price is still $0, it does not display the message. I thought it might. But it would not only have to display the message but also prevent the item from being added to the cart.

Sign in to reply to this post

Roxana

Oh no, I just noticed I get this message on the live site, when I try to put that item in the cart:
Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/www.segufixshop.eu/web/detailTest1.php:350) in /var/www/html/www.segufixshop.eu/web/detailTest1.php on line 386

Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/www.segufixshop.eu/web/detailTest1.php:350) in /var/www/html/www.segufixshop.eu/web/detailTest1.php on line 420


Maybe I started this all wrong. I tried to have both version and size with nothing preselected. So I added in the table tblPrice an item with "-" for version and size at a price of $0.00. With this I achieved to have no version or size preselected, but instead of forwarding to the cart page, it adds the item in the cart but stays on the detail page with the ugly warning.

This warning did not show up when I tested locally, and it also does not show up for other items. Just item 13, for which I added the $0.00 item line to the data base.

If this goes beyond the help you can give me here, please let me know. I will wait a little while, but then delete the $0.00 lines from the live data base.

Sign in to reply to this post

Roxana

I opened a new thread about the warning, because it seems to have nothing to do with adding lines to the database.

This is how far I have gotten:

This code placed right behind the size drop down menu will echo "make a selection" when I try to add a $0 item to the cart, but it does not prevent the item from being added to the cart.

<span class="validation">
<?php
if ($row_rspricelookup['price'] == "0")
{
echo"<--", "&nbsp;", ($row_rscontent3['description5']);
}
?>
</span>



With this code I successfully call a javascript function, when I hit submit (shortened for better viewing):

form action="Action" method="post" name="Name" id="ID" onsubmit="return checkForm();">



And this is the javascript function:

<script type="text/javascript">
function checkForm()
{
var prize = ($row_rspricelookup['price']);
if (prize == "0")
{
alert("Check size selection");
return false;
}
}
</script>



Not working. However when I leave out the variable and the if condition, the alert box will pop up instead of the page submitting. So I know, that the function is properly called, just the condition/variable is wrong.

I want to compare the current price to "0", but don't know how to incorporate the database value into the javascript function. ($row_rspricelookup['price']) worked for the php code, but not for the javascript.

Jason, could you tell me, how to call the value?

Sign in to reply to this post

Roxana

I think I found a solution. I added "&& ($row_rspricelookup['price'] != "0")", to the add to cart php code, so the first few lines look like this now:

<?php
// WA eCart AddToCart
if (isset($_POST["segufix_1_ATC"]) && ($row_rspricelookup['price'] != "0") || isset($_POST["segufix_1_ATC_x"])) {
$ATC_itemID = $_POST["segufix_1_ID_Add"];
$ATC_AddIfIn = 0;
$ATC_RedirectAfter = "cart.php";
$ATC_RedirectIfIn = "";



BTW, tried this also with item 35 (only item 13 and item 35 have a $0 line. Item 35 does not get the warning message mentioned in earlier posts, so I am sure the warning message must have been a problem before.

Sorry about the many posts, but this was a work in progress :)

Sign in to reply to this post
loading

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...