bernte
bernte

Reputation: 1184

PHP ⎼ Do not round numbers

I have to put 2 id-s together from $_POST then I want to explode them, example:

My id-s are 38 and 310. I made the id-s to this id = "38.310" in my html file.

After $_POST I want to explode the id-s:

$id=$_POST['id'];

echo($id);  // Gives 38.31
$new_id = explode(".", $id);

echo($new_id[0]); // Gives 38
echo($new_id[1]); // Gives 31

Is the a way to get these id-s not rounded?

I need the 38 and the 310! The id 310 can be also 1003000 ...

EDIT:

function dostuff(document, message, id, action) {
        var eingabe;
        eingabe = confirm(message);
        if (eingabe === true) {
            document.msgform.action.value = action;
            document.msgform.id.value = id;
            document.msgform.submit();
        } else {
            return true;
        }
        return false;
    }

LINK

<a href="#" onclick="dostuff(document, 'MESSAGE',<?php echo($row['vID']);?>.<?php echo($row['id']);?>,'FITTER_fitter_repaired');" class="button small red" >ACTION</a>

$row['vID'] = 38

$row['ID'] = 310

and my submit looks like this

<form enctype="multipart/form-data" name="msgform" action="" method="post">
    <input type="hidden" value="" name="action"/>
    <input type="hidden" value="" name="id"/>
</form>

Upvotes: 0

Views: 275

Answers (4)

bitWorking
bitWorking

Reputation: 12655

I don't know what the function dostuff is doing, but Javascript is the evil one. Put quotes around the values. In this way there is no casting involved and it gets posted as string.

<a href="#" 
onclick="dostuff(document, 'MESSAGE', '<?php echo($row['vID']);?>.<?php echo($row['id']);?>','FITTER_fitter_repaired');" class="button small red" >ACTION</a>
                                      ^                                                    ^                             

EDIT:

But I also think that Lee's solution would be better. Just make 2 input fields and fill them in your dostuff function. You could also give id's to the input fields to make it easier to fill.

<input id="id1" type="hidden" value="" name="id[]"/>
<input id="id2" type="hidden" value="" name="id[]"/>

Upvotes: 1

RiggsFolly
RiggsFolly

Reputation: 94662

Alternatively, if you dont understand @Lee answer you could use something other than a . as the seperator so PHP does not assume the variable is a number. Best not use a coma either as that is the decimal in some countries.

So for example use the dollar sign `id="38$310"

and $new_id = explode("$", $id);

Upvotes: 1

Lee
Lee

Reputation: 10603

Not 100% sure why your doing that, but why not just use a POST array like

id[]=38&id[]=310

Upvotes: 5

Jason McCreary
Jason McCreary

Reputation: 72981

is the a way to get these ids not rounded?

There are a few ways, I prefer to cast it to an integer:

$id = (int)$_POST['id'];

Others ways would be intval() or floor().

Upvotes: 2

Related Questions