콤마 값을 제외하고 전달하려고 합니다.
본문
안녕하세요 항상 도움 받고 있습니다. 숫자를 적어서 submit 을 할 때 숫자가 적힐 때는 콤마가 적용되게 하고 싶지만 post 로 전달 할 때는 콤마 값을 없애고 싶습니다. 어떻게 해야 하나요?
코드는 아래와 같습니다.
<tr>
<th>
<label class="control-label" for="">매매가 가격범위</label>
</th>
<td>
<input type="text" name="trading_price_from" id="trading_price_from" class=" form-control box_style" placeholder="" value="<?php echo $trading_price_from; ?>">
<span>~</span>
<input type="text" name="trading_price_to" id="trading_price_to" class=" form-control box_style" placeholder="" value="<?php echo $trading_price_to; ?>">
<span>원</span>
</td>
</tr>
<script>
// 천 단위 콤마를 추가하는 함수
function addCommas(input) {
return input.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
// 값이 입력될 때마다 천 단위 콤마 추가
document.getElementById('trading_price_from').addEventListener('input', function () {
this.value = addCommas(this.value.replace(/,/g, ''));
});
document.getElementById('trading_price_to').addEventListener('input', function () {
this.value = addCommas(this.value.replace(/,/g, ''));
});
</script>
bbs/list.php
// 콤마를 제거하여 정수로 변환
$trading_price_from = isset($_POST['trading_price_from']) ? (int)str_replace(',', '', $_POST['trading_price_from']) : null;
$trading_price_to = isset($_POST['trading_price_to']) ? (int)str_replace(',', '', $_POST['trading_price_to']) : null;
// 매매가 가격범위 검색 조건 추가
if ($trading_price_from !== null && $trading_price_to !== null) {
$sql_search .= "AND (CONVERT(REPLACE(trading_price, ',', ''), SIGNED) BETWEEN '{$trading_price_from}' AND '{$trading_price_to}') ";
}
항상 감사합니다.
답변 2
submit 하시기 전에 콤마를 제거하시면 됩니다.
function removeComma(n) {
return n.replace(/(,)/g, "");
}
php 정규식 숫자만 구글링