jQuery Help

9 posts by 2 authors in: Forums > CMS Builder
Last Post: August 7, 2014   (RSS)

By Perchpole - August 6, 2014

Hello, All -

I've got a very quick issue I need to resolve regarding jQuery. I'm using the following bit of code to $post data to a save file - which then updates a CMSB table:

// <![CDATA[
function ClickToSave () {
    var data1 = form.title.getData();
    var data2 = form.summary.getData();
    var data3 = form.content.getData();
    var data4 = "<?php echo $selectedPageNum; ?>";
    $.post('save.php', {
        title: data1,
        summary: data2,
        content: data3,
        num: data4
      });
    }
// ]]>

It works fine. The only flaw is that it requires you to provide a value for each var otherwise it breaks.

What I want to do is put in some kind of conditional statement so that if a value doesn't exist a default value will be used instead.

So, in the example above, if form.content.getData(); is empty (or does not exist) then var data3 = "no content".

How does that work in jQuery?

:0/

Perch

By claire - August 6, 2014

Hey Perch

You can use the length property of  a string to check if the different variables are set, like so:

if(data1.length < 1) {
    data1 = 'no content';
}

Basically if there's nothing in those fields, the string length will be 0 and this if statement will be triggered.

--------------------

Claire Ryan
interactivetools.com

Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

By claire - August 6, 2014

You can add it in just after this line, for whatever variables you want to check.

var data4 = "<?php echo $selectedPageNum; ?>";

--------------------

Claire Ryan
interactivetools.com

Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

By Perchpole - August 7, 2014

Ah! Thanks. It leads nicely to my next question:

Using the example above, how do I pass the value of a radio button array to a var?

:0/

Perch

By claire - August 7, 2014

You can't, as far as I know. Radio buttons only pass one value because they're an either-or selection - you can't choose two radio buttons in the same group. Selecting one deselects whatever you've already chosen.

They usually look something like this:

Choose one:

Option 1: <input type="radio" name="thischoice" value="option1" />
Option 2: <input type="radio" name="thischoice" value="option2" />
Option 3: <input type="radio" name="thischoice" value="option3" />

So when you're getting the value here, the variable will just look like $_REQUEST['thischoice'] and its value will be either option1, option2, or option3.

I'm not sure what you're trying to do, but if you need an array of variables, a list of checkboxes is probably a better idea.

--------------------

Claire Ryan
interactivetools.com

Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

By Perchpole - August 7, 2014

Hi, Claire -

Sorry, I didn't describe it very well. What I want to do is exactly as you have put it. How do I assign the value of "thischoice" to the var?

Perch

By claire - August 7, 2014

Sure - just like this:

var thischoice = $('input[name=thischoice]:checked').val()

--------------------

Claire Ryan
interactivetools.com

Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

By Perchpole - August 7, 2014

You star!

Thanks again.

:0)

Perch